I need to modify my program so that I can run it more than once if need be. I need to quit the program if the user enters a Q or q and if anything other than the requested entry (or the quit command) is entered the question will be repeated. Here is the code I have so far:
import java.util.Scanner;
public class TemperatureLoop
{
private static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args)
{
System.out.println("Enter a temperature in degrees (for example 32.6): ");
double temp;
temp = keyboard.nextDouble();
System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
String letter = keyboard.next();
double total = 0;
//if Farenheit then do this equation
if (letter.equals("F") || (letter.equals("f")))
{
total = ((temp-32)*5)/9; //convert the entered temperature to Celsius
System.out.println(temp + " degrees F = " + total + " degrees Celsius");
}
else //if Celsius then do this
if (letter.equals("C") || (letter.equals("c")) )
{
total = (((temp*9))/5)+32; //convert the entered temperature to Farenheit
System.out.println(temp + " degrees C = " + total + " degrees Fahrenheit");
}
}
}
I would suggest putting what you have into a while loop that breaks out if the user enters 'Q' or 'q'. Something similar to below:
// Declare your breaking condition variable outside the while loop
boolean done = false;
while (!done){
// Your existing code here
// A conditional to check for 'Q' or 'q'
// set done to true if the above line evaluates as true.
}
You should use a do-while
loop in this case,
String letter = "";
do{
System.out.println("Enter a temperature in degrees (for example 32.6): ");
double temp = 0;
while(true){
if(keyboard.hasNextDouble())
{
temp = keyboard.nextDouble();
break;
}
else
{
System.out.println("Enter a valid double");
sc.nextLine();
}
}
System.out.println("Enter 'F' (or 'f') for Fahrenheit or 'C' (or 'c') for Celsius: ");
letter = keyboard.next();
double total = 0;
//if Farenheit then do this equation
if (letter.equalsIgnoreCase("F"))
{
total = ((temp-32)*5)/9; //convert the entered temperature to Celsius
System.out.println(temp + " degrees F = " + total + " degrees Celsius");
}
else if (letter.equalsIgnoreCase("C"))
{ //if Celsius then do this
total = (((temp*9))/5)+32; //convert the entered temperature to Farenheit
System.out.println(temp + " degrees C = " + total + " degrees Fahrenheit");
}
}while(!letter.equalsIgnoreCase("Q"));
How the loop works is, whatever is in the do
part will always execute at least once . Then it will check the while
condition to determine whether or not to execute the do
part again. Like you said, once the user enters Q
or q
, the program will end because the while condition will evaluate to false and the do
part will no longer be executed. Therefore, the loop will terminate in that case.
What exactly happens when you enter Q
or q
? The do
part will technically happen, but your if-statements will be ignored since it doesn't satisfy those conditions. Once the while
check is reached, the condition will evaluate to false, causing the loop to end. If you entered something like M
or g
, then the if-statements will be ignored but the loop won't end because the while
condition won't evaluate to false so the program will ask you once again for a temperature and degrees.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.