所以我是初学者使用java,我试图让用户输入华氏温度并将其转换为摄氏温度,同时确保使用输入双变量而不是字母的有效数字。而不是显示在用户身上。我特别遇到循环问题。这是我的代码。
import java.util.Scanner;
public class TempConverter
{
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
double temp;
do
{
System.out.print("Please enter temperature in Fahrenheit: ");
while(!in.hasNextDouble())
{
System.out.println("ERROR. Please enter a valid temperature: ");
in.nextDouble();
}
temp = in.nextDouble();
}
double finalTemp = ((temp - 32)*5)/9;
System.out.print("Celsius value of Fahrenheit value " + temp + " is " + finalTemp + ".");
}
}
答案 0 :(得分:2)
一定要来了。它将继续迭代,直到满足while
中的条件。
do
{
System.out.print("Please enter temperature in Fahrenheit: ");
System.out.println("ERROR. Please enter a valid temperature: ");
in.nextDouble();
temp = in.nextDouble();
}
while(!in.hasNextDouble())