0

I am having so much trouble with something that shouldn't be so hard. I can't get my program to playagain correctly. I would appreciate the help, here's the code. Also this is my first post, it may look wierd.

package rpsgame;

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

    public class RockPaperScissors {
        public static void main(String[] args) {
            //variables
            final int ROCK = 0;
            final int PAPER = 1;
            final int Scissors = 2;
            int played = 0;
            boolean playing = true;

            Scanner scan = new Scanner(System.in);
            Random rng = new Random();

            //User and Computer input
            int compChose;
            int youChose;

            //Program
            System.out.println("=:=:=| Hello I'm Compy! Let's Play Rock-Paper-Scissors!|=:=:=");
            //System.out.println("Here are the rules.\n" + "Rock beats Scissors.\n" + "Scissors beat Paper.\n" + "Paper beats Rock.");
            while (playing == true) {

                while (played < 6) {


                    //Ask for some input
                    System.out.println("Rock = 0\n" + "Paper = 1\n" + "Scissors = 2\n" + "Please enter your choice.\n");

                    //Get numbers 0 , 1, or 2.
                    youChose = scan.nextInt();

                    //case for invalid choice.
                    if (youChose < 0 || youChose > 3) {
                        System.out.println("Invalid choice, Game Over.");
                        System.exit(0);
                    }
                    //Generate computers choice
                    compChose = rng.nextInt(3);
                    //Draws
                    if (youChose == compChose) {
                        if (youChose == 0) {
                            System.out.println("Both players chose rock!\n" + "It's a tie!");
                        } else if (youChose == 1) {
                            System.out.println("Both playes chose paper!\n" + "It's a tie!");
                        } else {
                            System.out.println("Both players chose Scissors!\n" + "It's a tie!");
                        }
                        played++;
                    }
                    //Rock
                    if (youChose == 0) {
                        if (compChose == 1) {
                            System.out.println("You chose rock; Compy chose paper.");
                            System.out.println("Compy Wins!\n");
                        } else {
                            System.out.println("You chose rock; Compy chose scissors.");
                            System.out.println("You win!\n");
                        }
                        played++;
                    }
                    //Paper
                    else if (youChose == 1) {
                        if (compChose == 0) {
                            System.out.println("You chose paper; Compy chose rock.");
                            System.out.println("Compy Wins\n");
                        } else {
                            System.out.println("You chose paper; Compy chose scissors.");
                            System.out.println("Compy wins!\n");
                        }
                        played++;
                    }
                    //Scissors
                    else    // User chooses scissors
                    {
                        if (compChose == 0) {
                            System.out.println("You chose scissors; Compy chose rock.");
                            System.out.println("Compy wins!\n");
                        } else {
                            System.out.println("You chose scissors; Compy chose paper.");
                            System.out.println("You win!\n");
                        }
                        played++;
                    }
                    if (played > 6) {
                        System.out.println("Do you want to play again? (Yes/No)");
                        String playagain = scan.nextLine();
                        if (playagain == "yes") {
                            playing = true;
                        } else {
                            playing = false;
                        }
                    }
                }
            }
        }
    }
}
Michael Queue
  • 1,340
  • 3
  • 23
  • 38
Effy
  • 1
  • Don't compare Strings using `==` or `!=`. Use the `equals(...)` or the `equalsIgnoreCase(...)` method instead. Understand that `==` checks if the two *object references* are the same which is not what you're interested in. The methods on the other hand check if the two Strings have the same characters in the same order, and that's what matters here. – Hovercraft Full Of Eels Nov 14 '15 at 04:46
  • So instead of `if (playagain == "yes")` use `if ("yes".equalsIgnoreCase(playagain))` – Hovercraft Full Of Eels Nov 14 '15 at 04:47
  • Thank you so much for clearing up that misconception. I am just curious why It throws an exception that say " else without if at line 118. – Effy Nov 14 '15 at 05:01
  • I've no idea, and you have me at a disadvantage: Which line is `line 118`? – Hovercraft Full Of Eels Nov 14 '15 at 05:10
  • Note that your use of indentation is not good, making your code hard to read. A consistent indentation style is not used to make your code look pretty, but rather to help you debug your code by showing precisely which block contains what code. – Hovercraft Full Of Eels Nov 14 '15 at 05:13

0 Answers0