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 4 of 4

Thread: rock paper scissors program giving wrong results

  1. #1
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default rock paper scissors program giving wrong results

    Here is my code:

    import java.util.*;

    class rps{
    public static void main(String args[]){
    Random number = new Random();
    Scanner input = new Scanner(System.in);

    // player chooses
    String choice;

    System.out.println("Enter your throw (rock, paper, scissors)(enter exactly as shown): ");

    choice = input.nextLine();

    System.out.println("The player throws "+choice);

    // computer chooses
    int integer;
    String computerinput;
    integer = 1+number.nextInt(3);
    if(integer == 1){
    computerinput = "rock";
    }else if(integer == 2){
    computerinput = "paper";
    }else{
    computerinput = "scissors";
    }

    String message = "The computer throws "+ computerinput;
    System.out.println(message);

    // determining winner...
    boolean isTie = computerinput.equals(choice);
    boolean isWin = (computerinput.equals("rock") && choice.equals("scissor")) ||
    (computerinput.equals("paper") && choice.equals("rock")) ||
    (computerinput.equals("scissor") && choice.equals("paper"));

    //message
    if (isWin){
    message += ". Computer Won.";
    }else if (isTie){
    message += " too. It is a Tie";
    }else{
    message += ". You Won";
    }

    System.out.print("\n"+message);


    }
    }

    Output:

    Enter your throw (rock, paper, scissors)(enter exactly as shown):
    paper
    The player throws paper
    The computer throws scissors

    The computer throws scissors. You Won

    It should have said "Computer won." not "You won".


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: rock paper scissors program giving wrong results

    You need to check your logic when assigning to the isWin boolean. Also who does the isWin boolean apply to, the AI or the human?
    Improving the world one idiot at a time!

  3. #3
    Member
    Join Date
    Oct 2013
    Posts
    30
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: rock paper scissors program giving wrong results

    the isWin boolean is simply a variable that stores all the possible ways to win the game

    --- Update ---

    nvm I found my problem.
    now how do you delete this thread?

  4. #4
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,511
    My Mood
    Grumpy
    Thanks
    0
    Thanked 165 Times in 156 Posts

    Default Re: rock paper scissors program giving wrong results

    You should post your solution so others can learn from it. So there is no need to delete the thread (even if you could).
    Improving the world one idiot at a time!

Similar Threads

  1. Replies: 4
    Last Post: February 15th, 2013, 03:19 PM
  2. Help Improve My Rock,Paper,Scissors
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 3
    Last Post: December 16th, 2011, 09:34 PM
  3. Rock paper scissors project
    By katie_gsu in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2011, 01:34 PM
  4. rock paper scissor. whats wrong here ??
    By bjorn10 in forum Loops & Control Statements
    Replies: 1
    Last Post: November 10th, 2011, 08:13 AM
  5. Rock Paper Scissors Spock Lizard Player Problem
    By flyingcurry in forum What's Wrong With My Code?
    Replies: 1
    Last Post: March 3rd, 2011, 09:53 AM