在 Java 中强制输入有效

发布于 2024-09-24 06:13:45 字数 1213 浏览 5 评论 0原文

我有一个用 Java 编写的类,其中一个方法是 getCommand() 此方法的目的是读取字符串并查看用户输入的内容是否与任何可接受的命令相匹配。

这就是我最初的写法:

public char getCommand(){


    System.out.println("Input command: ");
     command = input.nextLine();

    while(command.length() != 1){
        System.out.println("Please re-enter input as one character: ");
        command = input.nextLine();
    }

    while(  command.substring(0) != "e" ||
            command.substring(0) != "c" || 
            command.substring(0) != "s" ||
            command.substring(0) != "r" ||
            command.substring(0) != "l" ||
            command.substring(0) != "u" ||
            command.substring(0) != "d" ||
            command.substring(0) != "k" ||
            command.substring(0) != "f" ||
            command.substring(0) != "t" ||
            command.substring(0) != "p" ||
            command.substring(0) != "m" ||
            command.substring(0) != "q"){
        System.out.println("Please enter a valid character: ");
        command = input.nextLine();
    }

    fCommand = command.charAt(0);

    return fCommand;

}

现在,我发现问题在于,由于我使用 OR 运算符,它不会逃脱该循环,因为我输入的字符始终不等于其中之一。我尝试将其更改为 AND 运算符,但同样的问题。只接受这些特定字符的最佳方法是什么? 非常感谢。

I have a class I wrote in Java and one of the methods is getCommand()
The purpose of this method is to read in a string and see what the user typed in matches any of the acceptable commands.

This is how I wrote it initially:

public char getCommand(){


    System.out.println("Input command: ");
     command = input.nextLine();

    while(command.length() != 1){
        System.out.println("Please re-enter input as one character: ");
        command = input.nextLine();
    }

    while(  command.substring(0) != "e" ||
            command.substring(0) != "c" || 
            command.substring(0) != "s" ||
            command.substring(0) != "r" ||
            command.substring(0) != "l" ||
            command.substring(0) != "u" ||
            command.substring(0) != "d" ||
            command.substring(0) != "k" ||
            command.substring(0) != "f" ||
            command.substring(0) != "t" ||
            command.substring(0) != "p" ||
            command.substring(0) != "m" ||
            command.substring(0) != "q"){
        System.out.println("Please enter a valid character: ");
        command = input.nextLine();
    }

    fCommand = command.charAt(0);

    return fCommand;

}

Now, I see the problem with this is that since I use the OR operator, it won't escape that loop because the character I type in will always not equal one of them. I tried changing it to the AND operator, but same problem. What would be the best way to only accept those specific characters?
Much appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

水水月牙 2024-10-01 06:13:45

你的逻辑不正确。您应该使用逻辑 AND 而不是 OR。另外我相信您想使用 charAt() 而不是 substring() 然后比较字符。

即,

while(  command.charAt(0) != 'e' &&
        command.charAt(0) != 'c' && 
        command.charAt(0) != 's' &&
        ...)

否则,如果您想测试实际的单字符字符串输入,只需使用字符串相等性进行检查。

while(  !command.equals("e") &&
        !command.equals("c") &&
        !command.equals("s") &&
        ...)

Your logic is incorrect. You should be using logical ANDs and not ORs. Also I believe you want to use charAt() instead of substring() then compare characters.

i.e.,

while(  command.charAt(0) != 'e' &&
        command.charAt(0) != 'c' && 
        command.charAt(0) != 's' &&
        ...)

Otherwise if you want to test for actual single-character string inputs, just check using string equality.

while(  !command.equals("e") &&
        !command.equals("c") &&
        !command.equals("s") &&
        ...)
不可一世的女人 2024-10-01 06:13:45

您应该将命令定义为常量(单独)。像这样的硬编码值使得将来更新代码变得更加困难。

如果程序只是概念证明或家庭作业,我会使用:

private static final String COMMANDS = "ecsrludkftpmq";

while(!COMMANDS.contains(command.getChar(0)) {
  System.out.println("Please enter a valid character: ");
  command = input.nextLine();
}

否则,如果这是生产代码,我会考虑制作一个简单的 Command(char) 类,并提供单独的命令常量作为集合的一部分(可能是针对字符键的映射) ),可以测试一下是否包含匹配的命令。

You should define your commands as constants (individually). Hard coding values like this makes it harder to update your code in the future.

If the program is simply proof of concept or homework I would use:

private static final String COMMANDS = "ecsrludkftpmq";

while(!COMMANDS.contains(command.getChar(0)) {
  System.out.println("Please enter a valid character: ");
  command = input.nextLine();
}

Otherwise, if this is production code I would consider making a simple Command(char) class and providing individual command constants as part of a collection (possibly a Map against the Character key), which can be tested to see if contains a matching command.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文