第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

嘗試使用 Integer.parseInt() 將字符串轉(zhuǎn)換為整數(shù)時出錯

嘗試使用 Integer.parseInt() 將字符串轉(zhuǎn)換為整數(shù)時出錯

幕布斯7119047 2023-11-10 17:07:15
***更新我的帖子以顯示我的所有代碼,希望這提供更多上下文。當我運行該程序時,它會跳過我的第一個“if”語句并拋出錯誤。***我收到錯誤Exception in thread "main" java.lang.NumberFormatException: For input string: ""我正在嘗試在 while 循環(huán)的第一個 else 語句中將字符串變量轉(zhuǎn)換為 int...錯誤發(fā)生在int examScore = Integer.parseInt(userInput).   import java.util.Scanner;//imports scanner   public class ExamScoresCalculator{   public static void main(String[] args){   Scanner scan = new Scanner(System.in);//creates new scanner object    //prompts user for lowest possible exam score   System.out.println("Enter the lowest possible value for the exam: ");   int lowRange = scan.nextInt();   //prompts user for highest possible exam score   System.out.println("Enter the highest possible value for the exam: ");   int highRange = scan.nextInt();   boolean flag = true; //while loop flag   double lowestExamScore = highRange; // holds lowest score   double highestExamScore = lowRange; //holds highest score   double totalPoints = 0; //holds sum of all scores entered   int totalExams = 0; //holds the total number of exams entered by the user   while (flag == true){      System.out.println("Enter an exam score between " + lowRange + " and " + highRange + " or type exit. "); //asks user for an exam score within correct range           String userInput = scan.nextLine();      if ((userInput.equals("exit")) || (userInput.equals("Exit")) || (userInput.equals("EXIT"))){ //checks if user enters "exit"         flag = false; //ends while loop      }
查看完整描述

3 回答

?
慕婉清6462132

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());


查看完整回答
反對 回復 2023-11-10
?
慕慕森

TA貢獻1856條經(jīng)驗 獲得超17個贊

請使用userInput和 的兩個輸入examScore,這將解決您的問題,因為您不能對StringInteger數(shù)據(jù)類型使用單個值。

(但當您的輸入值為“123”(僅限數(shù)字)時,這是可能的,即使這也不適用于您的程序)


查看完整回答
反對 回復 2023-11-10
?
忽然笑

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 


查看完整回答
反對 回復 2023-11-10
  • 3 回答
  • 0 關(guān)注
  • 239 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號