//想把異常處理加入到"租車系統"的代碼中,但是用到的一個break-label跳轉卻進入了死循環。百思不得其解,望大神解惑。
public?static?void?main(String[]?args)?{
??Scanner?input=new?Scanner(System.in);
??System.out.println("請問您是否要租車?");
??System.out.println("1、是??????????2、否");
??int?confirm=input.nextInt();
??for(int?i=1;i>0;i++){
??????if(confirm==2)?{
???????System.out.println("答答租車系統已退出,感謝您的使用!");
???????break;
??????}else?if(confirm==1)?{
???????reselect:?????//設置標簽。
???????{
???????try?{
?????????????System.out.println("請選擇您要租賃的車輛類型:");
???????System.out.println("1、汽車??????????2、貨車??????????3、皮卡");
???????int?type=input.nextInt();
???????for(int?a=1;a>0;a++){
????????????if(type==1)?{
????????????Car?car=new?Car();?????//此處及以下調用的幾個類代碼均未貼出。
????????????car.getType();
????????????car.show();
????????????break;
????????????}else?if(type==2)?{
???????Truck?truck=new?Truck();
???????truck.getType();
???????truck.show();
???????break;
?????????????}else?if(type==3)?{
????????Pickup?pickup=new?Pickup();
????????pickup.getBrand();
????????pickup.show();
????????break;
??????????????}else?{
????????????System.out.println("您的輸入有誤!請選擇您要租賃的車輛類型:");
????????????System.out.println("1、汽車??????????2、貨車??????????3、皮卡");
????????????type=input.nextInt();
????????????}
???????}
???????}catch(InputMismatchException?ime)?{
????????break?reselect;??????????//【就是這里】,此處break跳轉后為什么會進入死循環?
???????}
???????for(int?b=1;b>0;b++){
????????System.out.println("確認嗎?(是Y/否N)");
????????String?conFinal=input.next();
???????????if(conFinal.equals("Y"))?{
????????????System.out.println("您已預訂成功!");
????????????break;
????????????}else?if(conFinal.equals("N"))?{
?????????????break?reselect;?????//此處break-label運行正常。
?????????????}else{
??????????????System.out.println("您的輸入有誤!");
??????????????continue;
??????????????}
?????????}
???????break;
????????????????}
??????}else{
???????System.out.println("您的輸入有誤!請問您是否要租車?");
???????System.out.println("1、是??????????2、否");
???????confirm=input.nextInt();
???????}
?????}
??input.close();
?}?
java入門,請各位大神幫忙看看一個問題!
qq_宮湦_03802224
2018-01-09 18:09:45