java - java中如何检查给定的句子是否避免了某个字母?

标签 java boolean

我一直在试图弄清楚如何查看给定的句子是否是一个避免字母 E 的 lipogram。我已经达到了输入真/假陈述的地步,但它只输出“这句话是不是每次都避免字母 e 的 Lipogram,无论输入是否包含 e。我做错了什么?

boolean avoidsE = false, hasE = true, avoidsS = false, containsS = true;

for(int i = 0; i < sentence.length(); i++)
{
  if (sentence.charAt(i) == 'e')
  hasE = true;
  else
  avoidsE = false;
}

if (hasE = true)
System.out.println("This sentence is not a Lipogram avoiding the letter e. ");
else if (avoidsE = false)
System.out.println("This sentence is a Lipogram avoiding the letter e! ");

最佳答案

if (hasE == true) // "=" is to assign, you want to use "==" here to check  
    System.out.println("This sentence is not a Lipogram avoiding the letter e. "); 
else if (avoidsE == false) //same here   
    System.out.println("This sentence is a Lipogram avoiding the letter e! ");

关于java - java中如何检查给定的句子是否避免了某个字母?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40053327/

相关文章:

java - Servlet 安全认证

Python C API : How to evaluate custom object for boolean?

c++ - 具有三元返回和短路的运算符

java - 提供不是 boolean 值的循环条件

java - 明确比较 boolean 常量是否很糟糕,例如如果(b == false)在Java中?

java - 使用 MethodHandle 查找最具体的重载方法

Java Swing 通过拖动鼠标滚动

java - 如何正确设计多个子类的组合?

Java本地数据库

scala - 奇怪的Scala语法,其中映射Future以便 “==”和 “!=”仅出现一个操作数(而不是两个)