我遇到了一些問題的最大和最小數計算...的平均值比第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
噢,我的上帝......我花了1小時試圖弄清楚發生了什麼,它的這樣一個愚蠢的錯誤。謝謝上帝,你幫我..上帝保佑你們,感謝隊友! –
謝謝! –
Thnxx快樂編碼:) –