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

Thread: Rock paper scissors program not displaying winner

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

    Default Rock paper scissors program not displaying winner

    So this is my code so far:

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

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

    // player chooses

    String userinput;


    System.out.println("Enter your throw (rock,paper,scissors)(enter exactly as shown)");
    userinput = input.nextLine();




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

    // 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...
    if(userinput == "rock" && computerinput == "rock"){
    System.out.println("It's a tie");
    }else if(userinput == "rock" && computerinput == "paper"){
    System.out.println("Computer wins");
    }else if(userinput == "rock" && computerinput == "scissors"){
    System.out.println("You win");
    }else if(userinput == "paper" && computerinput == "paper"){
    System.out.println("It's a tie");
    }else if(userinput == "paper" && computerinput == "rock"){
    System.out.println("You win");
    }else if(userinput == "paper" && computerinput == "scissors"){
    System.out.println("Computer wins");
    }else if(userinput == "scissors" && computerinput == "scissors"){
    System.out.println("It's a tie");
    }else if(userinput == "scissors" && computerinput == "rock"){
    System.out.println("Computer wins");
    }else if(userinput == "scissors" && computerinput == "paper"){
    System.out.println("You win");
    }


    }
    }

    Everything works perfectly until it gets to the "determining winners" part. For some reason, the program just ends before it gets to the "determining winners".


  2. #2
    Junior Member
    Join Date
    Sep 2013
    Posts
    6
    My Mood
    Inspired
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Rock paper scissors program not displaying winner

    I'm new to JAVA and am working on the same game. I posted mine on here for help also because I keep ending in a tie. I'm not sure but where you have:
    integer = 1+number.nextInt(3); I think should be integer = randomNumbers.nextInt(3); ...Also its suppose to start with a variable name and I don't think you can use integer as a variable name. But again I'm new to JAVA.

  3. #3
    Member
    Join Date
    Sep 2013
    Posts
    70
    Thanks
    1
    Thanked 13 Times in 13 Posts

    Default Re: Rock paper scissors program not displaying winner

    The issue is the way you are comparing ==. When comparing strings you need to use .equals(String) unless you want to know if the memory reference is identical in which case you would use the ==.

Similar Threads

  1. rock paper scissors program giving wrong results
    By namenamename in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 3rd, 2013, 10:57 PM
  2. Replies: 4
    Last Post: February 15th, 2013, 04:19 PM
  3. Help Improve My Rock,Paper,Scissors
    By Emperor_Xyn in forum Java Theory & Questions
    Replies: 3
    Last Post: December 16th, 2011, 10:34 PM
  4. Rock paper scissors project
    By katie_gsu in forum Loops & Control Statements
    Replies: 1
    Last Post: November 28th, 2011, 02:34 PM
  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, 10:53 AM