所以,我正在尝试创建一些东西来检查仅包含字母的kinderCode(字符串)。如果它确实包含非字母的字符,则需要要求用户为 kinderCode 提供一个新值,然后再次检查该值。这里的问题是 while 循环永远不会离开。条件始终返回 false。
kinderCode = input.next();
while (!(kinderCode.equals("[a-zA-Z]+"))) {
System.out.println("Foute ingave! Kindercode?");
kinderCode = input.next();
}
最佳答案
您需要使用.matches()
while (!(kinderCode.matches("[a-zA-Z]+"))) {
// rest of the code
}
matches(String regex)
Tells whether or not this string matches the given regular expression
.equals()
用于比较字符串的相等。
关于Java 等于 ("[a-zA-Z]+")始终为 false,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53534576/