我在比较 Java 中的字符时遇到问题。我试图找到一个有效的二进制数,所以我将一个带有二进制数字的字符串传递给一个函数,并确保它们是 0 或 1。我遍历并检查字符串中的每个字符,但是,我的函数告诉我它很糟糕(即使我知道我已经给它一个正确的二进制数)。
这是功能:
public boolean isValidBinary(String s) {
//First we get the string length
int strLen = s.length();
//Now we loop through each character of the string
for(int x = 0; x < strLen; x++) {
//Assign the character to a variable each loopthrough
char c = s.charAt(x);
//Check if it's either a 0 or a 1
if(c != '0' || c != '1') {
return false;
}
}
//This is reached when all char's have been evaluated as 0 or 1
return true;
}
我已经盯着这个问题很长一段时间了,一直无法弄清楚,所以任何帮助都将不胜感激。