简体   繁体   English

多个|| 一个if语句中的语句

[英]Multiple || statements in one if statement

I'm trying to have my program check the name that the player enters to see if it uses any invalid characters. 我试图让我的程序检查播放器输入的名称,以查看它是否使用了任何无效字符。 It seems to make sense in my mind how to make it work, but it seems not to work in the actual environment. 在我看来,如何使其有效似乎很有意义,但在实际环境中似乎无效。 When I run it, and I use invalid characters then it will just go on to the next part of the program. 当我运行它并且使用无效字符时,它将继续进行到程序的下一部分。 Any Ideas on how to fix it? 关于如何解决它的任何想法?

Here is my relevant code block: 这是我的相关代码块:

if (!(nameInput.getText().equals(""))) {
    boolean allow = true;
    String check = nameInput.getText();
    for (int i = 0;i>check.length();i++) {
        if (check.charAt(i) == '`' || check.charAt(i) == '~' ||
                check.charAt(i) == '!' || check.charAt(i) == '@' ||
                check.charAt(i) == '#' || check.charAt(i) == '$' ||
                check.charAt(i) == '%' || check.charAt(i) == '^' ||
                check.charAt(i) == '&' || check.charAt(i) == '*' ||
                check.charAt(i) == '(' || check.charAt(i) == ')' ||
                check.charAt(i) == '_' || check.charAt(i) == '+' ||
                check.charAt(i) == '=' || check.charAt(i) == '[' ||
                check.charAt(i) == ']' || check.charAt(i) == '{' ||
                check.charAt(i) == '}' || check.charAt(i) == ';' ||
                check.charAt(i) == '\''|| check.charAt(i) == ':' ||
                check.charAt(i) == '"' || check.charAt(i) == '<' ||
                check.charAt(i) == '>' || check.charAt(i) == '?' ||
                check.charAt(i) == ',' || check.charAt(i) == '.' ||
                check.charAt(i) == '/' || check.charAt(i) == '\\'||
                check.charAt(i) == '|' || check.charAt(i) == '1' ||
                check.charAt(i) == '2' || check.charAt(i) == '3' ||
                check.charAt(i) == '4' || check.charAt(i) == '5' ||
                check.charAt(i) == '6' || check.charAt(i) == '7' ||
                check.charAt(i) == '8' || check.charAt(i) == '9' ||
                check.charAt(i) == '0') {
            allow = false;
        }
    }
    if (allow == true) {
        player = new Player(check);
        window.refresh();
        mainMenu();
    } else {
        JOptionPane.showMessageDialog(window, "Invalid Name(Uses invalid Characters","Invalid!", JOptionPane.OK_OPTION);
    }
} else {
    JOptionPane.showMessageDialog(window, "Can Not Leave Name Blank!", "Missing Name!", JOptionPane.OK_OPTION);
    window.refresh();
    createPlayer();
}
}
for(int i = 0;i>check.length();i++)

Your for loop is never going to be entered, as I don't believe a String 's length can ever be negative... 您永远不会输入for循环,因为我不认为String的长度可以为负。

Did you mean to use < instead? 您是否打算使用<代替?

Also, there are significantly better ways to check for valid names than a gigantic chain of character checks. 另外,还有一些显著更好的方法来检查不是字符检查的一个巨大的链条有效的名称。

One way is by using regular expressions; 一种方法是使用正则表达式。 check out Java's Pattern class and Google to learn regex. 查看Java的Pattern类和Google以学习正则表达式。 That way you can reduce your loop-and-if-block to this (I think; I just accepted alphabetic characters only and rejected everything else based on a really quick glance at that monstrosity): 这样,您就可以减少循环和阻塞的次数(我认为;我只接受字母字符,而基于对这种怪异的一瞥,就拒绝了其他所有内容):

allow = String.matches("[a-zA-Z]{1,}");

If you really are interested in allowing only Strings that consist of characters not on that list, you can do it with a regular expression like this. 如果您真的想只允许包含不在该列表中的字符组成的字符串,则可以使用这样的正则表达式来实现。

allow = check.matches("[^]`~!@#$%^&*()_+=[{};':\"<>?,./\\\\|1234567890]*");

This will set allow to true if check is made up entirely of characters not on the list. 如果check完全由列表中未包含的字符组成,则将allow设置为true

Note that I placed ^ just inside the [ - this means "not". 请注意,我在[ -的内部放置了^ -这表示“不是”。 I also moved ] to immediately after the ^ , so that it's not interepreted as closing the list. 我也将[ ]移到了^ ,因此不会被解释为关闭列表。

Change 更改

for(int i = 0;i>check.length();i++)

to

for(int i = 0;i<check.length();i++)

Since i is 0 which is obviously less that check.length() program flow never really enters the for loop. 由于i为0,显然这比check.length()程序流真正从未进入for循环。

Also instead of just check for "" add a null check. 另外,不只是检查""而是添加空检查。

if(nameInput.getText()!=null && !(nameInput.getText().equals(""))){..}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM