重复温度编程项目添加一个循环,允许用户继续输入温度,直到他们输入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'移动到下一次迭代。 –