这是我的代码:
import java.util.*;
public class Assignment3
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
int numinput=0;
double suminput=0;
double lastinput=1;
double highest=0;
double lowest=0;
double firstinput=0;
int numextra=0;
System.out.println("Please type in a number. Type 0 to quit.");
firstinput=scan.nextDouble();
if(firstinput==0)
{
System.out.println("You didn't type anything in.");
return;
}
if(firstinput!=0)
{
numextra=1;
}
for(lastinput=1;lastinput != 0;numinput++)
{
System.out.println("Please type in a number. Type 0 to quit.");
lastinput=scan.nextDouble();
suminput=suminput+lastinput;
if(lastinput > highest){
highest = lastinput;
}else if (lastinput < lowest){
lowest=lastinput;
}
}
suminput= suminput+firstinput;
numinput=numinput+numextra;
System.out.println("The sum of your inputs is: "+suminput);
System.out.println("The total number of inputs you provided was: " +numinput);
System.out.println("The average of your inputs is: "+. (suminput/numinput));
System.out.println("The highest number you input was: "+highest);
System.out.println("The lowest number you input was: "+lowest);
}
}
我应该得到以下回报,
-6,
3,
-2,
-1,
-3,
但相反,我得到了这个, -6, 4, -1.5, 0, -2
有人可以解释一下我做错了什么吗? 我试图找出如何不包括起始值,如果它是0,但我有一些麻烦,这是我想出来的,但它仍然不能正常工作,虽然它更接近于期望的结果然后之前。
答案 0 :(得分:0)
numinput++
退出,for循环中的 0
也会增加1,您应该从计数中排除该输入。或者,只需删除行numinput=numinput+numextra
java.lang.Double.compare()
时,更新也是如此