這是我第一次嘗試java問題,這是我的程序設計任務的一部分。從Java中的數字列表中查找平均值
我必須編寫一個程序來計算用戶輸入的數字列表的平均值。當輸入0時,數據實體應該被終止。我的問題是這個「ALL負數應該被忽略」
由於某種原因,下面的代碼無法正常工作,當我輸入一個負數應該忽略它,但不知什麼原因終止數據enty
import java.util.*;
class Task_8
{
public static void main()
{
Scanner inputLine = new Scanner(System.in);
float row, numberentered, numbersum = 0, negativenumber = 0;
double result, count = 0;
System.out.println ("Welcome to Task 8 of 10 of my Programming Assignment... Nearly There!");
System.out.println ("_____________________________________________________________________");
System.out.println();
System.out.println ("Enter as many numbers as you like and this program will tell you the arithmatic mean");
System.out.println ("Terminate data entry by entering 0");
do{
System.out.print ("Please enter a number: ");
numberentered = inputLine.nextInt();
count++;
if (numberentered < 0)
{
numberentered = negativenumber;
}
numbersum = (numberentered + numbersum) - negativenumber;
}
while (numberentered !=0);
result = numbersum/count;
System.out.println();
System.out.println ("*************************************");
System.out.println();
System.out.println ("The sum of all of the numbers you entered is " +numbersum);
System.out.println ("You entered " + count + " numbers");
System.out.println ("The Average/mean of the numbers that you entered is " + result);
System.out.println();
System.out.println ("*************************************");
}
}
任何想法傢伙?
謝謝
非常感謝,只有這麼幾天才學會這麼掙扎。謝謝 – Phil 2011-03-19 15:43:31