Can't get Temperature table to work in java - java

I've been trying to get this program to work for hours now. I managed to get the Fahrenheit to Celsius conversion to work, but for the life of me I can't do the same for the Celsius to Fahrenheit. I'm going to ask my teacher when school starts up again, but as of now I would appreciate some help. Here's my code.
import java.util.Scanner;
public class TempConvert {
public static void main(String[] args) {
//variables
double number;
int maxFahrenheit = 52;
int maxCelsius = 20;
Scanner keyboard = new Scanner(System.in);
//user input
System.out.println("Please enter 1 to convert from Fahrenheit to" +
" Celsius");
System.out.println("Please enter 2 to convert from Celsius to" +
" Fahrenheit");
number = keyboard.nextDouble();
//if else
if (number == 1) {
System.out.println("Celsius\t\tFahrenheit");
System.out.println("---------------------------");
int i;
double c;
for (i = 0, c = 0; i <= maxCelsius; i++, c++) {
System.out.println(i + "\t\t" + 9 * (i + 32) / 5);
} //end for
} //end if
else if (number == 2) {
System.out.println("Fahrenheit\t\tCelsius");
System.out.println("-----------------------------------");
int i;
double f;
for (i = 32, f = 32; i <= maxFahrenheit; i++, f++) {
System.out.println(i + "\t\t" + 5 * (f - 32) / 9);
} //end for
} //end else if
else {
System.out.println("Entry is invalid");
} //end else
} //end main
} //end TempConvert

You need to use a DecimalFormat df = new DecimalFormat("#.####");
Look i changed your code like this:
public static void main(String[] args) {
//variables
double number;
int maxFahrenheit = 52;
int maxCelsius = 20;
Scanner keyboard = new Scanner(System.in);
//user input
System.out.println("Please enter 1 to convert from Fahrenheit to" +
" Celsius");
System.out.println("Please enter 2 to convert from Celsius to" +
" Fahrenheit");
number = keyboard.nextDouble();
//if else
if(number == 1) {
System.out.println("Celsius\t\tFahrenheit");
System.out.println("---------------------------");
int i;
double c;
for(i = 0, c = 0; i <= maxCelsius; i++, c++) {
System.out.println(i + "\t\t" + 9 * (i + 32) / 5);
}//end for
}//end if
else if(number ==2) {
System.out.println("Fahrenheit\t\tCelsius");
System.out.println("-----------------------------------");
int i;
double f;
DecimalFormat df = new DecimalFormat("#.####");
for(i = 32, f = 32; i <= maxFahrenheit; i++, f++) {
System.out.println(i + "\t\t" + df.format(5 * (f - 32) / 9));
}//end for
}//end else if
else {
System.out.println("Entry is invalid");
}//end else
}//end main
}//end TempConvert
Look it works better like google and if you post this in google you will see 42 fahrenheit to celsius
42 = 5.55556
just as your tool =)

Related

About while loops and if statements nested

