6

Greetings Stack Overflow users, I come to you this evening for assistance on a Java program that I have created. I'm relatively new to Java so please excuse my ignorance on the topic. I have made a Java program that's a "Rock" "Paper" "Scissors" game and there seems to be an error in one of the statements.

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();

        startGame = startGame.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();

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

            playerHuman = playerHuman.toUpperCase();

            if(computerResult == 1) {
                playerComputer = "ROCK";
            }
            else if(computerResult == 2) {
                playerComputer = "PAPER";
            }
            else if (computerResult == 3) {
                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("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..."); 
                    break;
            } 
        }
    }         
}
}

The problem that I'm facing is related to the winning calculations. When I run the program and I enter rock repeatedly until I win, the output will be You win, "ROCK" beats " " but with any other option I get You win, "ROCK" beats "PAPER"

My question is, why am I getting an empty callback when playing rock?

*Also if you'd be willing to point out any other suggestions to help out a novice that would be great. *

3 Answers 3

5

Math.random() * 3 is a number at least 0 and less than 3.

After casting it to an int, it is 0, 1, or 2.

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

Suggestions:

Be concise. You could change

String startGame;
startGame = input.nextLine();
startGame = startGame.toUpperCase();

to

String startGame = input.nextLine().toUpperCase();

It's more readable when you don't have to scroll and scroll.

Also, know that equalsIgnoreCase() exists.

Sign up to request clarification or add additional context in comments.

6 Comments

Even better would be if(computerResult == 0) { playerComputer = "ROCK"; } else if(computerResult == 1) { playerComputer = "PAPER"; } else{ playerComputer = "SCISSORS"; }
Don't forget to add that if you want a number between 1 and 3, you can do: 1 + (int)Math.random() * 3;
Good point @AmirAfghani, though in this case, 0, 1 and 2 are natural for most programmers.
Do you mind if I ask you another quick question? Is there a way to enter an if statement if the player wins for instance? I want to provide statistics at the very end and I thought about adding something like if(playerHumanWins = 1) { <statement>
I don't think I follow along completely with that statement.
|
1

This is not for a complete novice, but I would model the game using this code:

enum Choice { ROCK, PAPER, SCISSORS }

enum Result { COMPUTER_WINS, TIE, HUMAN_WINS }

Result decide(Choice computer, Choice human) {
  if (human == computer) {
    return Result.TIE;
  } else if (…) {
    …
  }
}

That way you have some part of the code that handles the game itself, while some other code handles the user interaction.

Comments

0

You will find that (int)(Math.random() * 3) results in 0 sometimes and never 3, which is what is giving you your blank, as you don't have a result for 0.

Specifically, its when Math.random() returns less than .33

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.