0

我不断收到一个编译器错误,指出在代码显示 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; 


}
}
4

2 回答 2

3

删除;public static String Mainmenu ( menu );

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

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

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

public void myMethod() {
  //body
};
于 2013-10-05T20:06:48.313 回答
0

//您好,我的意见是,您应该使用调用特定方法的控制开关来更好地构造 main。就像是:

导入 java.util.Scanner;

公开课作业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!");
}

}

于 2013-10-05T20:48:28.117 回答