【问题标题】:How to use an ignore case statement for inputs如何对输入使用忽略大小写语句
【发布时间】:2016-06-04 12:45:00
【问题描述】:

如何忽略大小写。我制作了一个显示菜单,当我输入小写字母时会出现正确的输出,但是当我输入大写字母时,它会继续使用 while 语句。我知道我必须使用 ignoreCase 语句,但我不知道如何。先感谢您。

public class Aaa {
    AQAConsole2016 console = new AQAConsole2016();
    Random random = new Random();
    int boardSize;
    boolean moveIsValid;

    char [][] board;
    int move;
    char choice;
    String playerName="Human";
    String player2="Computer";
    boolean exit=false;
    public Aaa() {

        boardSize = 6;

        do {
            displayMenu();
            choice = getMenuChoice(playerName);
            switch (choice) {
            case 'p' : playGame(playerName, boardSize);
            break;
            case 'e' : playerName = getPlayersName();
            break;
            case 'c' : boardSize = changeBoardSize();
            break;
            case 'm' : Multiplayer( boardSize,playerName,player2);
            break;
            case 'r' :readBoard(board,boardSize);
            break;
            case 'q' : Quit();


            }
        }while (choice!='p'||choice!='e'||choice!='c'||choice!='m'||choice!='r'||choice!='q');
    }
    void Quit(){
        while (choice=='q'){
            exit=true;
        }

【问题讨论】:

  • 所以您需要/希望用户菜单对 p 和 P 做出反应??
  • 是的,当用户键入 p 或 P、r 或 R 等时

标签: java case do-while void ignore-case


【解决方案1】:

只需为此使用Character#toLowerCase()

choice = Character.toLowerCase(getMenuChoice(playerName));

【讨论】:

    【解决方案2】:

    尝试使用字符小写method

    示例:

         displayMenu();
         choice = Character.toLowerCase(getMenuChoice(playerName));
         switch (choice) {
    

    【讨论】:

      【解决方案3】:

      更好的选择是使用toLowerCase() 函数。

      displayMenu();
      choice = Character.toLowerCase(getMenuChoice(playerName));
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-05-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多