在Java中编译时的错误代码

时间:2014-09-29 03:22:17

标签: java

好的,所以我试图让我的代码给我一个返回类型,但它一直给我一个错误。 这是我的代码。

import java.util.Scanner;

public class Gradebookmodified {
        public static void main(String[] args ){
            mainMenu();
            mainInstructions();
            mainGrades();
            average();
        }

        public static void mainMenu(){  
            Scanner input = new Scanner(System.in);
            System.out.print("Choose an option: \n1.Average grades for a new student \n2.Quit \n");
            int selection;
            selection = input.nextInt();
            if( selection == 1){
                System.out.println("Enter name of new student");
                String name = input.next();
            }else {
            }
        }

        public static void mainInstructions(){
            Scanner input = new Scanner(System.in);
            System.out.println("Grades will be entered in this order: \n1. Homework \n2. Quizzes \n3. Tests");
        }

        public static void mainGrades(){
            Scanner input= new Scanner(System.in);

            double[] homework = new double[5];
            for(int i = 0; i < homework.length; i++){
                System.out.println("Enter homework grades.");
                homework[i] = input.nextDouble();
            }

            double[] quizzes = new double[4];
            for(int j = 0; j <quizzes.length; j++){
                System.out.println("Enter quiz grades.");
                quizzes[j] = input.nextDouble();
            }

            double[] tests = new double[4];
            for(int k = 0; k <tests.length; k++){
                System.out.println("Enter test grades.");
                tests[k] = input.nextDouble();
            }
        }

        public static double average(double homework, int i){
            Scanner input = new Scanner(System.in);
            double result = 0.0;
            if (homework > 0){
                result = i / homework;
            }
            return result;
        }
}

这是编译器不断给我的错误。

Gradebookmodified.java:16: error: method average in class Gradebookmodified cannot be applied to given types;
        average();
        ^
required: double,int
found: no arguments
reason: actual and formal argument lists differ in length
1 error

我不知道为什么它一直给我这个错误,但如果有人能告诉我我做错了什么,那就太棒了。

3 个答案:

答案 0 :(得分:2)

您在主要方法中使用了average();

但您的平均功能签名是

average(double homework, int i)

注意:如您所见,您在main方法中使用平均功能与您的功能签名不一致。

答案 1 :(得分:0)

您已将平均方法声明为

public static double average(double homework, int i)

因此,当您调用它时,您应该将double和int参数传递给该方法。

错误是你的代码中没有任何参数调用它。 average();

你必须这样称呼它 e.g。

double homework = 2.3; // some value
int i = 1; // some value
average(homework, i);

答案 2 :(得分:0)

调用&#34时,你必须传递3个参数。平均()&#34;方法。当你调用&#34; average()&#34;我无法看到你没有传递任何参数。这就是编译器无法编译代码的原因。

方法参数的数量应与方法参数的数量匹配。否则编译器无法编译代码。

当您调用&#34; average()&#34;方法,你应该传递三个参数,如下,

平均值(5.5,5);

使用上面的代码并尝试继续其余的工作

祝你好运!!!!!

相关问题