I'm working on this project challenge called "Algebra Tutor" for my very first java class.
The program should be able to output a question to decide whether you want to solver for m or b or y in the formula y = mx + b.
Once one of those is pick by a number: "Select 1 to Solve for Y, 2 to Solve for M, 3 to Solve for B and 4 to quit. "
It should give you an output to solve the formula.
I'm working in part 3 of the project which is:
Update your program so that each problem type mode will repeatedly ask questions until the user gets 3 correct answers in a row.
If the attempts more than 3 questions in a particular mode, the program should provide a hint on how to solve problems of this type.
After the student has correctly answered 3 questions in a row, an overall score (the number of questions answered correctly divided by the total number of questions attempted) should be displayed, and the menu is presented again.
So I'm getting stuck on how I can place a counter that starts at 0 and every time the user answers correctly then 1 will be added to the counter and quit if it gets 3 in a row or go back to 0 if the user answer is incorrect.
I will provide my code I'm getting confused on how having a while loop inside of the if statement and then another while loop for the count, it's confusing I know but once you see my code you will see what I'm talking about.
So I know I can place a counter like this:
int counter = 0;
while (counter < 4)
if statement here
counter ++;
else
counter = 0;
import java.util.Scanner;
class AlgebraTutor {
public static void main(String[] arg) {
double min_value = -100;
double max_value = 100;
double m_value = generate_random(max_value, min_value);
double x_value = generate_random(max_value, min_value);
double b_value = generate_random(max_value, min_value);
double y_value = generate_random(max_value, min_value);
Scanner user_input = new Scanner(System.in);
int user_answer_int = 0;
while ((user_answer_int < 4) && (user_answer_int >= 0)) {
System.out.println("Select 1 to Solve for Y, 2 to Solve for M, 3 to Solve for B and 4 to quit. ");
user_answer_int = user_input.nextInt();
if (user_answer_int == 1) {
check_answer_for_y(m_value, x_value, b_value);
}
else if (user_answer_int == 2) {
check_answer_for_m(x_value, y_value, b_value);
}
else if (user_answer_int == 3) {
check_answer_for_b(m_value, x_value, y_value);
}
else {
System.out.println("You are done");
}
}
}
static void check_answer_for_m(double x_value, double y_value, double b_value) {
System.out.println("Solve For M Problem ");
System.out.println("Given: ");
System.out.println("b = " + b_value);
System.out.println("x = " + x_value);
System.out.println("y = " + y_value);
System.out.print("What is the value of m? ");
Scanner user_input = new Scanner(System.in);
String user_answer_m = "";
user_answer_m = user_input.next();
int user_answer_int = Integer.parseInt(user_answer_m);
int correct_answer_m = (((int)y_value - (int)b_value) / (int)x_value);
if (user_answer_int == correct_answer_m){
System.out.println("You are correct!");
} else {
System.out.print("Sorry, that is incorrect. ");
System.out.println("The answer is " + correct_answer_m);
}
}
static void check_answer_for_b(double m_value, double x_value, double y_value) {
System.out.println("Solve For B Problem ");
System.out.println("Given: ");
System.out.println("m = " + m_value);
System.out.println("x = " + x_value);
System.out.println("y = " + y_value);
System.out.print("What is the value of b? ");
Scanner user_input = new Scanner(System.in);
String user_answer_b = "";
user_answer_b = user_input.next();
int user_answer_int = Integer.parseInt(user_answer_b);
int correct_answer_b = ((int)y_value - ((int)m_value * (int)x_value));
if (user_answer_int == correct_answer_b){
System.out.println("You are correct!");
} else {
System.out.print("Sorry, that is incorrect. ");
System.out.println("The answer is " + correct_answer_b);
}
}
static void check_answer_for_y(double m_value, double x_value, double b_value) {
System.out.println("Solve For Y Problem ");
System.out.println("Given: ");
System.out.println("m = " + m_value);
System.out.println("x = " + x_value);
System.out.println("b = " + b_value);
System.out.print("What is the value of y? ");
Scanner user_input = new Scanner(System.in);
String user_answer_y = "";
user_answer_y = user_input.next();
int user_answer_int = Integer.parseInt(user_answer_y);
int correct_answer_y = (int) m_value * (int) x_value + (int) b_value;
if (user_answer_int == correct_answer_y) {
System.out.println("You are correct!");
} else {
System.out.print("Sorry, that is incorrect. ");
System.out.println("The answer is " + correct_answer_y);
}
}
static int generate_random(double max_value, double min_value) {
return (int) ((int) (Math.random() * ((max_value - min_value)+ 1)) + min_value);
}
}
Where I stated my while loop for the number entered by user
depending on the number then the if statement jumps in
where can I place my counter to start and catch all the if statements depending on the number the user picks and then if the user gets 3 correct in a row the program should go back to the main question. Can I do another while loop inside the one I already have? Or should I place it inside each if statement?
Place the loop here for each
else if (user_answer_int == 3) {
check_answer_for_b(m_value, x_value, y_value);
}
changing your main function to this.
public static void main(String[] arg) {
double min_value = -100;
double max_value = 100;
double m_value = generate_random(max_value, min_value);
double x_value = generate_random(max_value, min_value);
double b_value = generate_random(max_value, min_value);
double y_value = generate_random(max_value, min_value);
Scanner user_input = new Scanner(System.in);
int user_answer_int = 0;
while ((user_answer_int < 4) && (user_answer_int >= 0)) {
System.out.println("Select 1 to Solve for Y, 2 to Solve for M, 3 to Solve for B and 4 to quit. ");
user_answer_int = user_input.nextInt();
int i = 0;
if (user_answer_int == 1) {
while (i < 3){
if (check_answer_for_y(generate_random(max_value, min_value), generate_random(max_value, min_value), generate_random(max_value, min_value))) {
i++;
}
else {
i = 0;
}
}
}
else if (user_answer_int == 2) {
while (i < 3){
if (check_answer_for_m(generate_random(max_value, min_value), generate_random(max_value, min_value), generate_random(max_value, min_value))) {
i++;
}
else {
i = 0;
}
}
}
else if (user_answer_int == 3) {
while (i < 3){
if (check_answer_for_b(generate_random(max_value, min_value), generate_random(max_value, min_value), generate_random(max_value, min_value))) {
i++;
}
else {
i = 0;
}
}
}
else {
System.out.println("You are done");
}
}
}
static boolean check_answer_for_m(double x_value, double y_value, double b_value) {
System.out.println("Solve For M Problem ");
System.out.println("Given: ");
System.out.println("b = " + b_value);
System.out.println("x = " + x_value);
System.out.println("y = " + y_value);
System.out.print("What is the value of m? ");
Scanner user_input = new Scanner(System.in);
String user_answer_m = "";
user_answer_m = user_input.next();
int user_answer_int = Integer.parseInt(user_answer_m);
int correct_answer_m = (((int)y_value - (int)b_value) / (int)x_value);
if (user_answer_int == correct_answer_m){
System.out.println("You are correct!");
return true;
} else {
System.out.print("Sorry, that is incorrect. ");
System.out.println("The answer is " + correct_answer_m);
return false;
}
}
static boolean check_answer_for_b(double m_value, double x_value, double y_value) {
System.out.println("Solve For B Problem ");
System.out.println("Given: ");
System.out.println("m = " + m_value);
System.out.println("x = " + x_value);
System.out.println("y = " + y_value);
System.out.print("What is the value of b? ");
Scanner user_input = new Scanner(System.in);
String user_answer_b = "";
user_answer_b = user_input.next();
int user_answer_int = Integer.parseInt(user_answer_b);
int correct_answer_b = ((int)y_value - ((int)m_value * (int)x_value));
if (user_answer_int == correct_answer_b){
System.out.println("You are correct!");
return true;
} else {
System.out.print("Sorry, that is incorrect. ");
System.out.println("The answer is " + correct_answer_b);
return false;
}
}
static boolean check_answer_for_y(double m_value, double x_value, double b_value) {
System.out.println("Solve For Y Problem ");
System.out.println("Given: ");
System.out.println("m = " + m_value);
System.out.println("x = " + x_value);
System.out.println("b = " + b_value);
System.out.print("What is the value of y? ");
Scanner user_input = new Scanner(System.in);
String user_answer_y = "";
user_answer_y = user_input.next();
int user_answer_int = Integer.parseInt(user_answer_y);
int correct_answer_y = (int) m_value * (int) x_value + (int) b_value;
if (user_answer_int == correct_answer_y) {
System.out.println("You are correct!");
return true;
} else {
System.out.print("Sorry, that is incorrect. ");
System.out.println("The answer is " + correct_answer_y);
return false;
}
}
static int generate_random(double max_value, double min_value) {
return (int) ((int) (Math.random() * ((max_value - min_value)+ 1)) + min_value);
}

