在将摄氏温度转换为华氏度时找不到符号错误Java
问题描述:
我对这些东西都很陌生,我试图将Celsius
转换为jGRASP Java
中的Fahrenheit
。我使用的代码附加在图片中,错误也可以在其他图片中看到。在将摄氏温度转换为华氏度时找不到符号错误Java
错误消息
答
有消息称一切。你还没有声明F
,因此编译器找不到符号。使用它像
int F = 0;
编辑之前声明它:你可能是指与字符串文字"F"
比较input
。您必须声明input
为string
,读string
变量到它,然后使用if
条款喜欢
if (input == "F") {//...
答
if (input == F)
在您提供的代码,你永远不声明F.
通过评审您想要查看的用户是否输入了“F”的代码,但您可以如此分配输入变量:
int input = scan.nextInt();
这将是更好的做这样的事情:
String input = scan.nextLine();
if(input.equals("F")){
// rest of code
答
与您的代码的问题是你告诉扫描器来读取一个int数据和你期待一个文本或字符。使用scanner.next()将返回空格之前的字符串。然后你可以检查它的价值。这是一个例子。
public static void main(String args[]) {
Scanner scanner = new Scanner(System.in);
String tempScale = "";
System.out.print("Enter the current outside temperature: ");
double temps = scanner.nextDouble();
System.out.println("Celsius or Farenheit (C or F): ");
String input = scanner.next();
if ("F".equalsIgnoreCase(input)) {
temps = (temps-32) * 5/9.0;
tempScale = "Celsius.";
} else if ("C".equalsIgnoreCase(input)) {
temps = (temps * 9/5.0) + 32;
tempScale = "Farenheit.";
}
System.out.println("The answer = " + temps + " degrees " + tempScale);
scanner.close();
}
和一个例证:
[为什么可能我不SO问问题时上传的代码图像](http://meta.*.com/questions/285551/为什么我可能不会上传图像的代码的时候提出问题) –
请阅读http://*.com/help/how-to-ask并改革你的问题根据它。 –
请发表相关的代码,不要将其作为图片添加 – DarkBee