我使用的是这些数字:5,4,3.5,10,20,40,80,93.33,-1。它将适用于只有5个数字的简单事物,如:5,4,3,2,1。发生了什么事?如果有问题的话,我会尝试使用String.format来缩短数字。你们注意到我的代码中有什么我可以做的不同的事情吗。这是所提供集合的输出:
The minimum is -1.0, the maximum is 2.147483647E9, and the average is 2.38609322E8
-238609317.00
-238609318.00
1908874325.00
-238609312.00
-238609302.00
-238609282.00
-238609242.00
-238609229.00
-238609323.00
期望输出
最小值为3.5,最大值为93.33,平均值为31.978749999999998
平均得分5.0为-26.978749999999998
平均得分4.0为-27.978749999999998
平均得分3.5为-28.478749999999998
得分10.0为平均的-21.978749999999998
平均得分20.0为-11.978749999999998
平均得分40.0为8.021250000000002
得分80.0是平均的48.02125
平均得分93.33为61.35125
public static void main(String[] args)
{
int count = 0;
int arrayLength;
double min = 99999;
double max = 0;
double average;
double sum = 0;
JOptionPane.showMessageDialog(null, "This program will display the average as well as each numbers distance from the average");
// need to put loop here.
arrayLength = IO.readInt("How many numbers will you be entering?");
if (arrayLength <= 0 || arrayLength > 100)
{
JOptionPane.showMessageDialog(null, "Nothing entered, program will close.");
System.exit(0);
}
double[] array = new double[arrayLength];
for (int i = 0; i <arrayLength; i++)
{
array[i] = IO.readInt("Enter number: " + (i+1));
count++;
}
for (int i = 0; i <arrayLength; i++)
{
sum = (array[i] + sum);
if (array[i] <= min)
{
min = array[i];
}
if (array[i] > max)
{
max = array[i];
}
}
average = (sum / arrayLength);
System.out.println("The minimum is " + min + ", the maximum is " + max + ", and the average is " + average);
double[] array2 = new double[arrayLength];
for (int i = 0; i <arrayLength; i++)
{
array2[i] = (array[i] - average);
}
for (int i = 0; i <arrayLength; i++)
{
System.out.println(String.format("%.2f", array2[i]));
}
}
我不知道当你做IO.readInt并将其放入double时会发生什么,但它不可能是你想要的。。。