重複溫度編程項目添加一個循環,允許用戶繼續輸入溫度,直到他們輸入Q或 q退出程序的該部分。用戶應該輸入Q或q 退出並返回一個空字符串。 (任何其他條目應導致 問題重複)如果用戶輸入不同的字母 表示溫度爲F或C(大寫或小寫),則打印 錯誤消息並要求用戶輸入正確的溫度不需要詢問新的數值就可以縮放 。溫度不正確的用戶輸入
這是我到目前爲止....我無法得知它是否輸入65D而不是65C。我無法弄清楚如何循環回通過代碼,如果他們進入d,而不是F/F或C/C ...
import java.util.Scanner;
public class AssignmentFour {
public static Scanner keyboard = new Scanner(System.in);
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("Hello, I can convert Fahrenheit to Celsius!");
System.out.println("Please enter the temperature you want converted.");
System.out.println("Followed by: 'C/c' or 'F/f'");
String input2 = keyboard.next();
do{
String temp = input2.trim();
String degreesAsString = temp.substring(0, temp.length()-1);
double degrees = Double.parseDouble(degreesAsString);
if(temp.endsWith("C") || temp.endsWith("c")){
double degreeF = 9 * degrees/5 + 32;
System.out.println(input2 + " is equal to: ");
System.out.printf("%.2f", degreeF);
System.out.println(" Fahrenheit.");
} else if(temp.endsWith("F") || temp.endsWith("f")){
double degreeC = 5 * (degrees - 32)/9;
System.out.println(input2 +" is equal to: ");
System.out.printf("%.2f", degreeC);
System.out.println(" Celsius.");
} else if (temp.endsWith(""));{
System.out.println("ERROR: Please enter either 'F/f or C/c'.");
}
break;
} while(!input2.equals(""));
System.out.println("");
}
您最後的'else if'塊應該是'else'。不需要'if'語句。 –
不起作用。它的左手邊必須是一個變量... –
順便說一句,您正在使用'break'錯誤。 'break'退出一個循環,而'continue'移動到下一次迭代。 –