2016-04-27 89 views
3

我是一名初學Java程序員,他正在編寫計算一組正整數平均值的程序。首先,它向用戶查詢要輸入的整數量。然後,它從用戶收集整數並輸出計算出的平均值。我在處理例外時遇到問題。當用戶嘗試輸入一個負數以進行平均時,正確顯示例外,但它不能正確地繼續for循環以收集適當數量的數字。例如,下面是一個輸出示例:無法正確處理異常

Please enter the number of integers to be averaged: 5 
Enter a number: 1 
Enter a number: 2 
Enter a number: -3 
NegativeIntegerException: N must be a positive integer. 
Enter a number: 3 
Enter a number: 4 
The average is: 0.0 

它正確拋出異常,但沒有繼續填充5個整數。只有其他4個是有效的。

下面是代碼:

import java.util.*; 

class NegativeIntegerException extends Exception { 

    public NegativeIntegerException() 
    { 
     super("N must be a positive integer."); 
    } 
} 


public class intAverage { 

    public static void main(String[] args) 
    { 
     int N = 0; //number of integers to be averaged 
     int[] numbers = null; //array to hold integers 
     int sum = 0; 
     int newInt; 
     double average; 
     Scanner keyboard = new Scanner(System.in); 
     int x = 1; //for do-while loop #1 
     int z = 1; //for do-while loop #2 

     do { 
     try { 
      System.out.print("Please enter the number of integers to be averaged: "); 
      N = keyboard.nextInt(); 
      numbers = new int[N]; //setting size of array 
      x = 2; 
     } 
     catch (Exception e) { 
      System.out.println("N must be a positive integer"); 
     } 
     } while (x == 1); 

     do { 
      for(int i = 0; i < N; i++) //collecting the integers 
      { 
       System.out.print("Enter a number: "); 
       newInt = keyboard.nextInt(); 
       if (newInt < 0) { 
        try { 
         throw new NegativeIntegerException(); 
        } 
        catch(NegativeIntegerException e) { 
         System.out.println(e); 
        } 
       } 
       newInt = numbers[i]; 
      } 
      z = 2; 
     } while (z == 1); 


     for(int y = 0; y < N; y++) //calculate average 
     { 
      sum = sum + numbers[y]; 
     } 
     average = sum/N; 

     System.out.println("The average is: " + average); 

    } 

} 
+0

我想你的意思是說......'數字[i] = newInt'不是'newInt =數字[i]' – 3kings

回答

3

當你打印出來的例外,從i減1,然後繼續。這基本上會重新開始迭代。

do { 
    for(int i = 0; i < N; i++) //collecting the integers 
    { 
     System.out.print("Enter a number: "); 
     newInt = keyboard.nextInt(); 
     if (newInt < 0) { 
      try { 
       throw new NegativeIntegerException(); 
      } 
      catch(NegativeIntegerException e) { 
       System.out.println(e); 
       // Ignore this input 
       i--; 
       continue; 
      } 
     } 
     numbers[i] = newInt; 
    } 
    z = 2; 
} while (z == 1); 

請注意,您不需要拋出異常這個工作 - 你可以很容易地說:

do { 
    for(int i = 0; i < N; i++) //collecting the integers 
    { 
     System.out.print("Enter a number: "); 
     newInt = keyboard.nextInt(); 
     if (newInt < 0) { 
      System.out.println("NegativeIntegerException: N must be a positive integer"); 
      // Ignore this input 
      i--; 
      continue; 
     } 
     numbers[i] = newInt; 
    } 
    z = 2; 
} while (z == 1); 

這是更具可讀性,太。

+0

啊,我明白了。我做了更改,現在它正確處理負數。謝謝。我想知道爲什麼如果我輸入帶小數點的分數,字母或數字,爲什麼它會將程序發送到無限循環?我猜測它與do-while循環有關,但我不確定什麼是處理類型不匹配異常的有效方法。 – Daksakar