Java | how can I use multiple methods to print out grade scores?

import java.util.Scanner;
public class BA4 {
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Hello Drews, how many total grades do you want to process?");
int numberOfGrades = keyboard.nextInt();
int[] storeGrades = new int[numberOfGrades];
for (int i = 0; i < numberOfGrades; i++) {
System.out.println("Please enter grade " + (i + 1) + ": ");
storeGrades[i] = keyboard.nextInt();
}
System.out.println("Total score is: " + (getTotalScore(storeGrades)));
System.out.println("Lowest score is: " + (getLowestScore(storeGrades)));
System.out.println("Highest score is: " + (getHighestScore(storeGrades)));
System.out.println("Average score is: " + (averageScore(String.format("%.2f", storeGrades))));
}
public static int getTotalScore(int[] storeGrades) {
int sum = 0;
for (int i = 0; i < storeGrades.length; i++) {
sum += storeGrades[i];
}
return sum;
}
public static int getLowestScore(int[] storeGrades) {
int getLowestScore = 0;
for (int i = 0; i > storeGrades.length; i++) {
getLowestScore = storeGrades[i];
}
return getLowestScore;
}
public static int getHighestScore(int[] storeGrades) {
int getHighestScore = 0;
for (int i = 0; i < storeGrades.length; i++) {
getHighestScore = storeGrades[i];
}
return getHighestScore;
}
public static double averageScore(double[] storeGrades) {
double averageScore = 0;
for (int i = 0; i < storeGrades.length; i++) {
averageScore = (double) storeGrades[i];
}
return averageScore;
}
public static int printGrade(int[] storeGrades) {
int printGrade;
if (printGrade > 89) {
String gradeSoFar = "A";
System.out.println("Your grade so far is an " + gradeSoFar);
}
else if ((printGrade > 79) && (printGrade < 90)) {
String gradeSoFar = "B";
System.out.println("Your grade so far is a " + gradeSoFar);
}
else if ((printGrade > 69) && (printGrade < 80)) {
String gradeSoFar = "C";
System.out.println("Your grade so far is a " + gradeSoFar);
}
else if ((printGrade > 59) && (printGrade < 70)) {
String gradeSoFar = "D";
System.out.println("Your grade so far is a " + gradeSoFar);
}
else if ((printGrade > 0) && (printGrade < 60)) {
String gradeSoFar = "F";
System.out.println("Your grade so far is an " + gradeSoFar);
}
return printGrade;
}
}
I am trying to figure out where I am going wrong. I have a couple of errors which leads me to believe I really just don't understand methods as well as I thought I did.
The goal is to create 5 methods displaying to the user the total, lowest, highest and average scores, and then to print the letter grade. Thank you for your assistance to this noobie java coder! :)
You are passing in a String when you should be passing in a Double[] into averageScore function in this line:
System.out.println("Average score is: " + (averageScore(String.format("%.2f", storeGrades))));
and you did not initialize the printGrade variable inside the printGrade function, you need to give it an initial value if you are going to use it in a comparison.
That's all the errors that

