在我的代码中需要一些注意事项。我已经连续观看了8个小时,无法弄清楚。我需要用户输入介于-459和212之间。如果它是超出范围的整数,它将打印行以重新输入它,然后继续进行转换。现在,它既不执行任何操作,也不执行代码。我应该使用布尔值还是if时间?不知道。
第二个问题是转换后打印出华氏温度和摄氏温度时,我需要它们列出另外20行,并在每个温度上增加10行。这算吗?请提供主要帮助!
到目前为止,我有:
import java.text.DecimalFormat;
import java.util.Scanner;
public class Temperatures {
public static void main(String[] args) {
double Fahrenheit;
double Celsius;
Scanner scan = new Scanner(System.in);
System.out.print("Enter temperature in Fahrenheit: ");
Fahrenheit = scan.nextInt();
if(Fahrenheit >= -459 && Fahrenheit <= 212)
System.out.println("Please enter a number between -459 and 212 degrees:");
Fahrenheit = scan.nextInt();
Celsius = ((Fahrenheit -32)*5)/9;
DecimalFormat fmt = new DecimalFormat("#.##");
System.out.print("Fahrenheit: " + Fahrenheit + " Celsius: " + fmt.format(Celsius));
}
}
最佳答案
您的if
条件是相反的。并且下一个nextInt()
不在if
块中。如果要重复此操作直到获得有效输入,则需要将其转换为while
循环:
while(Fahrenheit < -459 || Fahrenheit > 212) {
System.out.println("Please enter a number between -459 and 212 degrees:");
Fahrenheit = scan.nextInt();
}
我不太明白第二个问题。
关于java - Java用户输入必须在范围内/20行产生10的整数,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/41882540/