3 回答

TA貢獻1804條經(jīng)驗 獲得超2個贊
一旦您按ENTER輸入最高分數(shù),掃描儀就會讀取整數(shù)(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());

TA貢獻1856條經(jīng)驗 獲得超17個贊
請使用userInput
和 的兩個輸入examScore
,這將解決您的問題,因為您不能對String
和Integer
數(shù)據(jù)類型使用單個值。
(但當您的輸入值為“123”(僅限數(shù)字)時,這是可能的,即使這也不適用于您的程序)

TA貢獻1806條經(jīng)驗 獲得超5個贊
添加 scan.nextLine() 來移動掃描儀以讀取下一行(新行)。
System.out.println("Enter the highest possible value for the exam: ");
int highRange = scan.nextInt();
scan.nextLine(); // Add this line
添加回答
舉報