Question
Very new programmer here. With my code, the result comes out to This round is a draw! every time. I think there might be something
Very new programmer here. With my code, the result comes out to "This round is a draw!" every time. I think there might be something wrong with the user input but I'm really not sure. Thanks!
import java.util.Random;
import java.util.Scanner;
public class RockPaperScissors{
static Scanner in = new Scanner(System.in);
public static char getPlayerChoice() {
System.out.print("Rock, Paper or Scissors? ");
return Character.toUpperCase(in.next().charAt(0));
}
private static boolean TESTING_MODE = false;
private static int cycler = 0;
public static char getMove() {
if (TESTING_MODE) {
switch (cycler++ % 3) {
case 0:
return 'r';
case 1:
return 'p';
default: // (case 2)
return 's';
}
}
else {
switch ((int) (Math.random() * 3)) {
case 0:
return 'r';
case 1:
return 'p';
default: // (case 2)
return 's';
}
}
}
public static void main(String[] args) {
System.out.print("How many rounds do you want to play: ");
int rounds = in.nextInt();
int player = 0, computer = 0;
char p, c;
while (player < rounds && computer < rounds) {
p = getPlayerChoice();
c = getMove();
if
//CP wins R
((p == 's' && c == 'r')) {
System.out.println("The computer chose rock, you lose!");
computer++;
} else if
//CP wins P
((c == 'p' && p == 's')) {
System.out.println("The computer chose paper, so you lose!");
computer++;
} else if
//CP wins S
((c == 's' && p == 'p')){
System.out.println("The computer chose scissors, so you lose!");
computer++;
}else if
//player wins CP:S
((p == 'r' && c == 's')){
System.out.println("The computer chose scissors, so you win!");
player++;
}else if
//player wins CP:P
((p == 's' && c == 'p')){
System.out.println("The computer chose paper, so you win!");
player++;
}else if
//player wins CP:R
((p == 'p' && c == 'r')){
System.out.println("The computer chose rock, so you win!");
player++;
} else {
System.out.println("This round was a draw!");
}
}
System.out.printf("Scores -> Computer: %d and You: %d ", computer, player);
if(player == rounds) {
System.out.println("You have won the game!");
} else {
System.out.println("Computer has won the game!");
}
}
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started