Zyplexx
Zyplexx

Reputation: 19

Java convert switch statement to another type

I am very new to Java. I am taking my first Java class right now.

I am trying to find a way to rewrite the following code using something other than a switch instruction. This code is related to a Sudoku game. I need to ask the question a couple times, after which I need to calculate the stats based on the answers.

Please note that I am in a beginners class and cannot use advanced Java codes.

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;
        }

    }
}

Upvotes: 0

Views: 71

Answers (2)

LiXie
LiXie

Reputation: 136

For a beginner's level, you can replace a switch statement with an if-else if statement, like you did previously in your code. You could try something like below:

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

}

Upvotes: 2

jace
jace

Reputation: 1674

For switch statement

    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
    }

Upvotes: 1

Related Questions