How can I assign user input (words) an integer value to be used in the code

Dodo :

I'm attempting to create a game of "Rock Paper Scissors Lizard Spock", and want the user to be able to put in either the integers assigned to the variables (Rock=0, Paper=1, Scissors=2, Lizard=3 and Spock=4) and also be able to enter the words "Rock", "Paper", "Scissors", "Lizard" or "Spock". Could you help me include a part of the code where I can assign the string inputs to integers. I also do not want to change the main framework of the code. I also know that the website says not to paste the entire file, but I cannot think of another way to show my problem. Please note that I have been using a website called SoloLearn. Any help would be greatly appreciated.

import java.util.Scanner;

public class RockPaperScissorsLizardSpock {

    final static int ROCK = 0;
    final static int PAPER = 1;
    final static int SCISSORS = 2;
    final static int LIZARD = 3;
    final static int SPOCK = 4;


    public static void main(String[] args) {

        double r = Math.random();
        int computerChoice = (int)(3.0 * r);

        Scanner input = new Scanner(System.in);
        System.out.print("Enter 0 for Rock, 1 for Paper, 2 for Scissors, 3 for Lizard, 4 for Spock: ");
        int playerChoice = input.nextInt();
    System.out.println(computerChoice);

    int playerChoice = 0;

    switch (playerChoice) {

        case "Rock":
            playerChoice = 0;
            break;
        case "Paper":
            playerChoice = 1;
            break;
        case "Scissors":
            playerChoice = 2;
        case "Lizard":
            playerChoice = 3;
        case "Spock":
            playerChoice = 4;

    if (computerChoice == playerChoice) {
            System.out.println("Tie");
        }
        else if (computerChoice == ROCK && playerChoice == SCISSORS) {
            System.out.println("I chose Rock,You chose Scissors, Rock crushes Scissors, You lose.");
        }
        else if (computerChoice == SCISSORS && playerChoice == PAPER) {
            System.out.println("I chose Scissors, You chose Paper, Scissors cut Paper, You lose.");
        }
        else if (computerChoice == PAPER && playerChoice == ROCK) {
            System.out.println("I chose Paper,You chose Rock, Paper covers Rock, You lose.");
        }
         else if (computerChoice == LIZARD && playerChoice == PAPER) {
            System.out.println("I chose Lizard, You chose Paper, Lizard eats Paper, You lose.");
        }
        else if (computerChoice == SPOCK && playerChoice == SCISSORS) {
            System.out.println("I chose Spock, You chose Scissors, Spock smashes Scissors, You lose.");
        }
        else if (computerChoice == ROCK && playerChoice == LIZARD) {
            System.out.println("I chose Rock, You chose Lizard, Rock crushes Lizard, You lose.");
        }
        else if (computerChoice == SCISSORS && playerChoice == LIZARD) {
            System.out.println("I chose Scissors, You chose Lizard, Scissors decapitates Lizard, You lose.");
        }
        else if (computerChoice == SPOCK && playerChoice == PAPER) {
            System.out.println("I chose Spock, You chose Paper, Paper disproves Spock, You lose.");
        }
        else if (computerChoice == SPOCK && playerChoice == ROCK) {
            System.out.println("I chose Spock, You chose Rock, Spock vaporizes Rock, You lose.");
        }
        else if (computerChoice == SCISSORS && playerChoice == ROCK) {
            System.out.println("I chose Scissors, You chose Rock, Rock crushes Scissors, You win.");
        }
        else if (computerChoice == PAPER && playerChoice == SCISSORS) {
            System.out.println("I chose Paper, You chose Scissors, Scissors cut Paper, You win.");
        }
        else if (computerChoice == ROCK && playerChoice == PAPER) {
            System.out.println("I chose Rock, You chose Paper, Paper covers Rock,You win.");
        }
        else if (computerChoice == PAPER && playerChoice == LIZARD) {
            System.out.println("I chose Paper, You chose Lizard, Lizard eats Paper, You win.");
        }
        else if (computerChoice == SCISSORS && playerChoice == SPOCK) {
            System.out.println("I chose Scissors, You chose Spock, Spock smashes Scissors, You win.");
        }
         else if (computerChoice == LIZARD && playerChoice == ROCK) {
            System.out.println("I chose Lizard, You chose Rock, Rock crushes Lizard, You win.");
        }
        else if (computerChoice == LIZARD && playerChoice == SCISSORS) {
            System.out.println("I chose Lizard, You chose Scissors, Scissors decapitates Lizard,  win.");
        }
        else if (computerChoice == PAPER && playerChoice == SPOCK) {
            System.out.println("I chose Paper, You chose Spock, Paper disproves Spock, You win.");
        }
        else if (computerChoice == ROCK && playerChoice == SPOCK) {
            System.out.println("I chose Rock, You chose Spock, Spock vaporizes Rock, You win.");
        }else{
            System.out.println("Error");
        }
    }
  }
}
Makusium :

If you want that your user can input 2 Values for 1 condition you can convert the value from playerChoice to String for a temp String and then make a switch case for it like the following:

 String tempString = Integer.toString(playerChoice);


        switch (tempString) {
        case "0":
        case "Rock":
            playerChoice = 0;
            break;
        case "1":
        case "Paper":
            playerChoice = 1;
            break;
        case "2":
        case "Scissors":
            playerChoice = 2;
            break;
        case "3":
        case "Lizard":
            playerChoice = 3;
            break;
        case "4":
        case "Spock":
            playerChoice = 4;
            break;
    }

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=166303&siteId=1