0

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?

user3092741
  • 69
  • 10
  • 5
    `while (running = true)` this will always result in an infinite loop unless you explicitly `break;` from it. – Jeroen Vannevel Mar 17 '14 at 18:25
  • There are many, many problems in your code. One example: `yourChoice.toLowerCase();` – fge Mar 17 '14 at 18:28
  • To clarify what @fge said, `string.toLowerCas()` *returns* the same string in lower case but doesn't modify the string itself. What you want to do is `yourChoice = yourChoice.toLowerCase()` – DSquare Mar 17 '14 at 18:39

2 Answers2

0

You have to read line String temp = input.nextLine();, and then create another Scanner locally to read int. So that the enter pressed (\n) by user is read as a line, otherwise the enter pressed is being interpreted as another line in the input.nextLine();

Also while loop is infinite loop, you may want to correct that as well.

Sourabh Bhat
  • 1,615
  • 13
  • 20
0

This code corrects your mistakes, though you still have to do a lot of things.

The problems were at firstly, the input scanner. (You need two input scanners)

Secondly, at yourChoice.toLowerCase() , it had to be yourChouice =yourChoice.toLowerCase(); Since Strings are immutable.

Finally at yourChoice.equals(Arrays.asList(choices).contains(yourChoice)). This actually had to be (Arrays.asList(choices).contains(yourChoice) only.

The Try Catch was at the wrong location, thus your entire code was running, see the following code without the problems.

import java.util.Scanner;
import java.util.Random;
import java.util.*;

public class sda {
    public static void main(String args[]) {

        Scanner input = new Scanner(System.in);
        Scanner lineInput = 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

                while (wins < winRounds && loses < winRounds) {
                    System.out
                            .println("Enter either Rock, Paper or Scissors: ");
                    yourChoice = lineInput.nextLine();
                    yourChoice= yourChoice.toLowerCase();
                    if (Arrays.asList(choices).contains(
                            yourChoice)) { // 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);

                }
            } catch (Exception e) { // if error pops up
                System.out.println("You DID NOT enter a WHOLE NUMBER.");
                break;
            }

        }

        input.close();
        lineInput.close();
    }
}
chettyharish
  • 1,556
  • 3
  • 23
  • 39