不区分大小写的输入和任何改进建议

时间:2018-05-08 05:55:56

标签: java string if-statement case-insensitive

我正在研究一个石头剪刀游戏项目,我想知道如何允许用户输入多个类型的相同单词。如果用户键入" ROCK","摇滚"或" RoCk"我希望程序允许它作为有效输入继续。此外,这是我自己的第一个项目,如果您有任何建议或批评,请告诉我。我希望在编程方面做得更好,并乐意接受任何建议。谢谢。

public static void main(String[] args) {
    Scanner input = new Scanner(System.in);

    //Displays what the game is
    System.out.println("Rock! Paper! Scissors!");

    String[] random = {"Rock", "Paper", "Scissors"};

    //Making the computer choice random
    String randomString = random[(int) (Math.random() * random.length)];

    //Telling the user to chose between rock paper and scissors
    System.out.println("Rock Paper or Scissors?");

    //User input
    String User;

    //If User doesn't enter Rock, Paper, or Scissors they will get an error message and have to try again
    do {
        User = input.nextLine();
        if (!User.equals("Rock") && !User.equals("Paper") && !User.equals("Scissors")) {
            System.out.println("ERROR Please enter \"Rock\" \"Paper\" or \"Scissors\" ");
        }
    } while (!User.equals("Rock") && !User.equals("Paper") && !User.equals("Scissors"));

    //Displays what the user chose
    if (User.equals("Rock")) {
        System.out.println("User has chosen: Rock!");
    }
    if (User.equals("Paper")) {
        System.out.println("User has chosen: Paper!");
    }
    if (User.equals("Scissors")) {
        System.out.println("User has chosen: Scissors!");
    }

    //Telling the user what the computer has chosen
    System.out.println("Computer has chosen: " + randomString + "!");

    //If the user's choice equals the computers choice the game is a tie
    if (User.equals("Rock") && randomString.equals("Rock")) {
        System.out.println("It is a tie!");
    }
    if (User.equals("Paper") && randomString.equals("Paper")) {
        System.out.println("It is a tie!");
    }
    if (User.equals("Scissors") && randomString.equals("Scissors")) {
        System.out.println("It is a tie!");
    }

    //Deciding who wins if both User and computer chose something different
    if (User.equals("Rock") && randomString.equals("Paper")) {
        System.out.println("Computer has won!");
    }
    if (User.equals("Rock") && randomString.equals("Scissors")) {
        System.out.println("User has won!");
    }
    if (User.equals("Paper") && randomString.equals("Scissors")) {
        System.out.println("Computer has won!");
    }
    if (User.equals("Paper") && randomString.equals("Rock")) {
        System.out.println("User has won!");
    }
    if (User.equals("Scissors") && randomString.equals("Paper")) {
        System.out.println("User has won!");
    }
    if (User.equals("Scissors") && randomString.equals("Rock")) {
        System.out.println("Computer has won!");
    }

}

2 个答案:

答案 0 :(得分:5)

String.equalsIgnoreCase(String str)符合您的需求。它将比较两个忽略大小写的字符串。

示例:在您的情况下,您可以使用User.equalsIgnoreCase("Rock")

答案 1 :(得分:0)

do {
    User = input.nextLine();
    if (!User.equals("Rock") && !User.equals("Paper") && !User.equals("Scissors")) {
        System.out.println("ERROR Please enter \"Rock\" \"Paper\" or \"Scissors\" ");
    }
} while (!User.equals("Rock") && !User.equals("Paper") && !User.equals("Scissors"));

你一直在这里重复。

你已经有了一个包含所有选项的数组:

String[] random = {"Rock", "Paper", "Scissors"};

所以只需使用它:

while (true) {
    User = input.nextLine();
    Optional<String> r = Stream.of(random).findFirst(User::equalsIgnoreCase);
    if (r.isPresent()) {
      User = choice; // get in the expected case
      break;
    }
    System.out.println("ERROR Please enter \"" + String.join("\", \"", random) + "\"");
}