Hello. I am trying to make a rock paper scissors program for a school project. I have the code is written and the main text parts etc works, However, the actual game does not work. The computer is supposed to choose between rock, paper and scissors, however, the user's choice is only printed and only asks if the user would like to play again. How do I make the computer choose one?
// Code Written By Sherwin Gazor October 16 2020 import java.util.Scanner; import java.util.Random; public class GameRPS1 { public static void main ( String[] args ) { String getUserInput; Scanner sc =new Scanner(System.in); //Scanner gets created Random generator = new Random(); //Random gets created System.out.println("Welcome to Rock, Paper, Scissors! Do you want to play?"); // Welcome message getUserInput =sc.nextLine(); // Asks user if they would like to play while(getUserInput.equalsIgnoreCase("y"))//Removes case sensitives { int randomGenForPCChoice = generator.nextInt(3)+1; //Computer's chooses one of the three ` System.out.println("Computer has made a choice. Make your choice!: "); System.out.println("Enter:Rock,Paper or Scissors"); getUserInput=sc.nextLine(); //Asks you for choice`` String getUserInput1=getUserInput.toUpperCase(); char firstletter=getUserInput1.charAt(0); if(randomGenForPCChoice == 1) { System.out.println (" Computer chose rock!"); // If the computer chooses rock this will print } if(randomGenForPCChoice == 2) { System.out.println (" Computer chose paper!"); // If the computer chooses paper this will print } if(randomGenForPCChoice == 3) { System.out.println (" Computer chose scissors!"); // If the computer chooses scissors this will print } if switch(firstletter) { case 'R': System.out.println(" You chose rock! "); break; case 'P': System.out.println(" You chose paper! "); break; case 'S': System.out.println(" You chose scissors! "); break; } System.out.println("Enter Y/y if you want to continue,otherwise N/n to stop the game"); getUserInput =sc.nextLine(); // If you type this the game will stop } System.out.println("Game over! Thanks for playing!"); // Message appears after you quit } }
Console :
Welcome to rock paper scissors! Do you want to play?
Y // Yes
Computer made a choice. Make your choice:
Enter:Rock,Paper or Scissors
Rock // I choose rock
You chose Rock // Only prints that I chose rock
// Does not print what the computer chose and thus I can not make it so that if the computer chose X then X will happen.
Enter Y/y if you want to continue,otherwise N/n to stop the game
4 warnings found:
File: C:\Users\Joe\Desktop\Computer Science\GameRPS1.java [line: 11]
Warning: The local variable rock is never read
File: C:\Users\Joe\Desktop\Computer Science\GameRPS1.java [line: 11]
Warning: The local variable paper is never read
File: C:\Users\Joe\Desktop\Computer Science\GameRPS1.java [line: 11]
Warning: The local variable scissors is never read
File: C:\Users\Joe\Desktop\Computer Science\GameRPS1.java [line: 13]
Warning: The local variable randomGenForPCChoice is never read