How to set a limit in a for loop?

I have to create a program to calculate the average of each students' scores. I managed to do that but how can I limit the score to be only between 0 to 100? I've searched other questions and many shows to put while statement. The problem is that I don't know where to add the while. So here's the code:
import java.util.Scanner;
public class AverageScore {
public static void main(String[] args) {
int x; // Number of students
int y; // Number of tests per student
int Score = 0; //Score of each test for each student
double Average = 0; //Average score
double Total = 0; //Total score
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the number of students: ");
x = keyboard.nextInt();
System.out.println("Please enter the amount of test scores per student: ");
y = keyboard.nextInt();
for (int z = 0; z < x; z++)
{
System.out.println("Student " + (z + 1));
System.out.println("------------------------");
for (int g=0; g < y; g++)
{
System.out.print("Please enter score " + (g + 1) + ": ");
Total += new Scanner(System.in).nextInt();
Total += Score;
Average = (Total/y);
}
System.out.println("The average score for student " + (z + 1) + " is " + Average);
System.out.println(" ");
Total= 0;
}
keyboard.close();
}
}
If there is any other ways please do state. Thanks in advance.
import java.util.Scanner;
public class AverageScore {
public static void main(String[] args) {
int x; // Number of students
int y; // Number of tests per student
int Score = 0; //Score of each test for each student
double Average = 0; //Average score
double Total = 0; //Total score
double Input = 0; **//Add this in your variable**
boolean Valid = false; **//Add this in your variable**
Scanner keyboard = new Scanner(System.in);
System.out.println("Please enter the number of students: ");
x = keyboard.nextInt();
System.out.println("Please enter the amount of test scores per student: ");
y = keyboard.nextInt();
for (int z = 0; z < x; z++)
{
System.out.println("Student " + (z + 1));
System.out.println("------------------------");
for (int g=0; g < y; g++)
{
System.out.print("Please enter score " + (g + 1) + ": ");
Input = new Scanner(System.in).nextInt();
//validation of your input from 0 to 100
if(Input>=0&&Input<=100)
{
Valid = true;
}
//enter while loop if not valid
while(!Valid)
{
System.out.println("");
System.out.print("Please enter a valid score " + (g + 1) + ": ");
Input = new Scanner(System.in).nextInt();
if(Input>=0&&Input<=100)
{
Valid = true;
}
}
Valid = false; //reset validation;
Total += Input;
Average = (Total/y);
}
System.out.println("The average score for student " + (z + 1) + " is " + Average);
System.out.println(" ");
Total= 0;
}
keyboard.close();
}
}
An easy way to go about this would be to put the user-input prompt inside of a while loop, and only break out once you've verified that the grade is valid:
Scanner scanner = new Scanner(System.in);
int score;
while (true) {
System.out.print("Please enter score " + (g + 1) + ": ");
score = scanner.nextInt();
if (score >= 0 && score <= 100) {
break;
}
System.out.println("Please enter a valid score between 0 and 100!");
}
Total += score;
Remember to close your Scanners to avoid memory leaks!

