2013-11-15 144 views
0

我的問題是簡短而甜蜜的。我不明白爲什麼我的程序在發現錯誤時無限循環。我做了一個全新的try-catch語句,但它循環,甚至複製,粘貼和修改以前的程序中的適當變量。下面是聲明本身,下面是整個程序。感謝您的幫助!異常處理無限循環

try { 
    input = keyboard.nextInt(); 
} 
catch(Exception e) { 
    System.out.println("Error: invalid input"); 
again = true; 

} 
if (input >0 && input <=10) 
    again = false; 

} 

計劃:

public class Blanco { 

    public static int input; 

    /** 
    * @param args the command line arguments 
    */ 
    public static void main(String[] args) { 
     // TODO code application logic here 
     nameInput(); 



    } 

    /** 
    * 
    * @param name 
    */ 
    public static void nameInput() { 

     System.out.println("What is the name of the cartoon character : "); 
     Scanner keyboard = new Scanner(System.in); 
     CartoonStar star = new CartoonStar(); 
     String name = keyboard.next(); 
     star.setName(name); 
     typeInput(keyboard, star); 

    } 

    public static void typeInput(Scanner keyboard, CartoonStar star) { 

boolean again = true; 
while(again){ 
     System.out.println("What is the cartoon character type: 1 = FOX,2 = CHICKEN,3 = RABBIT,4 = MOUSE,5 = DOG,\n" 
       + "6 = CAT,7 = BIRD,8 = FISH,9 = DUCK,10 = RAT"); 

try { 
    input = keyboard.nextInt(); 
} 
catch(Exception e) { 
    System.out.println("Error: invalid input"); 
again = true; 

} 
if (input >0 && input <=10) 
    again = false; 

} 


     switch (input) { 
      case 1: 
       star.setType(CartoonType.FOX); 
       break; 

      case 2: 
       star.setType(CartoonType.CHICKEN); 
       break; 
      case 3: 
       star.setType(CartoonType.RABBIT); 
       break; 
      case 4: 
       star.setType(CartoonType.MOUSE); 
       break; 
      case 5: 
       star.setType(CartoonType.DOG); 
       break; 
      case 6: 
       star.setType(CartoonType.CAT); 
       break; 
      case 7: 
       star.setType(CartoonType.BIRD); 
       break; 
      case 8: 
       star.setType(CartoonType.FISH); 
       break; 
      case 9: 
       star.setType(CartoonType.DUCK); 
       break; 
      case 10: 
       star.setType(CartoonType.RAT); 
       break; 
     } 
     popularityNumber(keyboard, star); 
    } 

    public static void popularityNumber(Scanner keyboard, CartoonStar star) { 
     System.out.println("What is the cartoon popularity number?"); 
     int popularity = keyboard.nextInt(); 
     star.setPopularityIndex(popularity); 
     System.out.println(star.getName() + star.getType() + star.getPopularityIndex()); 
    } 

} 
+0

使用java調試器調試您的代碼並追蹤執行,在方法頂部暫停它,然後逐步檢查變量值,直到找出結果。我們可以爲你弄明白,但是你學習調試你的代碼比我們可以告訴你的關於這個代碼的任何東西都更有價值。 – Bohemian

+0

謝謝你的建議。像這樣的評論是我最看重的東西。我不想讓別人爲我做,我想知道它是如何做到的! – user2946846

回答

3

您的程序一直運行,因爲調用nextInt不改變掃描儀的狀態要一次又一次地會導致異常:如果用戶沒有輸入int,呼籲keyboard.nextInt()不會改變掃描儀正在查看的內容,因此當您在下一次迭代中調用keyboard.nextInt()時,將會發生異常。

您需要添加一些代碼來讀取用戶維修異常後進入垃圾來解決這個問題:

try { 
    ... 
} catch(Exception e) { 
    System.out.println("Error: invalid input:" + e.getMessage()); 
    again = true; 
    keyboard.next(); // Ignore whatever is entered 
} 

注意:你不需要依靠例外在這種情況下:而不是調用nextInt(),您可以撥打hasNextInt(),並檢查掃描儀是否正在查看整數。