System.out.print("This program computes the average and variance of all numbers entered. ");
boolean finished = false;
int total = 0;
Scanner inputScanner = new Scanner (System.in);
double average = inputScanner.nextDouble();
double variance = inputScanner.nextDouble();
while (!finished)
{
System.out.println("Enter a number (or type 'exit'): ");
if (inputScanner.hasNextDouble())
{
total++;
System.out.println(total);
double currentNum = inputScanner.nextDouble();
average = (average) + (currentNum - average) / total;
double prevAverage = ((average * total) - currentNum) / (total - 1);
variance = ((variance * (total - 1)) + (currentNum - prevAverage)
* (currentNum - average)) / total;
System.out.println("So far the average is: " + average
+ "\nAnd the variance so far is: " + variance );
}
else if (inputScanner.hasNext("exit"))
{
finished = true;
System.out.println("Thanks for using this program.");
inputScanner.close();
我不知道为什么在它要求我输入数字之前我必须输入两个输入?方差公式也是NaN,我不确定为什么。任何帮助将不胜感激,谢谢。
修改
double var1 = (variance * (total - 1));
double var2 = (currentNum - prevAverage) * (currentNum - average);
variance = (var1 + var2) / total;
这是我对差异的计算。我如何解决它?如果我输入' 1' var2是NaN,如果我输入2 var1是Nan。
答案 0 :(得分:0)
在进入循环之前调用inputScanner.nextDouble()两次。 这需要两个值才能继续。
我建议你把它改成
chmod 755 file1
答案 1 :(得分:0)
您必须在&#34之前键入两个输入;输入一个数字(或键入'退出'):"声明如下面的行: double currentNum = inputScanner.nextDouble();
您正在使用以下行接受来自用户的两个double值: double average = inputScanner.nextDouble(); double variance = inputScanner.nextDouble();
删除以上两行,只声明/初始化变量将解决您的问题。
您正如以下公式之前的差异获得NaN 你正在做整个++内部循环,它将评估为值1 所以在下一行中,total-1将变为0,并且zoro的任何内容都将是NaN
double prevAverage =((average * total) - currentNum)/(total - 1); //将是NaN
答案 2 :(得分:0)
在循环之前执行此操作:
double average = inputScanner.nextDouble();
double variance = inputScanner.nextDouble();
您阻止,直到用户输入两个双倍值...
编辑:方差计算(我没有检查它的语义BTW)
double totalVar = variance * (double) (total - 1);
double currentNumMinusPreviousAvg = currentNum - prevAverage;
double currentNumMinusAvg = currentNum - average;
variance = (totalVar + currentNumMinusPreviousAvg * currentNumMinusAvg) / total;
这样的事情应该避免对中间表达式进行int
的无法强制转换,并且使用适当的变量名称使计算更加可读。
答案 3 :(得分:0)
您经常查询用户,并且您应该只在循环开始时执行此操作,并将其存储在变量中以在if-else语句中使用它。因为它可以是一个字符串,所以我们要求一个字符串,然后再解析为一个字符串。
boolean finished = false;
int total = 0;
Scanner inputScanner = new Scanner (System.in);
double average = inputScanner.nextDouble();
double variance = inputScanner.nextDouble();
while (!finished) {
System.out.println("Enter a number (or type 'exit'): ");
String input = inputScanner.nextLine();
if( input.equals( "exit" ) ) {
finished = true;
System.out.println("Thanks for using this program.");
inputScanner.close();
}
else {
try {
double nextDouble = Double.parseDouble( input );
//If an exception is trown it won't get past here, so
//it won't count
total++;
System.out.println(total);
double currentNum = nextDouble;
average = (average) + (currentNum - average) / total;
double prevAverage = ((average * total) - currentNum) / (total - 1);
variance = ((variance * (total - 1)) + (currentNum - prevAverage)
* (currentNum - average)) / total;
System.out.println("So far the average is: " + average
+ "\nAnd the variance so far is: " + variance );
}
catch ( NumberFormatException ex ) {
System.out.println( "The number is badly writen" );
//It can throw NumberFormatException exception if the
//number is badly writen.
}
}
}
另外,如果您打算精确使用十进制数,我建议使用BigDecimal
类。
我希望我有所帮助。
祝你有个愉快的一天。 :)