簡體   English   中英

如何添加try和catch驗證以及如何修復我的開關,do-while錯誤Java

[英]how to add try and catch validation and how to fix my switch, do-while error java

我無法在此處發布代碼不確定為什么會出錯,所以我在reddit上發布了代碼

https://www.reddit.com/r/javahelp/comments/3izvy2/how_to_add_try_and_catch_validation_and_how_to/

您的try / catch語法很好,這是什么問題?

但是,您的do / while錯誤,我看到while一直都在那兒,所以從do上刪除最后一個花括號,然后將其放在while之前:

do{     
 try{ 

  System.out.println("Choose your option");

  System.out.println("  1. Create List of used car");
  System.out.println("  2. Print list of used car");
  System.out.println("  3. Search ");
  System.out.println("  4. Exit");

  int choose = read.nextInt();

      if ( choose <= 4){
           throw new Exception();
    }

  }catch (Exception e){
  System.out.println("Please enter input option from 1-4");

..rest of code...
}while(choose <= 4);

並在開關, choose是暫無數據- choose是只對try / catch語句,無非就是try / catch語句其他可以訪問它的局部變量。 最好將全局變量放在頂部,以choose全局變量:

do{
    int choose;
    ..blah blah..
}

並在沒有int的情況下引用它-只需choose

choose = read.nextInt();

最后,在每種case都需要break一下, return s有什么意義?

case 1:
  for(int i=0; i<a.length; i++){

System.out.print("Enter Model: ");
Model = read.next();
System.out.print("Enter Plate Number: ");
PlateNo = read.next();
System.out.print("Enter Manufactured Year: ");
ManufacturedYear = read.nextInt();
System.out.print("Enter Transmission Type: ");   
TransmissionType = read.next();
System.out.print("Enter Color: "); 
Color = read.next();

CarList used = new CarList(Model, PlateNo, ManufacturedYear,
       TransmissionType, Color);
a[i] = used;
        return;
}
break;

...keep on going....

您不應該依賴他人來解決簡單的語法問題,遇到錯誤時不要驚慌,只需遍歷代碼並學習如何調試消息即可!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM