当我运行带有测试分数的代码:71,100,98,74和87时,这是我得到的输出:
您输入的测试分数按降序排列: [I @ 1a17589 平均值是86。
为什么我的selectionSort方法输出没有正确打印出来?
import java.util.Scanner;
/**
* The Average class calculates the average of the test
* scores entered and puts the scores into descending order.
*/
public class Average
{
private int[] data;
public Average(int[] dataParam)
{
data=dataParam;
}
public int calculateMean()
{
int total = 0;
int mean;
for (int index = 0; index < data.length; index++)
total += data[index];
mean = total / data.length;
return mean;
}
public int[] selectionSort()
{
int startScan, index, minIndex, minValue;
for (startScan = 0; startScan < (data.length-1); startScan++)
{
minIndex = startScan;
minValue = data[startScan];
for(index = startScan + 1; index < data.length; index++)
{
if (data[index] < minValue)
{
minValue = data[index];
minIndex = index;
}
}
data[minIndex] = data[startScan];
data[startScan] = minValue;
}
return data;
}
}
import java.util.Scanner;
public class AverageDriver
{
public static void main(String[] args)
{
int[] data = new int[5];
Scanner keyboard = new Scanner(System.in);
for (int index = 0; index < data.length; index++)
{
System.out.println("Enter score #" + (index + 1) + ": ");
data[index] = keyboard.nextInt();
}
Average object = new Average(data);
System.out.println("The test scores you entered, in descending order, are: ");
System.out.println(object.selectionSort());
System.out.println("The average is " + object.calculateMean() +".");
}
}