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

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

使用 ArrayList 和 java 進(jìn)行異常處理

使用 ArrayList 和 java 進(jìn)行異常處理

有只小跳蛙 2021-09-29 17:45:27
幾個(gè)月前我開始研究 Java,現(xiàn)在我陷入了異常處理中,我需要的是,每個(gè)StudentId、TeamID 都必須拋出一輪異常檢查,以便如果用戶輸入字母而不是整數(shù),它應(yīng)該發(fā)送回在同一個(gè)地方再次詢問那個(gè)值。我正在使用 ArrayList 來存儲(chǔ)數(shù)據(jù)。此外,所有g(shù)et************() 方法都在另一個(gè)名為Student 的類中。studentList是我訪問我的ArrayList 的變量請(qǐng)通過修復(fù)它來幫助我找到我的錯(cuò)誤在哪里,以便我的概念更加清晰。我被困在這里/*** * A method to check if the same Student ID exists in the ArrayList previously * @param StudentId * @return */public static boolean checkStudent(int StudentId) {    for (Student stud : studentList) {        if(stud.getStudentId() == StudentId) {            return true;        }    }    return false;}/*** * Method to add student in the ArrayList * @param scn */public static void addStudent(Scanner scn) {    System.out.println("Enter student ID");    int studentId;    studentId = scn.nextInt();    **//enter the try catch statement here     //ask the user to write the integer not string and revert to this scanner variable**    if (checkStudent(studentId)) {        System.out.println("Student Id " + studentId + " already exists,Please enter the valid details ");        addStudent(scn);        //break;   }else{       System.out.println("Enter student name:");       String studentName;       studentName = scn.next();       System.out.println("Enter team ID:");        **try{        int teamId;       teamId = scn.nextInt();        //ask the user to write the integer not string and revert to this scanner variable       }catch(NumberFormatException e){           //       }finally{        //do something            }**        System.out.println("Enter team name:");       String teamName;try{       teamName = scn.next();           }       Student std = new Student(studentId, studentName, teamId, teamName);       studentList.add(std);       System.out.println("*******************************************");       System.out.println("student with Student ID" + studentId +" added succesfully");       System.out.println("*******************************************");      }}  
查看完整描述

2 回答

?
白豬掌柜的

TA貢獻(xiàn)1893條經(jīng)驗(yàn) 獲得超10個(gè)贊

我將通過使用 while 循環(huán)給您另一個(gè)提示,您可以根據(jù)您的要求進(jìn)一步實(shí)現(xiàn)此邏輯。您可以在致電之前使用它c(diǎn)heckStudent()


System.out.println("Enter student ID");

while (true) {

    try {

        int studId= scn.nextInt();

        // terminate loop if everything is ok  

        break;

    } catch (NumberFormatException e) {

        // on an exception, loop still continues

    }

}



查看完整回答
反對(duì) 回復(fù) 2021-09-29
?
呼如林

TA貢獻(xiàn)1798條經(jīng)驗(yàn) 獲得超3個(gè)贊

請(qǐng)閱讀try..catchJava 語言規(guī)范中聲明或任何教程(您可以通過在您喜歡的搜索引擎中鍵入“java try catch”來找到許多教程)。

我相信你能弄清楚。


查看完整回答
反對(duì) 回復(fù) 2021-09-29
  • 2 回答
  • 0 關(guān)注
  • 213 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

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