Reputation: 9
I had to make a rock paper scissor game that loops 5 times and additional if you input an invalid number. I haven't been able to get it to loop enough if wrong, whenever I put an incorrect variable, it does loop additional times, but way too many times, and I only need 5 correct times. It also wont count the amount of player and computer scores. No matter how many each opponent gets right, it only displays Player Wins: 1 and Computer Win: 1. Please help! Here's my code
import java.util.*;
public class bmahipat_C5Lab1 {
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 = -1;
}
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 = +1;
} else if(computer == scissors) {
System.out.println ("Player picked Rock, Computer picked Scissors, Player wins");
playerScore = +1;
}
} else if (player == paper) {
if(computer == rock){
System.out.println ("Player picked Paper, Computer picked Rock , Player wins");
playerScore = +1;
} else if(computer == scissors) {
System.out.println ("Player picked Paper, Computer picked Scissors, Computer wins");
computerScore = +1;
}
} else if (player == scissors) {
if(computer == rock) {
System.out.println ("Player picked Scissors, Computer picked Rock , Computer wins");
computerScore = +1;
} else if(computer == paper) {
System.out.println ("Player picked Scissors, Computer picked Paper, Player wins");
playerScore = +1;
}
}
}
System.out.println("");
System.out.println("Computer Wins " + computerScore);
System.out.println("Player Wins " + playerScore);
}
}
Upvotes: 0
Views: 745
Reputation: 2598
There is a big difference between
computerScore =+ 1; // This is computerScore = (+1) So it will be always 1
and
computerScore += 1; // this is equals to computerScore=computerScore+1;
and the same logic for
i = -1;
Upvotes: 1