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 9270057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T15:16:40+00:00 2026-06-18T15:16:40+00:00

So im making everyone’s fun game Rock, Paper, Scissors I got everything working, except

  • 0

So im making everyone’s fun game “Rock, Paper, Scissors” I got everything working, except having the while loop repeat 3 time before stopping. Well it does repeat 3 times and stops, but the 2nd and 3rd repeat the variables don’t change. Take a look at the code and tell me what I’m doing wrong.
**UPDATE: Now that I have everything working how do I get this “Q” string to terminate the loop?

import java.util.Scanner;
import java.util.Random;

public class RockPaperScissors
{

    /**
     * (Insert a brief description that describes the purpose of this method)
     * 
     * @param args
     */
    public static void main(String[] args)
    {
        int compint;
        String usermove = "";
        String compmove = "";
        String winner = "";
        int count = 0;


        Scanner in = new Scanner(System.in);
        Random gen = new Random();

        System.out.println("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
        int input = in.nextInt();       

        while (count < 3)
        {
            compint = gen.nextInt(3) + 1;

            if (input == 1)
            {
                usermove = "Rock";
            }
            else if (input == 2)
            {
                usermove = "Paper";
            }
            else if (input == 3)
            {
                usermove = "Scissors";
            }

            if (compint == 1)
            {
                compmove = "Rock";
            }
            else if (compint == 2)
            {
                compmove = "Paper";
            }
            else if (compint == 3)
            {
                compmove = "Scissors";
            }

            if (compint == input)
            {
                winner = "TIE";
            }
            else if (compint == 1 && input == 3)
            {
                winner = "COMPUTER";
            }
            else if (compint == 2 && input == 1)
            {
                winner = "COMPUTER";
            }
            else if (compint == 3 && input == 2)
            {
                winner = "COMPUTER";
            }
            else
            {
                winner = "USER";
            }

            System.out.print("Computer: " + compmove + " | ");
            System.out.print("You: " + usermove + " | ");
            System.out.println("Winner: " + winner);
            System.out.println();
            System.out.println("Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: ");
            input = in.nextInt();
            count++;

        }
    }
}

Output:

Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: 
1
Computer: Scissors | You: Rock | Winner: USER

Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: 
2
Computer: Rock | You: Paper | Winner: USER

Enter Rock(1), Paper(2), Scissors(3) {Q to quit]: 
Q
Exception in thread "main" java.util.InputMismatchException
    at java.util.Scanner.throwFor(Unknown Source)
    at java.util.Scanner.next(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at java.util.Scanner.nextInt(Unknown Source)
    at RockPaperScissors.main(RockPaperScissors.java:102)
  • 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-18T15:16:41+00:00Added an answer on June 18, 2026 at 3:16 pm

    The logic that actually does anything with the input – all those if statements – is outside of the loop. With each iteration through the look, none of that logic is not actually executed. It all just happens first. Try this instead:

    for (int count=0; count < 3; count++)
    {
        int input = in.nextInt();
        int compint = gen.nextInt(3) + 1;
    
        // all the if statements and printing here
    }
    

    **UPDATE: Now that I have everything working how do I get this “Q” string to terminate the loop?

    You’re getting an InputMismatchException when typing Q but the code calls Scanner#nextInt(). The docs are pretty clear on what the problem is:

    Thrown by a Scanner to indicate that the token retrieved does not match the pattern for the expected type, or that the token is out of range for the expected type.

    It’s basically the Scanner‘s way of telling you “you asked for an int but the next token isn’t one.” You can add an additional check before the nextInt() calls, using Scanner#hasNextInt(), to verify that the next token actually is an int. If it’s not an int, then you can plan on parsing it just as a string.

    So instead of this:

    input = in.nextInt();
    

    Do something like this:

    if (in.hasNextInt())
    {
        input = in.nextInt();
    } else if (in.hasNext("Q")) {
        // quit
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm sitting at college making a noughts and crosses game while everyone else is
I'm making a server for a game and I need to save everyone's second
Hello everyone, I am making a VC++ 2008 project in pure Win32 API which
Hi everyone, I'm stuck these days on some memory leaks. The app I'm making
Thanks for your time everyone. I'm working with C# .Net 4.0 and Visual Studio
First of all, Hello everyone(/world) ! I am making an Eclipse RCP app' who
Hi everyone im making a perl script to encrypt and decrypt text, i just
I am making my own MVC framework (please do not downvote me because everyone
I'm making a game which uses procedurally generated levels, and when I'm testing I'll
I'm making my first site with Django, and I'm having a database design problem.

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.