如何計算所有輸入的整數而不計0?如何計算輸入的整數不包括0?
也我想要的是,如果用戶輸入0然後輸出將是「沒有數字輸入除0」,但由於我有其他輸出它也將打印。
Scanner input = new Scanner(System.in);
System.out.print("Enter integers, input ends with 0: ");
int number = input.nextInt();
if(number==0){ //This prints out along with the other system.out.println
System.out.println("No numbers were entered except 0");
}
while(number!=0){
count++;
total += number;
if(number > 0){
positive++;
}
else{
negative++;
}
number = input.nextInt();
}
average = total*1.0/count;
System.out.println("The number of positives is " + positive);
System.out.println("The number of negatives is " + negative);
System.out.println("The total count is " + total);
System.out.printf("The average is " + average);
}
}
OUTPUT:
Enter integers, input ends with 0: 1 2 -1 3 0
The number of positives is 3
The number of negatives is 1
The total count is 4 //mine is reading out 5 entered integers instead of 4
The average is 1.25`
//0 is the only entered number
Enter integers, input ends with 0: 0
No numbers were entered except 0
有什麼理由不與您現有的代碼的工作? – 2014-09-05 03:28:45
目前尚不清楚您面臨的問題,您是否可以更新您的問題並提供確切的問題詳細信息。 – Chaitanya 2014-09-05 03:59:14
@Nathan 5/4是1.25, – 2014-09-05 03:59:41