如何将String与整数进行比较?

时间:2015-10-18 16:59:01

标签: java

如何将字符串与int进行比较?我正在制作一个Rock-paper-scissors游戏,如何将用户输入的字符串转换为int,以便程序可以检查谁赢了?例如,如果用户输入" rock" ,程序会将其注册为 0 等等?

package rpc;

import java.util.Random;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        /* Random number generator */
        Random random = new Random();

        /* Scanner object for input */
        Scanner scanner = new Scanner(System.in);

        /*
         * Integer variables to hold the user and computer choice.
         * 0 = Rock
         * 1 = Paper
         * 2 = Scissors
         */
        String userChoice;
        int computerChoice;

        // Showing prompt and user input
        System.out.println("Enter move (0 = Rock; 1 = Paper; 2 = Scissors):");
        userChoice = scanner.nextLine();

        // Checking if userChoice is 0, 1, or 2.
        if (!userChoice.equalsIgnoreCase("Scissors") && !userChoice.equalsIgnoreCase("Paper")
            && !userChoice.equalsIgnoreCase("rock")) {
            System.out.println("Invalid choice. Ending program.");

            // Exit program
            Main.main(args);
        }

        // Generating random computer choice
        computerChoice = random.nextInt(3);

        // Determining the winner

        // If the choices are equal, it's a tie.
        if (userChoice == computerChoice) {
            if (userChoice == 0) {
                System.out.println("Both players chose rock!");
            } else if (userChoice == 1) {
                System.out.println("Both players chose paper!");
            } else {
                System.out.println("Both players chose scissors!");
            }

            // Exit program
            System.exit(0);
        }

        if (userChoice == 0) {        // User chooses rock
            if (computerChoice == 1) {
                System.out.println("You chose rock; Computer chose paper");
                System.out.println("Computer wins!");
            } else {
                System.out.println("You chose rock; Computer chose scissors");
                System.out.println("You win!");
            }
        } else if (userChoice == 1) {    // User chooses paper
            if (computerChoice == 0) {
                System.out.println("You chose paper; Computer chose rock");
                System.out.println("You win!");
            } else {
                System.out.println("You chose paper; Computer chose scissors");
                System.out.println("Computer wins!");
            }
        } else {    // User chooses scissors
            if (computerChoice == 0) {
                System.out.println("You chose scissors; Computer chose rock");
                System.out.println("Computer wins!");
            } else {
                System.out.println("You chose scissors; Computer chose paper");
                System.out.println("You win!");
            }
        }
        scanner.close();
    }
}

4 个答案:

答案 0 :(得分:2)

您可以使用枚举枚举三种可能的选择:

enum Hand {

    ROCK,
    PAPER,
    SCISSORS;

    public static Hand from(String input) {
        for (Hand hand : values()) {
            if (hand.name().equalsIgnoreCase(input)) {
                return hand;
            }
        }
        throw new IllegalArgumentException("Invalid choice: " + input);
    }

}

枚举具有内在整数值(对应于它们定义的位置)。例如,ROCK.ordinal()将返回0。

答案 1 :(得分:0)

只需使用pareseInt并将字符串转换为int

例如:

if(Integer.parseInt(userChoice) == computerChoice)

确保输入不为null且可格式化为int

编辑:将parese改为解析

答案 2 :(得分:0)

Retrieving a random item from ArrayList

这不是你的问题的确切答案(Integer.parseInt(myInt)),但你可以尝试更像这样的东西,避免使用不必要的整数。并简化您的代码

生成你的arrayList,然后选择随机的#34;计算机"选择。

List<String> posibilities = Arrays.asList("rock","paper","scissors");
String computerChoice = possibilites.get(Math.random(3));

然后做你的比较;)

/* Chose the possibilities */
List<String> posibilities = Arrays.asList("rock","paper","scissors");

/* Scanner object for input */
Scanner scanner = new Scanner(System.in);

// Showing prompt and user input
System.out.println("Enter move (0 = Rock; 1 = Paper; 2 = Scissors):");
String userChoice = scanner.nextLine();
userChoice = possibilities.get(Integer.parseInt(userChoice));

// Checking if userChoice is 0, 1, or 2.
if(!possibilities.contains(userChoice)) {
    System.out.println("Invalid choice. Ending program.");

    // Exit program
    Main.main(args);
}

// Generating random computer choice
String computerChoice = possibilites.get(Math.random(3));

// Determining the winner

// If the choices are equal, it's a tie.
if(userChoice.equals(computerChoice)) {
    System.out.println("Both players chose " + userChoice);

    // Exit program
    System.exit(0);
}

System.out.println("You chose " + userChoice + "; Computer chose " + computerChoice);
if(userChoice.equals("rock")) {    // User chooses rock
    if(computerChoice.equals("paper")) {
            System.out.println("Computer wins!");
    } else {
        System.out.println("You win!");
    }
}
else if(userChoice.equals("paper")) {  // User chooses paper
    if(computerChoice.equals("rock")) {
        System.out.println("You win!");
    } else {
        System.out.println("Computer wins!");
    }
} else {   // User chooses scissors
    if(computerChoice.equals("Scissors")) {
        System.out.println("Computer wins!");
    } else {
        System.out.println("You win!");
    }
}

scanner.close();

答案 3 :(得分:-2)

使用Integer.parseInt方法将String解析为int然后进行比较