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.
Yeah, no doubt! I loved when gaming magazines shipped with the latest and greatest demos! I still have many including Quake 1 demo way back when. Kinda like PC boxes with thick manuals, instructions, etc.
Sony has had some misses but honestly the one thing they have done well is not compromise (too much) on first party titles! Microsoft on the other hand have left first party IPs to rot in hell. The who conversation about "entertainment" we want to be the best entertainment company tells you everything you need to know. They are love bombing initially but then doing a Don Mattrick rug pull where XBOX becomes and entertainment company as oppose to a gaming one. All the fan service over the last 3 months down the pan. This company should never have been allowed to acquire so many studios only to kill them off!
Question
M4nB3arP1g
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
https://www.neowin.net/forum/topic/843954-java-if-statements-help/Share on other sites
8 answers to this question
Recommended Posts