簡體   English   中英

標識符預期的Java

[英]Identifier expected java

我不斷收到指示錯誤的編譯器錯誤,該錯誤指出代碼在代碼中顯示“ 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; 


}
}

刪除; public static String Mainmenu ( menu );

同樣的; public static double mean(double average );后的運算符public static double mean(double average );

問題是您試圖定義方法,而; 運算符表示語句結束 而且由於您要在方法簽名之后終止,因此這些方法未隨主體塊一起提供。

請注意,您可以添加; 在方法的主體之后, 但不在方法簽名和主體之間 例如,

public void myMethod() {
  //body
};

//嗨,我的觀點是,您應該通過調用特定方法的控制開關更好地構造主體。 就像是:

導入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!");
}

}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM