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.
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
Share on other sites
8 answers to this question
Recommended Posts