标识符预期java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19202168/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 14:59:48  来源:igfitidea点击:

Identifier expected java

javamethodsaverageidentifier

提问by user2814798

I keep getting a compiler error that states an identifier is expected where the code says public static String Mainmenu (menu);. I am new to programming so I know it is probably something simple. The code uses methods and when all is compiled correctly it should run as a grade averaging program.

我不断收到一个编译器错误,指出在代码显示 public static String Mainmenu (menu); 的地方需要一个标识符。我是编程新手,所以我知道这可能很简单。代码使用方法,当所有编译正确时,它应该作为成绩平均程序运行。

import java.util.Scanner;

public class Homework4 {

public static void main( String[] args )
{
    Scanner input = new Scanner( System.in );
    int choice;
    String name;
    String menu1;
    double homeworkAverage = 0.0;
    double quizAverage = 0.0;
    double testAverage = 0.0;
    double average = 0.0;
    double totalAverage = 0.0;

    do{

        menu1 = Mainmenu;

        choice = input.nextInt();

        if( choice == 1 )
        {

            System.out.println( "Eneter the student's name:");

                name = input.next();

            System.out.println( "Enter homework grades");

                homeworkAverage = mean();

            System.out.println( "Enter Quiz Grades");

                quizAverage = mean();

            System.out.println( "Enter Test Grades");

                testAverage = mean();

            totalAverage = 0.25*homeworkAverage + 0.25*quizAverage +0.5*testAverage;

            System.out.println( "The average for " + name + " is: " + totalAverage );

            }
            else if ( choice == 2 )
            {
                System.out.println( "Now quiting...");
            }
            else
            {
                System.out.println( "Invalid Choice" );
            }


    }while( choice != 2 );
}


public static String Mainmenu ( menu );
{

    Scanner input = new Scanner( System.in );

    String menu;


        System.out.println("Welcome to Grader 1.0");
        System.out.println("1. Average a New Student");
        System.out.println("2. Quit");
        System.out.println("Please Enter Your Choice:");


        return menu;

}



public static double mean(double average );
{

Scanner input = new Scanner( System.in );

double grade = 0.0; 
double average = 0.0;
double gradeTotal = 0.0;
int numberOfGrades = 0;

while( grade != -1 )
{

    System.out.println( "Please enter the next grade or -1 when finished: "); 
    grade = input.nextDouble ();

    if( grade != -1 )
    {
        gradeTotal = gradeTotal + grade;
        numberOfGrades++;
    }

}
    if( numberOfGrades > 0 )
        {

        average = gradeTotal / numberOfGrades;

        }
        else
        {
            System.out.println("No grades, no average");
        }   

            return average; 


}
}

回答by Konstantin Yovkov

Remove the ;after public static String Mainmenu ( menu );

删除;public static String Mainmenu ( menu );

Same for the ;operator after public static double mean(double average );

;之后的操作员也一样public static double mean(double average );

The problem is you're trying to define methods, while the ;operator means end of statement. And since you're terminating after the method signatures, those methods are not provided with body blocks.

问题是您正在尝试定义方法,而;运算符则表示语句结束。并且由于您在方法签名之后终止,因此这些方法不提供主体块。

Note that you're allowed to add ;after a method's body, but not between the method signature and the body. For example,

请注意,您可以;在方法主体之后添加,但不能在方法签名和主体之间添加。例如,

public void myMethod() {
  //body
};

回答by Mitch

//Hi There, my opinion is that you should structure the main better with control switches calling the specific methods. Something like:

//您好,我的意见是您应该使用调用特定方法的控制开关更好地构建主要内容。就像是:

import java.util.Scanner;

导入 java.util.Scanner;

public class Homework4{

公开课作业4{

public static void main(String[] args) {
    int menu = 0; //initial menu selection
    int menuChoice = 0; //user menu selection
    double meanValue = 0; //Mean value

    menuChoice = MainMenu(menu); //user return menu choice              
    switch (menuChoice) {
        case 1:  FindMean(meanValue);
            break;
        case 2:  Exit();
                break;
        default:  errorMsg();
            break;
    }       
}



//MminMenu
public static int MainMenu(int menu){
    System.out.println("Welcome to Grader 1.0");
            System.out.println("1. Average a New Student");
            System.out.println("2. Quit");
    System.out.print("Select: ");

    //user selection
    @SuppressWarnings("resource")
    Scanner input = new Scanner(System.in);
    menu = input.nextInt();

    return menu;    
} 


//FindMean
public static final void FindMean(double mean) {
    //write method for FindMean

} 

    //Exit
public static final void Exit() {
    //write method for Exit

} 



// Error
public static final void errorMsg() {
    System.out.println("Something went wrong!");
}

}

}