簡體   English   中英

Java將switch語句轉換為另一種類型

[英]Java convert switch statement to another type

我對Java非常陌生。 我現在正在上我的第一堂Java課。

我試圖找到一種方法來使用開關指令以外的方式重寫以下代碼。 此代碼與數獨游戲有關。 我需要問幾次這個問題,然后我需要根據答案計算統計數據。

請注意,我正在參加初學者課程,不能使用高級Java代碼。

String response = "Y";

while (response.toLowerCase().equals("y"))
{

    System.out.println("Please choose the game level");
    System.out.println(" -----------------------------------");
    System.out.println(" Enter 1 for Sudoku level Beginner");
    System.out.println(" Enter 2 for Sudoku level Advanced");
    System.out.println(" Enter 3 for Sudoku level Expert");
    System.out.println(" -----------------------------------");

    Sudoku = sudoku.nextInt();

    if (Sudoku == 1) {

        int choice1 = 0;

        nbGames_Easy++;

        System.out.println("Have you won the game ?");

        System.out.print(" Enter 4 for yes ");

        System.out.print(" Enter 5 for no ");

        choice1 = sudoku.nextInt();

        sudoku.nextLine();

        switch (choice1)

        {

            case 4:

                nbGamesEasy_Finished++;

                successRate_Easy = nbGamesEasy_Finished / nbGames_Easy * 100;

                System.out.println("How many time did you take to fill the grid ?");

                System.out.println("The time must be in minutes");

                String display5 = sudoku.nextLine();

                resolutionTime_easy = (nbGamesEasy_Finished * resolutionTime_easy) + Integer.parseInt(display5) / (nbGamesEasy_Finished);

                break;

            case 5:

                successRate_Easy = nbGamesEasy_Finished - 1 / nbGames_Easy * 100;

            default:

                break;
        }

    }
}

對於初學者,可以像以前在代碼中一樣,將if語句替換為if-else if語句。 您可以嘗試以下操作:

if (choice1 == 4){

       nbGamesEasy_Finished++;

       successRate_Easy = nbGamesEasy_Finished / nbGames_Easy * 100;

       System.out.println("How many time did you take to fill the grid ?");

       System.out.println("The time must be in minutes");

       String display5 = sudoku.nextLine();

       resolutionTime_easy = (nbGamesEasy_Finished * resolutionTime_easy) + Integer.parseInt(display5) / (nbGamesEasy_Finished);

}
else if (choice1 == 5){

       successRate_Easy = nbGamesEasy_Finished - 1 / nbGames_Easy * 100;

}
else {

       //default

}

對於switch語句

    if (choice1 == 4)

    {
            nbGamesEasy_Finished++;

            successRate_Easy = nbGamesEasy_Finished / nbGames_Easy * 100;

            System.out.println("How many time did you take to fill the grid ?");

            System.out.println("The time must be in minutes");

            String display5 = sudoku.nextLine();

            resolutionTime_easy = (nbGamesEasy_Finished * resolutionTime_easy) + Integer.parseInt(display5) / (nbGamesEasy_Finished);
    }

    else if (choice1 == 5)
    {
            successRate_Easy = nbGamesEasy_Finished - 1 / nbGames_Easy * 100;
    }

// and for default, use else

    else
    {
        //this is the default statement
        //you can do what you want here if not 4 or 5
    }

暫無
暫無

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

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