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,