Portion of code not running

Part of my java code is not running. I am fairly new to java and have been working out some new environment changes. My class was told to build a windchill temperature calculator. My main issue is that my code works up to the for (ws = wsp; ws <= c; ws += 0.5) then stops.
import java.util.*;
class Assign1
{
public static void main(String args[])
{
Menu user = new Menu();
Menu.mainmenu();
user.acceptSelection();
}
}
class Menu
{
public static void mainmenu()
{
System.out.println("Temperature Analysis MENU");
System.out.println("1.W)ind Chill Temperature");
System.out.println("0.E)xit");
System.out.println("Enter Selection:");
}
public void acceptSelection()
{
Scanner stdin = new Scanner(System.in);
String selection = stdin.nextLine();
char choice = selection.charAt(0);
switch(choice)
{
case 'W':
case 'w':
case '1':
processing.process(); break;
case 'E':
case 'e':
case '0':
System.out.println("E"); break;
}
}
}
class processing
{
public static void process()
{
Scanner stdin = new Scanner(System.in);
System.out.println("\n\n\n\n\n\n\n");
System.out.print("Please enter START air temp in celsius (decimal) MUST be BELOW 9: ");
double sa = stdin.nextDouble();
System.out.print("Please enter END air temp in celsius (decimal) MUST be BELOW 9: ");
double ea = stdin.nextDouble();
System.out.print("Please enter wind speed (decimal) FROM 8km/h to: ");
double w = stdin.nextDouble();
System.out.println("\n==================================================================\n");
calculation(sa, ea, w);
}
public static void calculation(double a, double b, double c)
{
double wsp = 8.0;
double airTemp;
double ws;
int size = 150;
double[] wChill = new double[size];
int count = 0;
System.out.print(" " + a);
while(a <= b)
{
System.out.print(" " + a);
a +=5;
count++;
}
System.out.print(" " + b);
int count2 = 0;
while(wsp <= c)
{
count2++;
wsp += 0.5;
}
double[][] chart = new double[count2][count];
int i = 0, j = 0, k = 0;
This is where it stops working. I cannot get it to print my loop out. Any help in fixing my problem would be appreciated as well as notes to my code as i am trying to improve. I am using JGrasp if it helps.
for (ws = wsp; ws <= c; ws += 0.5)
{
System.out.println(ws + " ");
for (airTemp = a; airTemp <= b; airTemp += 5.0)
{
if ((ws + 0.5) > c)
{
System.out.printf( "%2d %2d", c , chart[k][i]);
}
else
{
wChill[i] = (13.12 + (0.6215*airTemp)+(-11.37*Math.pow(ws, 0.16))+(0.3965*airTemp*Math.pow(ws, 0.16)));
chart[k][i] = wChill[i];
System.out.print(chart[k][i] + " ");
}
i++;
}
k++;
}
}
}
According to you code you have a while loop
while(wsp <= c) {...}
then you have a for loop
for (ws = wsp; ws <= c; ws += 0.5)
so as you can see ws is assigned the value of wsp which has in the while already exceeded the value of c

