简体   繁体   中英

If statement after default switch case - Java

I'm working on a Java program and I'm struggling with creating what I'm calling the statistical area for my program. I've created a "Rock" "Paper" "Scissors" program and I'm trying to tell the user how many times they won and how many times the computer won and finally, how many ties there were.

Please excuse my ignorance, I'm learning. Here's my code:

 import java.util.Scanner;

    public class TheAntlers {
    public static void main(String[] args) {

        int playerHumanWins = 0;
        int playerComputerWins = 0;
        int numberOfTies = 0;
        int computerResult;

        Scanner input = new Scanner(System.in);

        while(true) {

            String startGame;
            String playerHuman;
            String playerComputer = " ";

            System.out.print("Do you want to play \"Rock\", \"Paper\", \"Scissors\"? (Y/N): ");
            startGame = input.nextLine().toUpperCase();

            if(startGame.equals("N")) {
                System.out.println("NO!");
                break;
            }
            else if(! startGame.equals("Y")) {
                startGame = startGame.toLowerCase();
                System.out.println("Sorry, " + startGame + " is not a valid entry...");               
            }
            while(startGame.equals("Y")) {
                System.out.print("Please choose \"Rock\", \"Paper\", or \"Scissors\": ");
                playerHuman = input.nextLine().toUpperCase();

                computerResult = (int)(Math.random() * 3);

                if(computerResult == 0) {
                    playerComputer = "ROCK";
                }
                else if(computerResult == 1) {
                    playerComputer = "PAPER";
                }
                else if (computerResult == 2) {
                    playerComputer = "SCISSORS";
                }

                switch (playerHuman) {
                    case "ROCK" :
                        if(playerComputer.equals(playerHuman)) {
                        System.out.println("Tie you both picked \"ROCK\"");
                        numberOfTies++;
                    }
                        else if(playerComputer.equals("PAPER")) {
                            System.out.println("Sorry, the computer wins!");
                            playerComputerWins++;
                        }
                        else {
                            System.out.println("You win, \"ROCK\" beats " + "\"" + playerComputer + "\"");
                            playerHumanWins++;
                            return;
                        }
                        break;
                    case "PAPER" :
                        if(playerComputer.equals(playerHuman)) {
                        System.out.println("Tie you both picked \"PAPER\"");
                        numberOfTies++;
                    }
                        else if(playerComputer.equals("ROCK")) {
                            System.out.println("You win, \"PAPER\" beats " + "\"" + playerComputer + "\"");
                            playerHumanWins++;
                            return;
                        }
                        else {
                            System.out.println("Sorry, the computer won!");
                            playerComputerWins++;
                        }
                        break;
                    case "SCISSORS" :
                        if(playerComputer.equals(playerHuman)) {
                        System.out.println("Tie you both picked \"SCISSORS\"");
                        numberOfTies++;
                    }
                        else if(playerComputer.equals("PAPER")) {
                            System.out.println("You win, \"SCISSORS\" beats " + "\"" + playerComputer + "\"");
                            playerHumanWins++;
                            return;
                        }
                        else {
                            System.out.println("Sorry, the computer won!");
                            playerComputerWins++;
                        }
                        break;
                    default:
                        playerHuman = playerHuman.toLowerCase();
                        System.out.println("Sorry, " + playerHuman + " is not a valid entry...");      
                }

                if(playerHumanWins == 1) {
                    System.out.println("You won: " + playerHumanWins + " times");
                    System.out.println("The computer won " + playerComputerWins + " times");
                    System.out.println("There were " + numberOfTies++);
                }
            }
        }
    }
}

Basically I've made an if statement that says when the user wins at least once, display the statistics However this doesn't seem to work. I'm not 100% sure how I can get the statistics to display.

Basically, the return statements in your code and forcing Java to exit your ( main ) method, which is preventing it from executing you "statistics" section of the code and terminating your program.

Start by removing them.

Also...

if (playerHumanWins == 1) {...

Means that the statistics will only be displayed once, when the first time the player wins. Not sure if this is really want you want or not...

Updated

Based on the condition that the game can only end until if the player wins a game, you could do something like...

    if (playerHumanWins == 1) {
        break;
    }
}

System.out.println("You won: " + playerHumanWins + " times");
System.out.println("The computer won " + playerComputerWins + " times");
System.out.println("There were " + numberOfTies++);

Or...

while (startGame.equalsIgnoreCase("Y") && playerHumanWins < 1) {
    //...
}

System.out.println("You won: " + playerHumanWins + " times");
System.out.println("The computer won " + playerComputerWins + " times");
System.out.println("There were " + numberOfTies++);

Either way, you should also get rid of the while (true) { statement, its adding unnecessary complications...

You are using return to jump out of switch. Return exits the method. How about using break instead !

Also make sure to reset the value of playerHumanWins and others back to zero before you play a new game

Modify some change as follows:

From

 else if(! startGame.equals("Y")) {
     startGame = startGame.**toLowerCase()**;
     System.out.println("Sorry, " + startGame + " is not a valid entry...");               
 }

To

else if(! startGame.equals("Y")) {
    startGame = startGame.**toUpperCase()**;
    System.out.println("Sorry, " + startGame + " is not a valid entry...");               
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM