I am creating a Rock, Paper, Scissors game to practise my Java skills. But right now, I am facing a problem which I cannot fix. When I ask the user to enter something, they can't enter it and it goes straight to the if statement after it.
Code: package rps_game;
// Rock, Paper, Scissors game
import java.util.Scanner;
import java.util.Random;
import java.util.*;
public class rock_paper_scissors {
public static void main(String args[]){
Scanner input = new Scanner(System.in);
Random rand = new Random();
String choices[] = {"rock", "paper", "scissors"};
int winRounds = 0;
boolean running = true;
int rounds = 0;
int wins = 0;
int loses = 0;
String yourChoice="";
String compChoice="";
while (running = true){ // main loop
try{
System.out.println("Enter the amount of rounds you want to play: ");
rounds = input.nextInt(); // gets input
winRounds = (rounds/2)+1;
System.out.println("You are playing best of " + winRounds + " out of " + rounds);
running = false; // breaks off from loop
}catch(Exception e){ // if error pops up
System.out.println("You DID NOT enter a WHOLE NUMBER.");
// still runs b/c criteria has not been met.
}
while (wins < winRounds && loses < winRounds){
// problem arises here
System.out.println("Enter either Rock, Paper or Scissors: ");
yourChoice = input.nextLine();
input.nextLine();
yourChoice.toLowerCase();
if (yourChoice.equals(Arrays.asList(choices).contains(yourChoice))){ // if array contains what use entered
break;
}else{
System.out.println("You did not enter either Rock, Paper or Scissors.");
running = false; // exit program
}
compChoice = choices[rand.nextInt(choices.length)];
System.out.println(compChoice);
}
}
}
}
I have not finished it yet, but what is happening?