简体   繁体   中英

The method sum(int, int, int, int) is not applicable for the arguments (int)

Create a method called average that calculates the average of the numbers passed as parameters. The previously created method sum must be used inside this method!

Define the method in the following template:

 public static int sum(int number1, int number2, int number3, int number4) { // you can copy your implementation of the method sum here } public static double average(int number1, int number2, int number3, int number4) { // write your code here // calculate the sum of the elements by calling the method sum } public static void main(String[] args) { double result = average(4, 3, 6, 1); System.out.println("Average: " + result); }

I'm struggling with putting a sum() method within my average() method. I can't figure out how to get it to work because I keep getting this error in eclipse:

The method sum(int, int, int, int) in the type Test066 is not applicable for the arguments (int)
    at Test066.average(Test066.java:12)
    at Test066.main(Test066.java:5)

Here is my code:

public class Test066 {
    public static void main(String[] args) {
        double result = average(4, 3, 6, 1);
        System.out.println("Average: " + result);
    }

    public static int sum(int number1, int number2, int number3, int number4) {
        return number1 + number2 + number3 + number4;
    }

    public static double average(int number1, int number2, int number3, int number4) {
        int avg = sum(number1 + number2 + number3 + number4);
    }
}

I understand it has something to do with me defining the average method as a double type, but I've tried changing them out to double and calling my variable "int avg = ..." as "double avg = ..." but I'm still getting the same error. I'm not sure how to lump it in since I'm used to calling it in my main method, not in a separate method.

The method sum(int, int, int, int) in the type Test066 is not applicable for the arguments (int) at Test066.average(Test066.java:12) at Test066.main(Test066.java:5)

The following line in your code is the culprit:

int avg = sum(number1 + number2 + number3 + number4);

Your sum method declaration indicates that it accepts 4 integers as arguments. But when you call it, you are passing only 1 argument - number1 + number2 + number3 + number4 is one integer, not 4.

Correct it to

int avg = sum(number1, number2, number3, number4);

With this your code will start compiling. But it might not give you the correct result. As others have indicated, you need to divide the sum with number of operands to get an average.

int avg = sum(number1, number2, number3, number4) / 4;

Also, the average will be a floating point number. So you would want to do a floating point division and store it in a double data type to avoid truncation.

double avg = sum(number1, number2, number3, number4) / 4.0;
return avg;

int avg = sum(number1 + number2 + number3 + number4);

number1 + number2 + number3 + number4 converts those 4 numbers into one, so you are trying to call sum(int) which doesn't exist.

Try int avg = sum(number1, number2, number3, number4);

Thanks @Jordan. I was still referring to the sum() method as though I were still in that method. I should've just replaced the plus signs with commas. So it should look like this:

public class Test066 {

    public static void main(String[] args) {
        double result = average(4, 3, 6, 1);
        System.out.println("Average: " + result);
    }
    public static int sum(int number1, int number2, int number3, int number4) {
        return number1 + number2 + number3 + number4;
    }
    public static double average(int number1, int number2, int number3, int number4) {
        double avg = sum(number1, number2, number3, number4) / 4;
        return avg;
    }
}

This is your issue:

sum(number1 + number2 + number3 + number4)

Your method is

public static int sum(int number1, int number2, int number3, int number4)

That takes 4 arguments. By doing number1 + number2 + number3 + number4 you are passing one argument, since you are adding all the numbers together.

What you want instead is:

int sum = sum(number1, number2, number3, number4);

And, of course, you still need to calculate the average correctly after getting the sum.

您必须添加:

double avg = (double)sum(number1,number2,number3,number4)/4;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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