Reputation: 9
I had to make a rock paper scissors game, where you play against the computer. 1,2,3 are used for rock, paper, scissors.It loops 5 times, and shows the amount of computer wins and player wins. I got everything to work, except for the looping. When you put a number higher than 3, it's suppose to say "Invalid" and loop until you play 5 valid games. However if you you put an odd amount of wrong answers, it makes you play 6 games, and if you put an even amount of wrong answers, it makes you play 5 games. I need it to make you play 5 games regardless of odd and even amounts wrong. Please help.
public static void main(String[] args) {
Scanner reader = new Scanner(System.in);
int player = 0, computer = 0;
int computerScore = 0, playerScore = 0;
int loops = 0;
int rock = 1;
int paper = 2;
int scissors = 3;
for (int i = 1; i < 6; i++) {
computer = (int) (Math.random() * 3) + 1;
System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
player = reader.nextInt();
if (player > scissors) {
System.out.println("Not a valid response");
System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
player = reader.nextInt();
i--;
}
if (player == computer) {
System.out.println("Tie");
} else if (player == rock) {
if (computer == paper) {
System.out.println("Player picked Rock, Computer picked Paper, Computer wins");
computerScore++;
} else if (computer == scissors) {
System.out.println("Player picked Rock, Computer picked Scissors, Player wins");
playerScore++;
}
} else if (player == paper) {
if (computer == rock) {
System.out.println("Player picked Paper, Computer picked Rock , Player wins");
playerScore++;
} else if (computer == scissors) {
System.out.println("Player picked Paper, Computer picked Scissors, Computer wins");
computerScore++;
}
} else if (player == scissors) {
if (computer == rock) {
System.out.println("Player picked Scissors, Computer picked Rock , Computer wins");
computerScore++;
} else if (computer == paper) {
System.out.println("Player picked Scissors, Computer picked Paper, Player wins");
playerScore++;
}
}
}
System.out.println("");
System.out.println("Computer Wins " + computerScore);
System.out.println("Player Wins " + playerScore);
}
Upvotes: 0
Views: 200
Reputation: 1590
To make things easier, I suggest using a while loop.
E.G:
Scanner reader = new Scanner (System.in);
int computerScore = 0, playerScore = 0;
int rock = 1;
int paper = 2;
int scissors = 3;
int numberOfPlayerTurns = 0;
final int MAX_PLAYER_TURNS = 5;
while (numberOfPlayerTurns < MAX_PLAYER_TURNS)
{
int computer = (int) (Math.random() * 3) + 1;
int player = reader.nextInt();
System.out.println("Enter 1 for Rock, 2 for Paper, 3 for Scissors");
// invalid
if (player > scissors || player <= 0)
{
System.out.println("Not a valid response");
}
else
{
numberOfPlayerTurns++;
if (player == computer)
{
System.out.println("Tie");
}
else if (player == rock )
{
if(computer == paper)
{
System.out.println ("Player picked Rock, Computer picked Paper, Computer wins");
computerScore++;
}
else
{
System.out.println ("Player picked Rock, Computer picked Scissors, Player wins");
playerScore++;
}
}
else if (player == paper)
{
if(computer == rock)
{
System.out.println ("Player picked Paper, Computer picked Rock , Player wins");
playerScore++;
}
else
{
System.out.println ("Player picked Paper, Computer picked Scissors, Computer wins");
computerScore++;
}
}
// scissors
else
{
if(computer == rock)
{
System.out.println ("Player picked Scissors, Computer picked Rock , Computer wins");
computerScore++;
}
else
{
System.out.println ("Player picked Scissors, Computer picked Paper, Player wins");
playerScore++;
}
}
}
}
System.out.println("");
System.out.println("Computer Wins " + computerScore);
System.out.println("Player Wins " + playerScore);
Upvotes: 1