为什么当前的多个条件 if 语句不能创建一个不带元音的新字符串?
public class Disemvowel {
public static void main(String[] args) {
System.out.println("Please enter a word: ");
Scanner stdin = new Scanner(System.in);
String word = stdin.next();
int wordLength = word.length();
String disemvoweledWord = "";
for (int i=0;i<=(wordLength-1);i++){
char currentLetter = word.charAt(i);
System.out.println(currentLetter);
if (currentLetter!='a' || currentLetter!='e' || currentLetter!='i' || currentLetter!='o' || currentLetter!='u'){
disemvoweledWord += currentLetter;
}
}
System.out.println(disemvoweledWord);
}
}
最佳答案
您应该对条件进行 AND 运算,而不是 OR 运算。
您只想添加不是“a”且不是“e”且不是“i”的字母...
要亲自查看,请计算出字母为“a”或“e”时 boolean 表达式的值。
关于java - Java中的多个OR条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28729912/