【问题标题】:Case insensitive input and any advice for improvement不区分大小写的输入和任何改进建议
【发布时间】:2018-05-08 05:55:55
【问题描述】:

我正在做一个石头剪刀布游戏项目,我想知道如何让用户输入多种类型的同一个单词。如果用户键入“ROCK”、“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!");
    }

}

【问题讨论】:

  • 使用toLowerCase()。以小写格式处理每个输入。
  • if (User.equals("Rock")) { 之类的所有条件都是不必要的:您已经检查过它是其中之一。只需将用户的选择连接到字符串即可。
  • 关于项目的批评请发邮件至Code Review。对于寻求改进建议的可行解决方案来说,这将是一个更好的地方。

标签: java string if-statement case-insensitive


【解决方案1】:

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

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

【讨论】:

    【解决方案2】:
    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) + "\"");
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-03-06
      • 2020-02-18
      • 2014-07-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多