如何添加 try 和 catch 验证以及如何修复我的开关、do-while 错误 java
how to add try and catch validation and how to fix my switch, do-while error java
我不能 post 这里的代码不知道为什么会出错所以我 post 在 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:
的顶部来使它成为一个全局变量
do{
int choose;
..blah blah..
}
并且在没有 int 的情况下引用它 - 只是 choose
:
choose = read.nextInt();
最后,您需要在每个 case
的末尾添加一个 break
,return
的意义何在?:
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....
你不应该依赖别人来解决你简单的语法问题,当你遇到错误时不要惊慌,只需检查你的代码并学习如何调试消息!
我不能 post 这里的代码不知道为什么会出错所以我 post 在 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:
do{
int choose;
..blah blah..
}
并且在没有 int 的情况下引用它 - 只是 choose
:
choose = read.nextInt();
最后,您需要在每个 case
的末尾添加一个 break
,return
的意义何在?:
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....
你不应该依赖别人来解决你简单的语法问题,当你遇到错误时不要惊慌,只需检查你的代码并学习如何调试消息!