I am relatively new to Java, I have some troubles with running this do-while loop.
Problem 1
Write a program that allows the user to convert a temperature given in degrees from either Celsius to Fahrenheit or Fahrenheit to Celsius. Use the following formulas: Degrees_C = 5 (Degrees_F − 32) / 9 Degrees_F = (9 (Degrees_C) / 5) + 32 Prompt the user to enter a temperature and either a C or c for Celsius or an F or f for Fahrenheit. Convert the temperature to Fahrenheit if Celsius is entered, or to Celsius if Fahrenheit is entered. Display the result in a readable format. If anything other than C, c, F, or f is entered, print an error message and stop.
Problem 2
Let’s continue Problem 1, but use a loop so the user can convert other temperatures. If the user enters a letter other than C or F—in either uppercase or lowercase—after a temperature, print an error message and ask the user to reenter a valid selection. After each conversion, ask the user to type Q or q to quit or to press any other key to repeat the loop and perform another conversion
public class Lab_4 {
public static void main(String arg[]) {
Scanner input = new Scanner(System.in);
double F; //Fahrenheit
double C; //Celsius
String method;
boolean keepGoing = true;
do {
System.out.println("Choose a method: ");
System.out.println("(F) Fahrenheit to Celsius. ");
System.out.println("(C) Celsius to Fahrenheit. ");
System.out.println("(Q) Exit the loop. ");
method = input.nextLine();
if (method.charAt(0) == 'f' || method.charAt(0) == 'F') {
System.out.println("Method F");
System.out.println("Enter the temperature in Fahrenheit: ");
F = input.nextDouble();
C = 5 * (F - 32) / 9;
System.out.println("Temperature in Celsius: " + C); }
if (method.charAt(0) == 'c' || method.charAt(0) == 'C') {
System.out.println("Method C");
System.out.println("Enter the temperature in Celsius: ");
C = input.nextDouble();
F = (9 * C / 5) + 32;
System.out.println("Temperature in Fahrenheit: " + F); }
if (method.charAt(0)== 'q' || method.charAt(0)== 'Q') {
keepGoing = false;
System.out.println("Exiting the loop! "); }
else {
//if index 0 doesn't equal to C, F, Q, it's out of range.
System.out.println("Method is out of range! ");
}
}while(keepGoing = true);
input.close();
}
}
The loop will keep going until I enter Q or q to exit out. So I have to enter Q in order to exit the loop, but instead I got an error message right after I got the conversion value, it simply doesn't run through the loop. The program just doesn't go through the while loop.
Try 2, still errors
package Lab_4;
import java.util.Scanner;
public class Lab_4 {
public static void main(String arg[]) {
Scanner input = new Scanner(System.in);
double F; //Fahrenheit
double C; //Celsius
String method;
boolean keepGoing = true;
do {
System.out.println("Choose a method: ");
System.out.println("(F) Fahrenheit to Celsius. ");
System.out.println("(C) Celsius to Fahrenheit. ");
System.out.println("(Q) Exit the loop. ");
method = input.nextLine();
if (method.charAt(0) == 'f' || method.charAt(0) == 'F') {
System.out.println("Method F");
System.out.println("Enter the temperature in Fahrenheit: ");
F = input.nextDouble();
C = 5 * (F - 32) / 9;
System.out.println("Temperature in Celsius: " + C); }
else if (method.charAt(0) == 'c' || method.charAt(0) == 'C') {
System.out.println("Method C");
System.out.println("Enter the temperature in Celsius: ");
C = input.nextDouble();
F = (9 * C / 5) + 32;
System.out.println("Temperature in Fahrenheit: " + F); }
else if (method.charAt(0)== 'q' || method.charAt(0)== 'Q') {
keepGoing = false;
System.out.println("Exiting the loop! "); }
else {
//if index 0 doesn't equal to C, F, Q, it's out of range.
System.out.println("Method is out of range! ");
}
}while(keepGoing);
input.close();
}
}