-3

I am trying to get the "press q to quit" function to work properly, but I'm having some trouble. The "invalid Character" function is also not working properly. How would I go about fixing this? My professor suggested putting the "press q to quit" function in the beginning of my code, but hasn't given me any further instruction.

public static void main(String[] args) {

    char userChar = 0;

    Scanner sc = new Scanner(System.in);
    Random rnd = new Random();

    // Intro/directions/prompting for user input
    System.out.println("Welcome to Rock, Paper, Scissors by Rancid!");
    System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, then press Enter: ");

    // If player chooses to quit
    if (userChar == 'q' || userChar == 'Q') {
        System.out.println("Player chose to quit. Goodbye!");
    }

    // Start of loop
    while (userChar != 'q' && userChar != 'Q') {

        // Prompting computer to generate a random number
        int randomNumber = rnd.nextInt(3) + 1;

        // If computer generates 1 (Rock)
        if (randomNumber == 1) {
            if (userChar == 'r' || userChar == 'R') {
                System.out.println("Rock vs. Rock! It's a tie!");
            } else if (userChar == 'p' || userChar == 'P') {
                System.out.println("Paper covers Rock, you win!");
            } else if (userChar == 's' || userChar == 'S') {
                System.out.println("Rock breaks Scissors, you lose!");
            }
        }

        // If computer generates 2 (Paper)
        else if (randomNumber == 2) {
            if (userChar == 'r' || userChar == 'R') {
                System.out.println("Paper covers Rock, you lose!");
            } else if (userChar == 'p' || userChar == 'P') {
                System.out.println("Paper vs. Paper! It's a tie!");
            } else if (userChar == 's' || userChar == 'S') {
                System.out.println("Scissors cuts Paper, you win!");
            }
        }

        // If computer generates 3 (Scissors)
        else if (randomNumber == 3) {
            if (userChar == 'r' || userChar == 'R') {
                System.out.println("Rock breaks Scissors, you win!");
            } else if (userChar == 'p' || userChar == 'P') {
                System.out.println("Scissors cuts Paper, you lose!");
            } else if (userChar == 's' || userChar == 'S') {
                System.out.println("Scissors vs. Scissors! It's a tie!");
            }
        }

        // If player types an invalid character
        else {
            System.out.println("Invalid input! Please enter a valid character.");
        }

        userChar = sc.next().charAt(0);
        }
    }
}
AS Mackay
  • 2,831
  • 9
  • 19
  • 25
  • 1
    Define "not working properly". Consider this a great time to familiarize yourself with the use of a debugger. With a debugger you can step through the code line by line as it executes and observe the runtime values and behaviors. When you do this, on which specific line of code does a problem first occur? Does a specific operation produce an unexpected result? What was the result? What result was expected? Why? – David Mar 27 '19 at 11:54

2 Answers2

0

Well, firstly your condition to quit is outside of scope of while loop which means it will never execute in this given context.

Secondly, condition in your while loop will not be true unless userChar is not q and Q simultaneously, which doesn't make sense.

What I suggest is to refactor your while loop little bit, as such:

while (true) {

    //Your logic here

    if (userChar == 'q' || userChar == 'Q') {
        System.out.println("Player chose to quit. Goodbye!");
        break;
    }


    //Your logic here
}
Samuel.P
  • 38
  • 11
0

In order to really take care of invalid input you need to do it first thing in the loop. Moreover you can't do it in the else block of your if-else statements because in those if-else you check the randomNumber, not the input, so it doesn't make sense and it will never work. In addition you check the input as what you get from sc.next().charAt(0) but for example if the user insert a string like "R_invalid_something_without_space" you only look at the first letter which is R and you take it as a valid input. I modified your code so that you read a line in each loop iteration and if this line is not q / r / p / s it will consider as invalid input and if its q the program will finish.

 public static void main(String[] args) {

    char userChar = 0;

    Scanner sc = new Scanner(System.in);
    Random rnd = new Random();

    // Intro/directions/prompting for user input
    System.out.println("Welcome to Rock, Paper, Scissors by Rancid!");
    System.out.println("Choose R for Rock, P for Paper, S for Scissors, or Q to Quit, then press Enter: ");

    // Start of loop
    while (true) {
        // what I changed
        // **********************************************************
        String line = sc.nextLine().toLowerCase();
        if(line.equals("q")){
            System.out.println("Player chose to quit. Goodbye!");
            break;
        }
        if(!line.equals("r") && !line.equals("s") && !line.equals("p")) {
            System.out.println("Invalid input! Please enter a valid character.");
            continue;
        }
        userChar = line.charAt(0);
        // **********************************************************

        // Prompting computer to generate a random number
        int randomNumber = rnd.nextInt(3) + 1;

        // If computer generates 1 (Rock)
        if (randomNumber == 1) {
            if (userChar == 'r' || userChar == 'R') {
                System.out.println("Rock vs. Rock! It's a tie!");
            } else if (userChar == 'p' || userChar == 'P') {
                System.out.println("Paper covers Rock, you win!");
            } else if (userChar == 's' || userChar == 'S') {
                System.out.println("Rock breaks Scissors, you lose!");
            }
        }

        // If computer generates 2 (Paper)
        else if (randomNumber == 2) {
            if (userChar == 'r' || userChar == 'R') {
                System.out.println("Paper covers Rock, you lose!");
            } else if (userChar == 'p' || userChar == 'P') {
                System.out.println("Paper vs. Paper! It's a tie!");
            } else if (userChar == 's' || userChar == 'S') {
                System.out.println("Scissors cuts Paper, you win!");
            }
        }

        // If computer generates 3 (Scissors)
        else if (randomNumber == 3) {
            if (userChar == 'r' || userChar == 'R') {
                System.out.println("Rock breaks Scissors, you win!");
            } else if (userChar == 'p' || userChar == 'P') {
                System.out.println("Scissors cuts Paper, you lose!");
            } else if (userChar == 's' || userChar == 'S') {
                System.out.println("Scissors vs. Scissors! It's a tie!");
            }
        }
    }
}
Omri Attiya
  • 3,917
  • 3
  • 19
  • 35