有人可以帮忙吗?我可以让它运行,但在输出消息“什么是外部温度:”“数字在这里”
之后摄氏度的类型(C)或华氏的(F):
输入C或F后,它会显示此错误信息
线程“main”中的异常java.util.InputMismatchException
at java.util.Scanner.throwFor(Scanner.java:909)
at java.util.Scanner.next(Scanner.java:1530)
at java.util.Scanner.nextInt(Scanner.java:2160)
at java.util.Scanner.nextInt(Scanner.java:2119)
at Tem.main(Tem.java:13)
请帮助
import java.util.Scanner;
public class Tem
{
public static void main(String [] args)
{
Scanner scan = new Scanner(System.in);
String unit = "";
System.out.print("What is the outside Temperature: ");
double temp = scan.nextDouble();
System.out.println("Type (C) for Celsius or (F) for Fahrenheit: ");
int input = scan.nextInt();
if (input == 'F')
{
temp = (temp - 32) * 5/9.0;
unit = "Celsius.";
}
else if (input == 'C')
{
temp = (temp * 9/5.0) +32;
unit = "Fahrenheit.";
}
System.out.println("It is "+temp+" degrees "+unit);
}
}
答案 0 :(得分:2)
好吧,"C"
和"F"
无效int
,因此这是InputMismatchException
的来源。但是你想要一封信,所以你应该拨打next()
而不是nextInt()
来获取下一个令牌。
next()
method将返回String
,您可以看到equals
"C"
"F"
的{{1}}。