Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9188819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:06:17+00:00 2026-06-17T20:06:17+00:00

I’m trying to get a random number using the Random().nextInt() function. I don’t know

  • 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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T20:06:18+00:00Added an answer on June 17, 2026 at 8:06 pm

    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);
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I am trying to find ID3V2 tags from MP3 file using jid3lib in Java.
I need a function that will clean a strings' special characters. I do NOT
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Basically, what I'm trying to create is a page of div tags, each has
I am trying to understand how to use SyndicationItem to display feed which is
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.