Scanner only reads for loop once? - Java

I'm trying to make a class where you input any number of grades (A-F) and calculates the GPA and returns the GPA and eligibility to extracurricular activities. It seems like the scanner only allows one input, then prints the GPA and eligibility.
So far this is what I have:
import java.util.Scanner;
public class Grades
{
public static void main(String[] args)
{
double myGPA;
int myNumClasses;
double myValue;
Scanner sc = new Scanner(System.in);
System.out.println("Press any other lettter to calculate.");
System.out.print("Enter grades: ");
String input = sc.nextLine();
myValue = 0;
myNumClasses = 0;
myGPA = 0;
for (String next = sc.next(); input.equalsIgnoreCase("a") || input.equalsIgnoreCase("b") ||
input.equalsIgnoreCase("c")|| input.equalsIgnoreCase("d") || input.equalsIgnoreCase("f"); next = sc.next())
{
if (input.equalsIgnoreCase("a"))
{
myValue += 4.0;
myNumClasses += 1;
}
else if (input.equalsIgnoreCase("b"))
{
myValue += 3.0;
myNumClasses += 1;
}
else if (input.equalsIgnoreCase("c"))
{
myValue += 2.0;
myNumClasses += 1;
}
else if (input.equalsIgnoreCase("d"))
{
myValue += 1.0;
myNumClasses += 1;
}
else if (input.equalsIgnoreCase("f"))
{
myNumClasses += 1;
}
myGPA = myValue / myNumClasses;
if (myGPA >= 2.0 && myNumClasses >= 4)
{
System.out.println("Eligible");
}
else if (myNumClasses < 4)
{
System.out.println("Ineligible, taking less than 4 classes");
}
else if (myGPA >= 2.0 && input.equalsIgnoreCase("f"))
{
System.out.println("Ineligible, gpa above 2.0 but has F grade");
}
else if (myGPA <= 2.0 && input.equalsIgnoreCase("f"))
{
System.out.println("Ineligible, gpa below 2.0 and has F grade");
}
else if (myGPA < 2.0)
{
System.out.println("Inelligible, gpa below 2.0");
}
System.out.println("Your GPA = " + myGPA);
}
}
}
// It looks like your missing a for loop. I just copied some of your
//code and ran it through a for loop. The rest of the code is kind of unclear.
System.out.println("Enter the number of grades you will enter: ");
int userAns = sc.nextInt();
for (int index = 0; index <= userAns; index++)
{
System.out.println("Press any other letter to calculate.");
System.out.print("Enter grades: ");
String input = sc.nextLine();
}

Categories

Resources