打印阵列平均值和最高

时间:2015-09-21 16:49:03

标签: java arrays

我正在尝试将getAverage和getHighest调用到我的驱动程序类并将它们打印在屏幕上。但是,我一直在获取垃圾值。知道这个程序有什么问题吗?谢谢

public class ArrayOperations
{
    public double getAverage(int[] array)
    {
        double total = 0;
        double average;

        for (int index = 0; index < array.length; index++)
            total += array[index];

        average = total / array.length;
        System.out.println("The average is: " + average);
        return average;
    }

    public int getHighest(int[] array)
    {
        String output = new String("");
        int highest = array[0];
        for(int i = 1; i < array.length; i++)
        {
            if (array[i] > highest)
                highest = array[i];
            System.out.println("The highest score=" + highest);
        }
        return highest;
    }
}

驱动程序类:

public class ArrayOperationDriver
{
    public static void main(String[] args)
    {
        int [] testScores = {80, 90, 58, 75, 85, 45, 68, 72, 95};
        ArrayOperations object = new ArrayOperations();
        System.out.println(object);
    }
}

3 个答案:

答案 0 :(得分:1)

你在任何地方调用方法。只需要

System.out.println(object.getAverage(testScore));
System.out.println(object.getHighest(testScore));

使用您的代码,您只需打印对象,该对象为您提供该对象的字符串表示。

答案 1 :(得分:0)

选项1:

您获得的“垃圾”值是object所在的内存位置。而是调用方法:

System.out.println(object.getAverage(testScores));
System.out.println(object.getHighest(testScores));

我将对您的代码添加一个修改,因此它不会打印很多“最高价值是:”

public int getHighest(int[] array) {
    String output = new String("");
    int highest = array[0];
    for(int i = 1; i < array.length; i++) {
        if (array[i] > highest)
            highest = array[i];
            //Removed from here
            //System.out.println("The highest score=" + highest);
    }
    //Moved to here
    System.out.println("The highest score=" + highest);
    return highest; 
}

选项2:

但是,由于您已经在方法中打印,我将其更改为void并取消return语句。如下:

public class ArrayOperations {
    public void getAverage(int[] array) {
        double total = 0; 
        double average; 
        for (int index = 0; index < array.length; index++)
            total += array[index];

        average = total / array.length;
        System.out.println("The average is: " + average);
    }

    public void getHighest(int[] array) {
        String output = new String("");
        int highest = array[0];
        for(int i = 1; i < array.length; i++) {
            if (array[i] > highest)
                highest = array[i];
        }
        System.out.println("The highest score=" + highest);
    }
}

以这种方式调用方法:

public class ArrayOperationDriver {
    public static void main(String[] args) {
        int [] testScores = {80, 90, 58, 75, 85, 45, 68, 72, 95};
        ArrayOperations object = new ArrayOperations();
        object.getAverage(testScores);
        object.getHighest(testScores);
    }
}

我想这是一种更干净的方式。

选项3:

另外还有一个选择,就是从你的方法中返回数字,但是从里面删除S.o.p调用。

public class ArrayOperations {
    public double getAverage(int[] array) {
        double total = 0; 
        double average; 
        for (int index = 0; index < array.length; index++)
            total += array[index];

        average = total / array.length;
        return average;
    }

    public int getHighest(int[] array) {
        String output = new String("");
        int highest = array[0];
        for(int i = 1; i < array.length; i++) {
            if (array[i] > highest)
                highest = array[i];
        }
        return highest; 
    }
}

将它们放在main方法中:

public class ArrayOperationDriver {
    public static void main(String[] args) {
        int [] testScores = {80, 90, 58, 75, 85, 45, 68, 72, 95};
        ArrayOperations object = new ArrayOperations();
        System.out.println("Average number is: " + object.getAverage(testScores));
        System.out.println("Highest number is: " + object.getHighest(testScores));
    }
}

修改

要打印数组testScores中的所有数字,您可以在简单的for循环或for-each循环中执行此操作,我使用了for-each,但如果您使用for,则可以尝试使用for想知道怎么做。

public class ArrayOperationDriver {
    public static void main(String[] args) {
        int [] testScores = {80, 90, 58, 75, 85, 45, 68, 72, 95};
        ArrayOperations object = new ArrayOperations();
        System.out.println("Average number is: " + object.getAverage(testScores));
        System.out.println("Highest number is: " + object.getHighest(testScores));

        System.out.print("The test scores are: ");
        for (int score : testScores) 
            System.out.print(score + " ");
        System.out.println("");
    }
}

答案 2 :(得分:0)

您不是在呼叫getAveragegetHighest。您只是打印ArrayOperations,实际上是ArrayOperations.toString()。由于您没有覆盖它,因此您将获得默认实现,该实现将打印类名及其默认hashCode()实现。

最佳做法是从ArrayOperations&#39;删除打印。方法,只返回结果。打印应由调用者(驱动程序)类处理。这样,如果调用者想要对结果执行其他操作(例如,将其显示在网页中,将其保存到数据库中,执行另一次计算),则可以。

public class ArrayOperationDriver
{
    public static void main(String[] args)
    {
        int [] testScores = {80, 90, 58, 75, 85, 45, 68, 72, 95};
        ArrayOperations object = new ArrayOperations();
        System.out.println
            ("The average score is: " + object.getAverage(testScores));
        System.out.println
            ("The highest score is: " + object.getHighest(testScores));
    }
}