This is my second week in a Java course, so bear with me. I am trying to make a program that allows for a user input of "rock","paper", or "scissors." I am sure there are more efficient ways than what I have done, and I thought I was actually making progress, but now I stuck.
My applicable knowledge to this program is limited to if and if-else statements as well as switch statements. I tried to get a user input for rock, paper, or scissors, convert that to a number, and compare that to a randomly generated number, with 0 representing rock, 1 representing paper, and 2 representing scissors.
import java.util.*;
public class RockPaperScissors
{
public static void main(String[] args) {
int computer;
computer = (int)(Math.random() * 2 + 1);
Scanner input = new Scanner(System.in);
String player;
System.out.println("Enter the word rock, paper, or scissors.");
player = input.next();
if (computer == 0) {
System.out.println("The computer chose rock.");
}
else if (computer == 1) {
System.out.println("The compuer chose paper.");
}
else if (computer == 2) {
System.out.println("The computer chose scissors.");
}
// personally starting from here is where I feel like I started to make mistakes.
int rock = 0;
int paper = 1;
int scissors = 2;
player = (int)player;
switch (player) {
case "r" : rock = 0;
player = 0;
break;
case 'p' : paper = 1;
player = 1;
break;
case 's' : scissors = 2;
player = 2;
break;
default : System.out.println("Invalid input");
}
if (player == computer) {
System.out.println("You tied") ;
}
else if (player = 1 && computer = 0 ^ player = 2 && computer = 0) {
System.out.println("You won");
}
else {
System.out.println("You lost");
}
}
}