Java 将摄氏度转换为华氏度时找不到符号错误
我对所有这些东西都很陌生,我正试图在Java 将摄氏度转换为华氏度时找不到符号错误,java,jgrasp,Java,Jgrasp,我对所有这些东西都很陌生,我正试图在jgraspjava中将摄氏度转换为华氏度。我使用的代码附在图片中,错误也可以在另一张图片中看到 错误消息 这条信息说明了一切。您尚未声明F,因此编译器无法找到符号。在使用它之前声明它,就像 int F = 0; 编辑:您可能想将输入与字符串literal“F”进行比较。您必须将input声明为string,将string变量读入其中,然后使用if子句,如 if (input == "F") {//... 在您提供的代码中,您从不声明F 根据您想要查看用
jgraspjava
中将摄氏度
转换为华氏度
。我使用的代码附在图片中,错误也可以在另一张图片中看到
错误消息
这条信息说明了一切。您尚未声明
F
,因此编译器无法找到符号。在使用它之前声明它,就像
int F = 0;
编辑:您可能想将输入
与字符串literal“F”
进行比较。您必须将input
声明为string
,将string
变量读入其中,然后使用if
子句,如
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();
}
还有一个例子:
请阅读并根据问题进行修改。请张贴相关代码,不要将其作为图片附上
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();
}