一旦我運行代碼來讀取數據,程序執行就不會循環回來。我嘗試改變放置 try catch 語句和finally 語句的位置,并且所有中斷方式都會繼續。 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");盡管程序執行只是停止循環,但沒有錯誤消息。
1 回答

慕運維8079593
TA貢獻1876條經驗 獲得超5個贊
通過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');
添加回答
舉報
0/150
提交
取消