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 GAME.

  1. #1
    Junior Member
    Join Date
    Dec 2013
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default ROCK, PAPER, SCISSORS GAME.

    I would like to know what could be possibly wrong with my code since I get feedback that "my code returned 'paper wins' instead of 'scissors wins' when the inputs are scissors and paper"

    Below is my code. Thanks for the help.

    var userChoice = prompt("Do you choose rock, paper or scissors?");
    var computerChoice = Math.random();
    if (computerChoice < 0.34) {
    computerChoice = "rock";
    } else if(computerChoice <= 0.67) {
    computerChoice = "paper";
    } else {
    computerChoice = "scissors";
    }

    compare(userChoice,computerChoice);

    var compare = function (choice1,choice2)
    {

    if (choice1 === choice2)
    {
    return ("The result is a tie!");
    }

    if (choice1 === "rock")
    {
    if (choice2 === "scissors")
    return ("Rock wins");

    else
    {
    return ("Paper wins");
    }
    }
    if (choice1 === "paper")
    {
    if (choice2 === "rock")
    return ("paper wins");

    else
    {
    return ("scissors wins");
    }
    }
    if (choice1 === "paper")
    {
    if (choice2 === "scissors")
    return ("Scissors wins");

    else
    {
    return ("rock wins");
    }
    }
    if (choice1 === "scissors")
    {
    if (choice2 === "rock")
    return ("rock wins");
    else
    {
    return ("paper wins");
    }
    }
    if (choice1 === "scissors")
    {
    if (choice2 === "paper")
    return ("scissors wins");

    else
    {
    return ("rock wins");
    }
    }
    };


  2. #2
    Junior Member
    Join Date
    Dec 2013
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: ROCK, PAPER, SCISSORS GAME.

    When comparing strings with you have to use the .equals method in this case it would be
    if(choice1.equals("scissors")){}

    and etc.
    I'm fairly sure that's your problem

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    6,287
    My Mood
    Mellow
    Thanks
    252
    Thanked 777 Times in 762 Posts

    Default Re: ROCK, PAPER, SCISSORS GAME.

    === ?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    22,265
    Thanks
    56
    Thanked 2,372 Times in 2,343 Posts

    Default Re: ROCK, PAPER, SCISSORS GAME.

    javascript vs java?
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Creating a GUI for a rock, paper, scissors game
    By nmg13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 11th, 2013, 02:09 PM
  2. Rock paper scissors program not displaying winner
    By namenamename in forum What's Wrong With My Code?
    Replies: 2
    Last Post: October 5th, 2013, 04:38 AM
  3. Replies: 4
    Last Post: February 15th, 2013, 04:19 PM
  4. Help Improve My Rock,Paper,Scissors
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 3
    Last Post: December 16th, 2011, 10:34 PM
  5. Rock paper scissors project
    By katie_gsu in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2011, 02:34 PM