3 回答
![?](http://img1.sycdn.imooc.com/5458478b0001f01502200220-100-100.jpg)
TA贡献1804条经验 获得超2个赞
一旦您按ENTER输入最高分数,扫描仪就会读取整数(nextInt),但也会读取行尾(nextLine)。由于该行中没有提供文本,因此它被读取为空字符串“”。
您应该在代码中将scanner.nextInt替换为scanner.nextLine。它应该可以在以下更改中正常工作。
//prompts user for lowest possible exam score
System.out.println("Enter the lowest possible value for the exam: ");
int lowRange = Integer.parseInt(scan.nextLine());
//prompts user for highest possible exam score
System.out.println("Enter the highest possible value for the exam: ");
int highRange = Integer.parseInt(scan.nextLine());
![?](http://img1.sycdn.imooc.com/5458655200013d9802200220-100-100.jpg)
TA贡献1856条经验 获得超17个赞
请使用userInput
和 的两个输入examScore
,这将解决您的问题,因为您不能对String
和Integer
数据类型使用单个值。
(但当您的输入值为“123”(仅限数字)时,这是可能的,即使这也不适用于您的程序)
![?](http://img1.sycdn.imooc.com/54584dad0001dd7802200220-100-100.jpg)
TA贡献1806条经验 获得超5个赞
添加 scan.nextLine() 来移动扫描仪以读取下一行(新行)。
System.out.println("Enter the highest possible value for the exam: ");
int highRange = scan.nextInt();
scan.nextLine(); // Add this line
添加回答
举报