Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 9 of 9

Thread: What is wrong with my code?

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default What is wrong with my code?

    I am making a rock,paper,scissors,lizard,spock java program for one of my classes. Ive got it all done, except when I was testing it, I noticed choice 4 and 5(LIZARD and SPOCK) dont do anything unless its a tie. I dont see what is wrong with my code, so if someone else can see it and point it out to me, much appreciated! Here is my code:

    import java.util.*;
     
    public class RockPaperScissors {
     
        private final static int ROCK = 1;
        private final static int PAPER = 2;
        private final static int SCISSORS = 3;
        private final static int LIZARD = 4;
        private final static int SPOCK = 5;
     
        public static void main(String[] args) {
     
            Random rand = new Random();
     
            int userScore = 0;
            int computerScore = 0;
            int userPlay = 1;
            int computerPlay;
     
            Scanner key = new Scanner(System.in);
     
            System.out.println("Get ready to play the game of Rock, Paper,"
                    + "Scissors, Lizard, Spock!");
            System.out.println("The Rules are as followed: ");
            System.out.println("User will choose their gesture and the computer will"
                    + "choose a random gesture. Whoever wins gets a point!\n");
            System.out.print("*Scissors cut Paper*\n" + "*Paper covers Rock*\n" 
                    + "*Rock crushes Lizard*\n" + "*Lizard poisons Spock*\n"
                    + "*Spock melts Scissors*\n" + "*Scissors decapitate Lizard*\n"
                    + "*Paper disproves Spock*\n" + "*Spock vaporizes Rock*" 
                    + "*Rock crushes Scissors*\n\n");
     
            while (userPlay != 0) {
     
                System.out.println("Choose 0 if you want to quit the game.");
                System.out.println("Choose 1 for Rock, 2 for Paper, 3 for Sciscors, "
                        + "4 for Lizard, or 5 for Spock");
                userPlay = key.nextInt();
                computerPlay = (rand.nextInt(5) + 1);
     
                //Users Choice of Gesture.
                if (userPlay == 1) {
                    userPlay = ROCK;
                } else if (userPlay == 2) {
                    userPlay = PAPER;
                } else if (userPlay == 3) {
                    userPlay = SCISSORS;
                } else if (userPlay == 4) {
                    userPlay = LIZARD;
                } else if (userPlay == 5) {
                    userPlay = SPOCK;
                }
     
                //Computers choice of Gesture.
                if (computerPlay == 1) {
                    computerPlay = ROCK;
                } else if (computerPlay == 2) {
                    computerPlay = PAPER;
                } else if (computerPlay == 3) {
                    computerPlay = SCISSORS;
                } else if (computerPlay == 4) {
                    computerPlay = LIZARD;
                } else if (computerPlay == 5) {
                    computerPlay = SPOCK;
                }
     
                if (computerPlay == ROCK && userPlay == SCISSORS) {
                    System.out.println("You chose Scissors");
                    System.out.println("Computer chose Rock! You Lose\n\n");
                    computerScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == ROCK && userPlay == PAPER) {
                    System.out.println("You chose Scissors");
                    System.out.println("Computer chose Rock you Win\n\n");
                    userScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == PAPER && userPlay == SCISSORS) {
                    System.out.println("You chose Scissors");
                    System.out.println("Computer chose Scissor you Win\n\n");
                    userScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == PAPER && userPlay == ROCK) {
                    System.out.println("You chose Rock");
                    System.out.println("Computer chose Paper you Lose\n\n");
                    computerScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == SCISSORS && userPlay == ROCK) {
                    System.out.println("You chose Rock");
                    System.out.println("Computer chose Scissors you Win\n\n");
                    userScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == SCISSORS && userPlay == PAPER) {
                    System.out.println("You chose Paper");
                    System.out.println("Computer chose Scissors you Lose\n\n");
                    computerScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == LIZARD && userPlay == ROCK) {
                    System.out.println("You chose Rock");
                    System.out.println("Computer chose Lizard you Win\n\n");
                    userScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == LIZARD && userPlay == SPOCK) {
                    System.out.println("You chose Spock");
                    System.out.println("Computer chose Lizard you Lose\n\n");
                    computerScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == LIZARD && userPlay == SCISSORS) {
                    System.out.println("You chose Scissors");
                    System.out.println("Computer chose Lizard you Win\n\n");
                    userScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == LIZARD && userPlay == PAPER) {
                    System.out.println("You chose Paper");
                    System.out.println("Computer chose Lizard you Lose\n\n");
                    computerScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == SPOCK && userPlay == ROCK) {
                    System.out.println("You chose Rock");
                    System.out.println("Computer chose Spock you Lose\n\n");
                    computerScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == SPOCK && userPlay == PAPER) {
                    System.out.println("You chose Paper");
                    System.out.println("Computer chose Spock you Win\n\n");
                    userScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == SPOCK && userPlay == SCISSORS) {
                    System.out.println("You chose Scissors");
                    System.out.println("Computer chose Spock you Lose");
                    computerScore++;
                    System.out.println("Current Score: "
                            + computerScore + " to " + userScore + "\n\n");
                }
                if (computerPlay == userPlay) {
                    System.out.println("TIE");
     
                }
            }
            if (userPlay == 0) {
                if (computerScore > userScore) {
                    System.out.println("Computer wins. Final Score: "
                            + computerScore + " to " + userScore);
                } else if (computerScore < userScore) {
                    System.out.println("You beat the Computer! Final Score: "
                            + userScore + " to " + computerScore);
                } else if (computerScore == userScore) {
                    System.out.println("You Tied the Computer! Final Score: "
                            + userScore + " to " + computerScore);
                }
     
     
            }
        }
    }
    Last edited by connorlm3; January 28th, 2013 at 04:51 PM. Reason: warpped code


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    Can you copy the console window for when you execute the program and paste it here?
    Add some comments to it showing where the output is wrong and say what the output should be.


    Have you made a spread sheet of ALL possible combinations of user and computer plays? A grid with user values as rows and computer value as columns. Or the other way. There would be 25 -5 (matches) or 20 combos to test.


    On windows: To copy the contents of the command prompt window:
    Click on Icon in upper left corner
    Select Edit
    Select 'Select All' - The selection will show
    Click in upper left again
    Select Edit and click 'Copy'

    Paste here.

    Please edit your post and wrap your code with
    [code=java]
    <YOUR CODE HERE>
    [/code]
    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    Here is my output when I just tested. sometimes 5(SPOCK) will work, but 4(LIZARD) isnt working. Its making no sense to me.

    run:
    Get ready to play the game of Rock, Paper,Scissors, Lizard, Spock!
    The Rules are as followed:
    User will choose their gesture and the computer willchoose a random gesture. Whoever wins gets a point!

    *Scissors cut Paper*
    *Paper covers Rock*
    *Rock crushes Lizard*
    *Lizard poisons Spock*
    *Spock melts Scissors*
    *Scissors decapitate Lizard*
    *Paper disproves Spock*
    *Spock vaporizes Rock**Rock crushes Scissors*

    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    1
    You chose Rock
    Computer chose Lizard you Win


    Current Score: 0 to 1


    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    2
    You chose Paper
    Computer chose Spock you Win


    Current Score: 0 to 2


    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    3
    You chose Scissors
    Computer chose Rock! You Lose


    Current Score: 1 to 2


    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Scissors, 4 for Lizard, or 5 for Spock // should be like the other ones, user chose 4
    4 //vs computer chose # 1-5
    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock //same here not working like the other #s
    5
    You chose Spock
    Computer chose Lizard you Lose


    Current Score: 2 to 2


    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    5
    TIE
    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    5
    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    5
    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    5
    TIE
    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    4
    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    4
    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    4
    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock
    4
    Choose 0 if you want to quit the game.
    Choose 1 for Rock, 2 for Paper, 3 for Sciscors, 4 for Lizard, or 5 for Spock

    --- Update ---

    Ok tested some more. Eventually I get a tie when i enter 4, so computerPlay = 4(LIZARD) and userPlay = 4(LIZARD) works, also the only time i have gotten 5 to work so far is when computerPlay = 4(LIZARD) and userPlay = 5(SPOCK). So confused why those 2 are working but the rest arent?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    Can you add comments to the output to show where the problem is and to show what the output should be?

    For debugging you should print out the value of the computer's play.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    I dont know what else to say about the output. no matter than the user chooses or the computer chooses when it comes to 1 , 2 , and 3. everything works. Its just a problem with 4 and 5 somehow?

    --- Update ---

    Could anyone run my code and look at the output for me? its too much to put on here because it just repeats.

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    Did you see this:
    Have you made a spread sheet of ALL possible combinations of user and computer plays? A grid with user values as rows and computer value as columns. Or the other way. There would be 25 -5 (matches) or 20 combos to test.

    Have you done this:
    For debugging you should print out the value of the computer's play.


    Where in the print out that you posted is the program not doing what you want it to do? What was the computer's play when that happened?
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    I put code in to make it print the computers play in every single instance, it didnt work for the 4 and 5 cases except on ties.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: What is wrong with my code?

    Have you made a spread sheet on paper of ALL possible combinations of user and computer plays? A grid with user values as rows and computer value as columns. Or the other way. There would be 25 -5 (matches) or 20 combos to test plus one for the tie.

    How many if tests does your program have? Which ones are missing?
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Junior Member
    Join Date
    Jan 2013
    Posts
    29
    Thanks
    9
    Thanked 0 Times in 0 Posts

    Default Re: What is wrong with my code?

    Gahhhh I didn't check that. I think im missing some test cases. Im going to write them all out to see what im missing. If i need more help I will post. Thanks!

Similar Threads

  1. what is wrong with my code???
    By koolestkid20 in forum What's Wrong With My Code?
    Replies: 6
    Last Post: November 25th, 2012, 01:12 PM
  2. What is wrong with my code?
    By dannyboi in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 23rd, 2012, 02:40 PM
  3. What is wrong with my code?
    By unleashed-my-freedom in forum What's Wrong With My Code?
    Replies: 5
    Last Post: July 3rd, 2012, 12:41 AM
  4. can someone tell me what's wrong with my code
    By Nate08 in forum What's Wrong With My Code?
    Replies: 7
    Last Post: February 23rd, 2012, 05:07 PM
  5. what is wrong in this code
    By rk.kavuri in forum What's Wrong With My Code?
    Replies: 2
    Last Post: March 6th, 2011, 03:13 PM