Java代码编译报错:符号未识别,请求技术排查(附代码)
解决Java编译错误:无法识别符号的问题
嘿,我看到你的代码里有两个关键问题导致了“无法识别符号”的编译错误,咱们一步步来解决:
问题1:缺少Scanner类的导入
Scanner类属于java.util包,Java不会自动导入这个包下的类,所以你需要在代码最顶部添加导入语句,告诉编译器你要使用这个类:
import java.util.Scanner;
问题2:Scanner对象创建时的大小写错误
Java是严格区分大小写的,类名的首字母必须大写。你代码里写的是new scanner (System.in),这里的小写s会让编译器找不到对应的类,应该改成大写的Scanner:
Scanner in = new Scanner(System.in);
修正后的完整代码
import java.util.Scanner; class Main { public static double calcAvg(double q1, double q2, double q3) { return (q1 + q2 + q3) / 3.0; } public static int ageInYears(int months) { return months / 12; } public static double convertCtoF(double C) { return (C * 9.0 / 5.0) + 32.0; } public static void main(String[] args) { Scanner in = new Scanner(System.in); System.out.print("Enter employee ID: "); String empID = in.nextLine(); System.out.print("Enter quiz 1 score: "); double quiz1 = in.nextDouble(); in.nextLine(); System.out.print("Enter quiz 2 score: "); double quiz2 = in.nextDouble(); in.nextLine(); System.out.print("Enter quiz 3 score: "); double quiz3 = in.nextDouble(); in.nextLine(); System.out.print("Enter age in months: "); int age = in.nextInt(); in.nextLine(); System.out.print("Enter temp in Celsius: "); double celsius = in.nextDouble(); in.nextLine(); System.out.printf("Employee ID: %s\n", empID); System.out.printf("Avg quiz score: %f\n", calcAvg(quiz1, quiz2, quiz3)); System.out.printf("Age in years: %d\n", ageInYears(age)); System.out.printf("Temp in Fahrenheit: %f\n", convertCtoF(celsius)); } }
修正这两个地方之后,你的代码应该就能正常编译运行了。
内容的提问来源于stack exchange,提问作者Bria Wingate
相关产品推荐
相关产品推荐

