Java中带有数组的未使用参数

时间:2015-10-05 14:08:34

标签: java arrays methods parameters

我正在编写一个程序,在中间分割一个偶数长度的数组,找到两半中最大的一个。这是我到目前为止的代码,但是我不确定将什么作为calculateAverage方法中的第一个参数传递。任何帮助将不胜感激,谢谢。

public int calculateAverage(int[] scores, int start, int end) {
    int count = 0;
    int score = 0;
    for (int i = start; i < end; i++) {
        score += scores[i];
        count++;
    }

    score = (score / count);

    return score;
}

public int scoresAverage(int[] scores) {
    int endOfFirstHalf = (scores.length / 2) - 1;
    int firstHalf = calculateAverage(scores[], scores[0], endOfFirstHalf);
    int secondHalf = calculateAverage(scores[], scores[scores.length / 2],     scores[scores.length - 1]);

    if (firstHalf < secondHalf) {
        return secondHalf;
    } else {
        return firstHalf;
    }

}

3 个答案:

答案 0 :(得分:0)

请试试这个。

public class TestBench{
        public static void main(String[] args){
            int[] i = {1,2,3,4};
            System.out.println(scoresAverage(i));
       }

        public static double calculateAverage(int[] scores, int start, int end) {
            int count = 0;
            double score = 0.0;
            for (int i = start; i <= end; i++) {
                score += scores[i];
                count++;
            }
            score = (score / count);

            return score;
        }

        public static double scoresAverage(int[] scores) {
            int endOfFirstHalf = (scores.length / 2) - 1;
            double firstHalf = calculateAverage(scores, 0, endOfFirstHalf);
            double secondHalf = calculateAverage(scores, endOfFirstHalf + 1, scores.length - 1);

            if (firstHalf < secondHalf) {
                return secondHalf;
            } else {
                return firstHalf;
            }
        }
    }

答案 1 :(得分:0)

int secondHalf = calculateAverage(scores[scores.length / 2], 
                                        scores[scores.length - 1]);

1.你的方法名称有拼写错误。

2.传递数组名称。应该有3个参数,而不是2个。

看起来应该是这样的:

int firstHalf = calculateAverage(scores, 0, endOfFirstHalf);
    int secondHalf = calculateAverage(scores,scores[scores.length / 2], 
                                            scores[scores.length - 1]);

答案 2 :(得分:0)

你应该通过你的功能期望。无论如何,这是修改后的代码:

public class UnusedParam {
public int calculateAverage(int[] scores, int start, int end) {
    int count = 0;
    int score = 0;

    for (int i = start; i <= end; i++) {
        score += scores[i];
        count++;
    }
    score = (score / count);

    return score;
}

public int scoresAverage(int[] scores) {
    int endOfFirstHalf = (scores.length / 2) - 1;
    int firstHalf = calculateAverage(scores, 0, endOfFirstHalf);
    int secondHalf = calculateAverage(scores,scores.length / 2, scores.length - 1);

    if (firstHalf == secondHalf) {
        System.out.print("First Half is the same as Second Half");
        return secondHalf;
    }
    else if (firstHalf < secondHalf) {
        System.out.print("Second Half is the largest");
        return secondHalf;
    } 
    else {
        System.out.print("First Half is the largest");
        return firstHalf;
    }
}

public static void main(String[] args) {

    int[] scores = {1,2,3,4,5,6,7,8};

    UnusedParam up = new UnusedParam();
    int retVal = up.scoresAverage(scores);
    System.out.println(", "+retVal);
}
}