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!");
}
}
}
No comments:
Post a Comment