【问题标题】:do While loop trouble做While循环麻烦
【发布时间】:2020-01-23 00:35:01
【问题描述】:

我的 do-while 语句有问题。我创建了一个 do-while 循环以确保唯一接受的输入是“e”或“o”(不区分大小写),但是,即使我插入了所需的输入,它也会继续循环。任何帮助表示赞赏!

Current Code & Result

【问题讨论】:

  • 你的意思是循环 &&。并且不应该将代码作为图像发布......

标签: java string loops while-loop or-operator


【解决方案1】:

此声明:
while(!side.equalsIgnoreCase("O") || !side.equalsIgnoreCase("E"));
总是正确的

如果你输入Ee,这个!side.equalsIgnoreCase("E")是假的,这个!side.equalsIgnoreCase("O")是真的

如果你输入O或者o,这个!side.equalsIgnoreCase("O")是假的,这个!side.equalsIgnoreCase("E")是真的

由于你使用的是||true || false 给你true 所以循环永远不会结束

对于所有其他输入,两者都为真(true || true),这也是真的

您需要将其替换为:
while(!side.equalsIgnoreCase("O") && !side.equalsIgnoreCase("E"));

【讨论】:

  • 我脑子里有不同的理解,所以谢谢你的解释,它消除了我的困惑。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-17
  • 2019-04-27
  • 2015-03-09
  • 2022-01-03
  • 1970-01-01
  • 2023-03-13
相关资源
最近更新 更多