Results 1 to 4 of 4
  1. #1
    Doman123 is offline Member
    Join Date
    Oct 2014
    Posts
    1
    Rep Power
    0

    Default New to java, could use some help.

    I have just started learning programming, and i would appreciate if someone would look at this piece of code i have written.

    It is supposed to be a rock, paper, scissors simulator. The problem is that my if statements are not working.


    import java.util.Scanner;

    Java Code:
    public class Main {
    
    	
    	
    	
    	public static void main(String args[]){
    		
    		Scanner scan = new Scanner(System.in);
    		
    		
    		
    		System.out.println("Player 1, pick either Rock, paper or Scissors. ");
    		String player1 = scan.next();
    		System.out.println("You picked " + player1);
    		
    		System.out.println("Player 2, pick either Rock, paper or Scissors. ");
    		String player2 = scan.next();
    		System.out.println("You picked " + player2);
    		System.out.println("Player 1: " + player1 + ", Player 2: " + player2);
    		
    		if(player1 == "Rock" && player2 == "Rock"){
    			System.out.println("Tie!");
    		}
    		if(player1 == "Rock" && player2 == "Paper"){
    			System.out.println("Player 2 wins!");
    		}
    		if(player1 == "Rock" && player2 == "Scissors"){
    			System.out.println("Player 1 Wins!");
    		}
    		if(player1 == "Paper" && player2 == "Rock"){
    			System.out.println("Player 1 Wins!");
    		}
    		if(player1 == "Paper" && player2 == "Paper"){
    			System.out.println("Tie!");
    		}
    		if(player1 == "Paper" && player2 == "Scissors"){
    			System.out.println("Player 2 Wins!");
    		}
    		if(player1 == "Scissors" && player2 == "Rock"){
    			System.out.println("Player 2 Wins!");
    		}
    		if(player1 == "Scissors" && player2 == "Paper"){
    			System.out.println("Player 1 Wins!");
    		}
    		if(player1 == "Scissors" && player2 == "Scissors"){
    			System.out.println("Tie!");
    		}
    		
    		
    		
    	}
    	
    	
    }
    Last edited by Doman123; 10-22-2014 at 12:40 AM.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    20,003
    Rep Power
    33

    Default Re: New to java, could use some help.

    Please edit your post and wrap your code with code tags:
    [code=java]
    YOUR CODE GOES HERE
    [/code]
    to get highlighting and preserve formatting.

    my if statements are not working.
    Use the equals() method to compare String objects, not the == operator.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    12,059
    Rep Power
    26

    Default Re: New to java, could use some help.

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

  4. #4
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: New to java, could use some help.

    I have an exercise for you. And I am not advocating this to fix your code. Just before your first if statement add the following two statements:

    Java Code:
    player1 = player1.intern();
    player2 = player2.intern();
    Why does your program now work! If you can answer that, you will have learned a great
    deal about == and equals and objects in general.

    Other recommendations.

    1. Do what Norm said regarding comparisons.
    2. Use if/else constructs for efficiency.
    3. Consider making your program case independent.
    4. And think about this. What constitutes a tie regardless of input?

    Regards,
    Jim
    Last edited by jim829; 10-22-2014 at 04:50 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •