2016-06-11 67 views
1

我遇到了一些問題的最大和最小數計算...的平均值比第2個數字輸入數量較多,也不會記錄第一位數字爲國內最大的...閱讀非負整數的列表,如果我輸入的第一個號碼是顯示的最大整數,最小的整數和所有的整數

看看輸出,這將有助於更好地闡述.. Calculation Error.. & 1st input problem.. 以下代碼!

public static void main(String[] args) { 

    int smallest = Integer.MAX_VALUE; 
    int largest = 0; 
    int number; 
    double totalAvg = 0; 
    double totalSum = 0; 
    int count = 0; 

    Scanner kb = new Scanner(System.in); 

    System.out.println("Enter few integers (Enter negative numbers to end input) :"); 
    while (true) { //LOOP till user enter "-1" 
     number = kb.nextInt(); 

     //Condition for the loop to break 
     if (number <= -1) { 
      System.out.println("End Of Input"); 
      break; 
     } else { 
      count = count + 1; 
     } 

     if (number < smallest) { //Problem 1 : If 1st input num is bigger than 2nd input num, 
      smallest = number; // largest num will not be recorded.. 
     } else { 
      largest = number; 
     } 

     totalSum = totalSum + number; 
     totalAvg = (totalSum/count); 

    } 

    System.out.println("The smallest number you have entered is : " + smallest); 
    System.out.println("The largest number you have entered is : " + largest); 
    System.out.println("The total sum is : " + totalSum); 
    System.out.println("The total average is : " + totalAvg); 
    System.out.println("Count : " + count); 
} // PSVM 

回答

0

做這樣的:

public static void main(String[] args) { 

int smallest = Integer.MAX_VALUE; 
int largest = 0; 
int number; 
double totalAvg = 0; 
double totalSum = 0; 
int count = 0; 

Scanner kb = new Scanner(System.in); 

System.out.println("Enter few integers (Enter negative numbers to end input) :"); 
while (true) { //LOOP till user enter "-1" 
    number = kb.nextInt(); 

    //Condition for the loop to break 
    if (number <= -1) { 
     System.out.println("End Of Input"); 
     break; 
    } else { 
     count = count + 1; 
    } 

    if (number < smallest) { //Problem 1 : If 1st input num is bigger than 2nd input num, 
     smallest = number; // largest num will not be recorded.. 
    } 

    //REMOVED ELSE ADDED another IF 

    if (number > largest){ 
     largest = number; 
    } 

    totalSum = totalSum + number; 
    totalAvg = (totalSum/count); 

} 

System.out.println("The smallest number you have entered is : " + smallest); 
System.out.println("The largest number you have entered is : " + largest); 
System.out.println("The total sum is : " + totalSum); 
System.out.println("The total average is : " + totalAvg); 
System.out.println("Count : " + count); 
} // PSVM 
+0

噢,我的上帝......我花了1小時試圖弄清楚發生了什麼,它的這樣一個愚蠢的錯誤。謝謝上帝,你幫我..上帝保佑你們,感謝隊友! –

+0

謝謝! –

+0

Thnxx快樂編碼:) –

0

的問題是你的if語句,因爲邏輯是有缺陷的。如果輸入號碼小於最小號碼,則更新最小號碼。到目前爲止,一切都是正確問題發生,因爲你更新最大的部分。這意味着,如果一個數字不是最小的,則最大值被覆蓋。但是,如果數量大於最小數量,它不會自動成爲最大數量。這樣做的正確方法,是檢查的數量if語句比最大的一個新的更大,只有在這種情況下,更新最大。

+0

感謝您的幫助萊昂! :-D –

1

如果您使用Java 8,則可以構建IntStream,並使用IntSummaryStatistics自動提取這些數字。您可以從Oracle here中找到官方文檔。

下面是代碼實現的是:

List<Integer> input = new ArrayList<>(); 
    while (true) { // LOOP till user enter "-1" 
     number = kb.nextInt(); 

     // Condition for the loop to break 
     if (number <= -1) { 
      System.out.println("End Of Input"); 
      break; 
     } else { 
      input.add(number); 
     } 
    } 
    IntSummaryStatistics z = input.stream() // gives Stream<Integer> 
      .mapToInt(Integer::intValue) // gives IntStream 
      .summaryStatistics(); // gives you the IntSummaryStatistics 
    System.out.println(z); 

如果你輸入8 3 7輸出將是:

IntSummaryStatistics{count=3, sum=18, min=3, average=6.000000, max=8} 

我希望它能幫助!