简体   繁体   English

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

[英]Unused parameters in Java with array

I am writing a program that splits an even length array down the middle and finds the largest of the two halves.我正在编写一个程序,将一个偶数长度的数组从中间拆分,并找到两半中最大的。 This is the code I have so far, however i'm not sure what to pass as the first parameter in the calculateAverage method.这是我到目前为止的代码,但是我不确定在calculateAverage 方法中将什么作为第一个参数传递。 Any help would be appreciated thank you.任何帮助将不胜感激,谢谢。

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;
    }

}

Please try with this.请试试这个。

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;
            }
        }
    }
int secondHalf = calculateAverage(scores[scores.length / 2], 
                                        scores[scores.length - 1]);

1.you have a typo in method name. 1.你在方法名称中有一个错字。

2.pass an array name. 2.传递数组名。 There should be 3 parameters, not 2.应该有 3 个参数,而不是 2 个。

it should look somewhat like that:它应该看起来像这样:

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

You should pass what ever your function is expecting.您应该传递您的函数所期望的任何内容。 Anyway, here is the modified code:无论如何,这是修改后的代码:

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);
}
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM