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

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

如何在 switch 中使用 try catch 一會(huì)兒

如何在 switch 中使用 try catch 一會(huì)兒

當(dāng)年話下 2023-07-19 15:52:52
一旦我運(yùn)行代碼來讀取數(shù)據(jù),程序執(zhí)行就不會(huì)循環(huán)回來。我嘗試改變放置 try catch 語句和finally 語句的位置,并且所有中斷方式都會(huì)繼續(xù)。    long code;    char choice;    Cars CarSales = new Cars(); //It creates a Java object and allocates memory for it on the heap.    Scanner sc = new Scanner(System.in);    System.out.println("   -----CARS SALES YARD------"); //The println is a method of java.io.PrintStream.    do {        System.out.println("1. Add item");        choice = sc.nextLine().charAt(0);        switch (choice) {     //switch statement allows a variable to be tested for equality against a list of values.         case '6':            try{            CarSales.ReadData();            continue;            }            catch(IOException e){                     System.out.println("Error reading file '" );                     continue;            }        default:            System.out.println("Invalid Selection\n");        }    } while (choice != '6'); //while loop statement repeatedly executes a statement as long as a given condition is true    sc.close();public void ReadData() throws IOException{//This Method is in the Cars classString fileName = "input.txt";String line = null;FileReader fileReader = new FileReader(fileName);BufferedReader bufferedReader = new BufferedReader(fileReader);while((line = bufferedReader.readLine()) != null) {            System.out.println(line);    }   bufferedReader.close();   System.out.println("TRY");盡管程序執(zhí)行只是停止循環(huán),但沒有錯(cuò)誤消息。
查看完整描述

1 回答

?
慕運(yùn)維8079593

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

通過continue;將移至后catch。喜歡,


do {

    System.out.println("1. Add item"); //<-- where are 2-6?

    choice = sc.nextLine().charAt(0);

    switch (choice) {

    case '6': // <-- don't forget case '1' - '5'

        try {

            CarSales.ReadData();

        } catch (IOException e) {

            System.out.println("Error reading file '");

        }

        continue; // <-- here, or a break;

    default:

        System.out.println("Invalid Selection\n");

    }

} while (choice != '6');


查看完整回答
反對(duì) 回復(fù) 2023-07-19
  • 1 回答
  • 0 關(guān)注
  • 190 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號(hào)

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