0

I'm trying to get a random number using the Random().nextInt() function. I don't know if it's right or if my logic is messed up or not reading right. Also for loop isn't working the way that it's suppose to be working. Can you please help? I'm trying to finish an assignment for class since we started Java like last month.

//DECLARE VARIABLES
    String rounds, userChooses;
    Random computerChooses = new Random();
    int round = 1;
    int userChoice = 0;
    final int ONE = 1;
    final int TWO = 2;
    final int THREE = 3;
    int computerChoice = computerChooses.nextInt(3) + 1;

    //ASK USER FOR NUMBER OF ROUNDS
    rounds = JOptionPane.showInputDialog(null, "How many rounds do you want to play?");
    round = Integer.parseInt(rounds);

    //TRACK NUMBER OF ROUNDS
    for (int x = 1; x <= round; x++) {
        JOptionPane.showMessageDialog(null, "This is round " + x + ".");

    //CREATE THE INPUT FOR THE USER
    try {

    //START GAME
    userChooses = JOptionPane.showInputDialog(null, "Enter 1)rock, 2)paper, or 3)scissors!");
    userChoice = Integer.parseInt(userChooses);
    if (userChoice > 3 || userChoice < 1) {
        throw new Exception();
    }

    } catch (Exception ex) {
        JOptionPane.showInputDialog(null, "That wasn't a number!", "Error!", JOptionPane.ERROR_MESSAGE);
        JOptionPane.showMessageDialog(null, "You have not entered correct number! Terminating program!");
        System.exit(0);
    }
    if (userChoice == ONE) {
        if (computerChoice == ONE) {
            JOptionPane.showMessageDialog(null, "You tied the computer.\nYou chose: rock\nComputer chose: rock");
        } else if (computerChoice == TWO) {
            JOptionPane.showMessageDialog(null, "You lost!\nYou chose: rock\nComputer chose: paper");
        } else if (computerChoice == THREE){
            JOptionPane.showMessageDialog(null, "You won!\nYou chose: rock\nComputer chose: scissors");
        }
    } else if (userChoice == TWO) {
        if (computerChoice == ONE) {
            JOptionPane.showMessageDialog(null, "You won!\nYou chose: paper\nComputer chose: rock");
        } else if (userChoice == TWO) {
            JOptionPane.showMessageDialog(null, "You tied!\nYou chose: paper\nComputer chose: paper");
        } else if (userChoice == THREE) {
            JOptionPane.showMessageDialog(null, "You lost!\nYou chose: paper\nComputer chose: scissors");
        }
    } else if (userChoice == THREE) {
        if (computerChoice == ONE) {
            JOptionPane.showMessageDialog(null, "You lost!\nYou chose: scissors\nComputer chose: rock");
        } else if (computerChoice == TWO) {
            JOptionPane.showMessageDialog(null, "You won!\nYou chose: scissors\nComputer chose: paper");
        } else if (computerChoice == THREE) {
            JOptionPane.showMessageDialog(null, "You tied!\nYou chose: scissors\nComputer chose: scissors");
        }
    }
    }

}

Every time I choose something, it says that the computer always chooses the rock option. Any idea?

  • Possible [duplicate](http://stackoverflow.com/questions/363681/generating-random-number-in-a-range-with-java) – Amarnath Jan 25 '13 at 18:01
  • You initiate the computer choice outside the loop, so its choice is always the same across all rounds. See also my update answer with a working code. – Guillaume Polet Jan 25 '13 at 18:37

1 Answers1

4

Simply by using a Random object:

new Random().nextInt(3) + 1;

As for your for-loop, the closing brace should be put after your catch Exception-block and not after: JOptionPane.showMessageDialog(null, "This is round " + round + ".");

Here is something that seems to work better:

import java.util.Random;

import javax.swing.JOptionPane;
import javax.swing.SwingUtilities;

public class RockPaperScissors {
    private static final int ROCK = 1;
    private static final int PAPER = 2;
    private static final int SCISSORS = 3;

    private Random computerChooses = new Random();

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {
            @Override
            public void run() {
                new RockPaperScissors().play();
            }
        });
    }

    protected void play() {
        String rounds;
        String userChooses;
        int round = 0;
        int userChoice;

        // CREATE THE INPUT FOR THE USER
        try {
            rounds = JOptionPane.showInputDialog(null, "How many rounds do you want to play?");
            round = Integer.parseInt(rounds);

            // CREATE COUNTER
            for (int x = 0; x < round; x++) {
                int computerChoice = computerChooses.nextInt(3) + 1;
                JOptionPane.showMessageDialog(null, "This is round " + x + ".");

                // START GAME
                userChooses = JOptionPane.showInputDialog(null, "Enter 1)rock, 2)paper, or 3)scissors!");
                userChoice = Integer.parseInt(userChooses);

                String message = null;
                switch (userChoice) {
                case ROCK:
                    switch (computerChoice) {
                    case ROCK:
                        message = "You tied computer. You both chose rock";
                        break;
                    case PAPER:
                        message = "You win.";
                        break;
                    case SCISSORS:
                        message = "You loose";
                        break;

                    }
                    break;
                case PAPER:
                    switch (computerChoice) {
                    case ROCK:
                        message = "You win.";
                        break;
                    case PAPER:
                        message = "You tied computer. You both chose paper";
                        break;
                    case SCISSORS:
                        message = "You loose";
                        break;

                    }
                    break;
                case SCISSORS:
                    switch (computerChoice) {
                    case ROCK:
                        message = "You loose";
                        break;
                    case PAPER:
                        message = "You win";
                        break;
                    case SCISSORS:
                        message = "You tied computer. You both chose scissors";
                        break;

                    }
                    break;

                }
                JOptionPane.showMessageDialog(null, message);
            }
        } catch (Exception ex) {
            JOptionPane.showInputDialog(null, "That wasn't a number!", "Error!", JOptionPane.ERROR_MESSAGE);
            JOptionPane.showMessageDialog(null, "You have not entered correct number! Terminating program!");
            System.exit(0);
        }
    }
}
Guillaume Polet
  • 47,259
  • 4
  • 83
  • 117