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

Thread: Can someone tell me why I keep getting a tie, no matter what I type in?

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

    Default Can someone tell me why I keep getting a tie, no matter what I type in?

    Hey guys I am trying to write this code to play rock, paper, scissors against the computer. No matter what letter I type in it converts it to 0 and also randomly gives the computer 0 and calls it a tie every time I try it.

    /*  
        File Name: rockPaperScissorsGame
    	Programmer: David Wheeler
    	Purpose: Creates the rock, paper, scissor game. 
    */
    import javax.swing.JOptionPane; // Needed for JOption class to recieve input.	
    import java.util.Scanner; // Have to import in order to use the Scanner class.
    import java.util.Random;  // Needed for Random class
    public class rockPaperScissorsGame
    {
    	public static void main (String[] args)
    	{
    		int CT = 0;
    		String user = "0";
     
    		//First thing call the directions funtion to explain the game. 
    		directions();
    		//get the computers choice. 
    		computersTurn(CT);
    		//get the users choice.
    		usersTurn(user);
    		//Display the computers choice and the winner.  
    		displayResults(user, CT);
     
     
     
    	}
     
    	public static void directions()
    	{
    	JOptionPane.showMessageDialog(null,"\nTo play the game you type R for ROCK, P for PAPER, or S for Scissors\n\n"+
    	                   "A  winner  is selected according  to  the  following  rules:\n\n"+ 
    						"*  If one player  chooses rock  and the other  player chooses scissors,\n"+		
          						"then  rock  wins, (The rock  smashes the  scissors.)\n\n"+ 
    						"*  If  one  player  chooses  scissors  and the other  player  chooses  paper,\n"+ 
    						    "then  scissors wins. (Scissors cuts  paper.) \n\n"+
    						"*  If one player chooses paper and the other player chooses rock, then paper\n"+ 
    						   "wins. (Paper wraps  rock.)\n\n"+
    						"*  If both players make the same choice, the game must be played again to\n"+ 
    						   "determine the winner.");
    	}
     
    	public static int computersTurn(int CT)
    	{
     
    	Random randomNumbers = new Random();
    	CT = randomNumbers.nextInt(3);
    	return CT;
    	}
     
    	public static String usersTurn(String user)
    	{
     
    	user = JOptionPane.showInputDialog("What is your choice?  ");
    	return user;
     
    	public static void displayResults(String user, int CT)
    	{
    	JOptionPane.showMessageDialog(null,"You have" + user + "and the computer has" + CT + "\n");
     
    	if (user == "R" && CT == 2)
    	{
    	JOptionPane.showMessageDialog(null,"You Win! Rock  smashes Scissors.");
    	}
    	else
    		if (user == "S" && CT == 0)
    		{
    		JOptionPane.showMessageDialog(null,"You Lose! Rock  smashes Scissors.");
    		}
    		else
    			if (user == "P" && CT == 0)
    			{
    			JOptionPane.showMessageDialog(null,"You Win! Paper wraps  rock.");
    			}
    			else
    				if (user == "R" && CT == 1)
    				{
    				JOptionPane.showMessageDialog(null,"You Lose! Paper wraps  rock.");
    				}
    				else
    					if (user == "S" && CT == 1)
    					{
    					JOptionPane.showMessageDialog(null,"You Win! Scissors cuts  paper.");
    					}
    					else
    						if (user == "P" && CT == 2)
    						{
    						JOptionPane.showMessageDialog(null,"You Lose! Scissors cuts  paper.");
    						}
    						else
    						{
    						JOptionPane.showMessageDialog(null,"It's a tie. Play again to beat the tie");
    						}
    	}
    }


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

    Default Re: Can someone tell me why I keep getting a tie, no matter what I type in?

    As mentioned in other post to a user doing something similar. The issue is you are attempting to compare a String value which is not a primitive variable with ==. When you check for == on an Object it checks the memory reference to see if that matches. You must use String.equals("String to compare")

  3. #3
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: Can someone tell me why I keep getting a tie, no matter what I type in?

    Duplicate topic.

    Please don't start a new topic for the same problem. Continue to work the problem in the original where I believe someone has already offered an answer to this post.

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

    Default Re: Can someone tell me why I keep getting a tie, no matter what I type in?

    So I changed it and now it throws an error that says bad operand types for binary operator.
    if (user.equals("R") && CT = 2)
    {
    	JOptionPane.showMessageDialog(null,"You Win! Rock  smashes Scissors.");
    	}
    Last edited by Norm; October 5th, 2013 at 04:06 PM. Reason: fixed code tag

  5. #5
    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: Can someone tell me why I keep getting a tie, no matter what I type in?

    You should use the == operator to compare int values. The = is the assignment operator.
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Replies: 1
    Last Post: September 9th, 2013, 11:26 AM
  2. Replies: 1
    Last Post: November 27th, 2012, 08:55 AM
  3. Need help on a basic matter
    By Dan-jr in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 15th, 2011, 11:51 AM
  4. Rounding long or floating numbers, or longs for that matter.
    By SPACE MONKEY in forum What's Wrong With My Code?
    Replies: 3
    Last Post: February 28th, 2011, 12:32 AM

Tags for this Thread