2错误:表达式的非法开始[Java]
我还没有完成这个,但如果我已经有错误没有进展。这里是我的代码..2错误:表达式的非法开始[Java]
public static void main(String[] args)
{
Scanner inputDevice = new Scanner(System.in);
System.out.print("Please enter the name of student ==> ");
studentName = inputDevice.nextInt();
inputDevice.nextLine();
System.out.printIn("Enter the mark for student "+ studentName " out of 65 ==> ");
studentMark = inputDevice.nextInt();
inputDevice.nextLine();
}
1号错误: ')' 预期
System.out.printIn("Enter the mark for student "+ studentName " out of 65 ==> ");
第二个错误:表达非法启动
System.out.printIn("Enter the mark for student "+ studentName " out of 65 ==> ");
我非常新的哈哈。我不明白为什么我的第二打印是给我一个错误
基本上,几乎所有是错在这里。
问题#1 - 3号线
studentName = inputDevice.nextInt();
你没有正确申报studentName
。当你想要一个名字(String
)时,你也试图获得int
。相反,该行应为:
String studentName = inputDevice.nextLine();
问题#2 - 4行
inputDevice.nextLine();
这是什么线在这里做什么?你没有把输入分配给任何东西。只需完全删除此行。
问题3 - 5行
System.out.printIn("Enter the mark for student "+ studentName " out of 65 ==> ");
你缺少一个+
。此行应为:
System.out.printIn("Enter the mark for student "+ studentName +" out of 65 ==> ");
问题#4 - 6号线
studentMark = inputDevice.nextInt();
同样,你没有正确声明变量。它应该是:
int studentMark = inputDevice.nextInt();
问题#5 - 7行
inputDevice.nextLine();
就像第2期,此行实现了什么。去掉它。
摘要
您的代码应该(可能)这样写的,而不是:
Scanner inputDevice = new Scanner(System.in);
System.out.print("Please enter the name of student ==> ");
String studentName = inputDevice.nextLine();
System.out.println("Enter the mark for student "+ studentName + " out of 65 ==> ");
int studentMark = inputDevice.nextInt();
非常感谢您的帮助! – pnkrtn
替换此
System.out.printIn("Enter the mark for student "+ studentName " out of 65 ==> ");
与
System.out.printIn("Enter the mark for student "+ studentName +" out of 65 ==> ");
他们的代码有很多错误。 – domsson
我想,你应该你不声明变量。 扫描仪输入=新扫描仪(System.in);
String studentName = inputDevice.nextLine(); int studentMark = inputDevice.nextInt();
'studentName'应该是'字符串studentName','studentMark'应该是'INT studentMark',不是吗?此外,您永远不会将'nextLine()'的返回值赋值给任何东西... – domsson
哇,7行中有5行有错误或不需要。我给你写了一份所有问题的清单,以便你能够理解和解决它们,更重要的是,在未来阻止这些问题。然而,下一次请给自己一杯咖啡,仔细阅读你的代码(以及错误信息),以便在这里询问之前自己发现一些这些问题。 – domsson