为什么我的代码使用提供的集合输出荒谬的数字?我正在尝试 String.format
Why does my code output ridiculous numbers with the provided set? I'm trying String.format
我正在使用这些数字: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 并将其放入双精度时会发生什么,但它不一定是您想要的...
我正在使用这些数字: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 并将其放入双精度时会发生什么,但它不一定是您想要的...