• 0

[Java] If statements help


Question

I am a newbie at java and I am trying to make a simple rock, paper, scissors game:

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

public class RockPaperScissors
{
	public static void main(String[] args)
	{
		Scanner scan = new Scanner(System.in);
		Random generator = new Random();

		int number;
		String choice, computerChoice;

		number = generator.nextInt(3) + 1;

		computerChoice = "";

		//-------------------------------------------------
		// uses random to select a choice for the computer
		//-------------------------------------------------
		switch (number)
		{
			case 1:
				computerChoice = "Rock";
				break;
			case 2:
				computerChoice = "Paper";
				break;
			case 3:
				computerChoice = "Scissors";
				break;				
		}

		//--------------------------------------------------------
		// asks the user for their choice and takes in the choice
		//--------------------------------------------------------
		System.out.print("Enter your choice: ");
		choice = scan.nextLine();	

		//-------------------------------------------------------------------
		// compares the users choice to the computers and outputs the result
		//-------------------------------------------------------------------
		if (computerChoice == "Rock" && choice == "rock" || choice == "Rock")
		{
			System.out.println("Tie. Computer chose: " + computerChoice + " You chose : " + choice);
		}

		else if (computerChoice == "Rock" && choice == "scissors" || choice == "Scissors")
		{
			System.out.println("Lose. Computer chose: " + computerChoice + " You chose : " + choice);
		}

		else if (computerChoice == "Rock" && choice == "paper" || choice == "Paper")
		{
			System.out.println("Win. Computer chose: " + computerChoice + " You chose : " + choice);
		}

		else if (computerChoice == "Scissors" && choice == "rock" || choice == "Rock")
		{
			System.out.println("Win. Computer chose: " + computerChoice + " You chose : " + choice);
		}

		else if (computerChoice == "Scissors" && choice == "scissors" || choice == "Scissors")
		{
			System.out.println("Tie. Computer chose: " + computerChoice + " You chose : " + choice);
		}

		else if (computerChoice == "Scissors" && choice == "paper" || choice == "Paper")
		{
			System.out.println("Lose. Computer chose: " + computerChoice + " You chose : " + choice);
		}

		else if (computerChoice == "Paper" && choice == "rock" || choice == "Rock")
		{
			System.out.println("Lose. Computer chose: " + computerChoice + " You chose : " + choice);
		}

		else if (computerChoice == "Paper" && choice == "scissors" || choice == "Scissors")
		{
			System.out.println("Win. Computer chose: " + computerChoice + " You chose : " + choice);
		}

		else if (computerChoice == "Paper" && choice == "paper" || choice == "Paper")
		{
			System.out.println("Tie. Computer chose: " + computerChoice + " You chose : " + choice);
		}			
	}
}

I am sure this code is working up to the if statements because I made some print statements to see if the computer choice and use choice are being stored correctly and I can't figure out why the if statements are not working.

Link to comment
Share on other sites

8 answers to this question

Recommended Posts

  • 0

As giantpotato said, you should use the equals() method to compare two strings. Also, instead of having an OR statement to compare if the choice is "rock" or "Rock" or "rocK" or whatever other combination you can think of, you should use the equalsIgnoreCase() method.

equalsIgnoreCase

Link to comment
Share on other sites

  • 0

Or you can use toUpperCase() / toLowerCase() so you don't need to do the or for the capital letters

Edited by WelshBluebird
Link to comment
Share on other sites

  • 0

You might want to think about ways to streamline your logic.

if(computerChoice.equalsIgnoreCase(choice))
{
	System.out.print("Tie.");
}
else if(
	(choice.equalsIgnoreCase("Rock") && computerChoice.equals("Scissors")) ||
	(choice.equalsIgnoreCase("Paper") && computerChoice.equals("Rock")) ||
	(choice.equalsIgnoreCase("Scissors") && computerChoice.equals("Paper")))
{
	System.out.print("Win.");
}
else
{
	System.out.print("Lose.");
}

System.out.println("Computer chose: " + computerChoice + " You chose : " + choice);

Link to comment
Share on other sites

  • 0

Also, is there any reason you've added 1 to the random number output?

number = generator.nextInt(3) + 1;

If you removed that and had the switch cases to 0/1/2 it'd make less work for you in the first place, and whilst it won't really make any difference to a small program like this, it's one less thing for the code to do (when you work on larger, more complex programs you'll realise the need for optimised code (especially if you have to do something 500,000 times :p).

Link to comment
Share on other sites

  • 0
If you removed that and had the switch cases to 0/1/2 it'd make less work for you in the first place, and whilst it won't really make any difference to a small program like this, it's one less thing for the code to do (when you work on larger, more complex programs you'll realise the need for optimised code (especially if you have to do something 500,000 times :p).

There are very few situations I can think of where adding 1 to a number would account for any appreciable performance loss, even when executed thousands of times.

The only reason I bring it up is that picking a number from 1 to 3 makes more sense to the human brain than picking a number from 0 to 2. Unless your project is frequently-executed OS code, an embedded application, a game, or something like that, then it is sometimes desirable to take a negligible performance hit and make things easier to understand for yourself and especially others that may be reading your code later.

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.