This programming project/assignment had me write a program that lets the user play rock paper scissors with a computer until the user decides to quit, at which point their score(wins, loses, ties) will be displayed.

The program I have so far works, aside from the "loop until the user decides to quit" part :

import java.util.*;
public class RockPaperScissors {

    public static void main(String[] args) {
        int win = 0;
	int lose = 0;
	int tie = 0;
	do {

	Scanner keyboard = new Scanner(System.in);
    	Random generator = new Random();

    	System.out.println("1 for Rock, 2 for Paper, 3 for Scissors (anything else to quit): ");
    	int userinput = keyboard.nextInt();
    	int compinput = generator.nextInt(3);
		String userresult = Integer.toString(userinput);
		String compresult = Integer.toString(compinput);

    	if (userinput == compinput){
    		System.out.println("You picked option " + userresult + ", the computer picked option " + compresult + ".  It is a tie");
    		tie++;
    	}
    	else if (userinput == 1 && compinput == 2){
    		System.out.println("You picked option " + userresult + ", the computer picked option " + compresult + ".  You lose");
    		lose++;
    	}
    	else if (userinput == 1 && compinput == 3){
    		System.out.println("You picked option " + userresult + ", the computer picked option " + compresult + ".  You win");
    		win++;
    	}
    	else if (userinput == 2 && compinput == 1){
    		System.out.println("You picked option " + userresult + ", the computer picked option " + compresult + ".  You lose");
    		lose++;
    	}
    	else if (userinput == 2 && compinput == 3){
    		System.out.println("You picked option " + userresult + ", the computer picked option " + compresult + ".  You win");
    		win++;
    	}
    	else if (userinput == 3 && compinput == 1){
    		System.out.println("You picked option " + userresult + ", the computer picked option " + compresult + ".  You lose");
    		lose++;
    	}
    	else if (userinput == 3 && compinput == 2){
    		System.out.println("You picked option " + userresult + ", the computer picked option " + compresult + ".  You win");
    		win++;
    	}
    	else{
    		String won = Integer.toString(win);
    		String lost = Integer.toString(lose);
    		String tied = Integer.toString(tie);
    		System.out.println("You won " + won + " times, lost " + lost + " times, and tied " + tied + " times.");
    		return;
    	}
		} //end of do
		while (1 > 0);

    }//end of main
}

Here is the output I got from 2 different trial runs :


Trial 1:
1 for Rock, 2 for Paper, 3 for Scissors (anything else to quit):
1
You picked option 1, the computer picked option 1. It is a tie
1 for Rock, 2 for Paper, 3 for Scissors (anything else to quit):
1
You picked option 1, the computer picked option 2. You lose
1 for Rock, 2 for Paper, 3 for Scissors (anything else to quit):
3
You won 0 times, lost 1 times, and tied 1 times.

Process completed.

Trial 2:
1 for Rock, 2 for Paper, 3 for Scissors (anything else to quit):
1
You picked option 1, the computer picked option 2. You lose
1 for Rock, 2 for Paper, 3 for Scissors (anything else to quit):
1
You picked option 1, the computer picked option 1. It is a tie
1 for Rock, 2 for Paper, 3 for Scissors (anything else to quit):
2
You picked option 2, the computer picked option 2. It is a tie
1 for Rock, 2 for Paper, 3 for Scissors (anything else to quit):
3
You picked option 3, the computer picked option 1. You lose
1 for Rock, 2 for Paper, 3 for Scissors (anything else to quit):
2
You picked option 2, the computer picked option 2. It is a tie
1 for Rock, 2 for Paper, 3 for Scissors (anything else to quit):
3
You won 0 times, lost 2 times, and tied 3 times.

Process completed.


It seems like my loop is exiting at random times. Im wondering if someone can guide me through this.

Cheers,

Recommended Answers

All 2 Replies

You are somewhat right that it is exiting at "random" times - it's your generator for the computer choice that is causing the exit. The nextInt(int) function is specified

Returns a pseudorandom, uniformly distributed int value between 0 (inclusive) and the specified value (exclusive),

So it is giving you a value from 0 to 2. You don't account for a value of 0 in your if() statements, so it will fall into your else() block where you are exiting.

Note that the computer will also never choose Scissors with that generator either.

Knowing why it's exiting, you should be able to figure out how to fix it quite easily now.

You are somewhat right that it is exiting at "random" times - it's your generator for the computer choice that is causing the exit. The nextInt(int) function is specified So it is giving you a value from 0 to 2. You don't account for a value of 0 in your if() statements, so it will fall into your else() block where you are exiting.

Note that the computer will also never choose Scissors with that generator either.

Knowing why it's exiting, you should be able to figure out how to fix it quite easily now.

Thanks for the help, I've fixed it now.

Cheers,

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.