Java 中的多个 OR 条件
Multiple OR conditions in Java
为什么当前的多重条件 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' 时的布尔表达式的值。
为什么当前的多重条件 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' 时的布尔表达式的值。