.equals与其他布尔函数

huangapple go评论70阅读模式
英文:

.equals with other Boolean functions

问题

在我程序的一个小片段中,我正在尝试验证是否输入了 M、m、F 或 f,大部分情况下我都做到了,但当我把这些语句放在一起时似乎出现了问题。例如,

   String _gender = "M";
   if(!"m".equals(_gender.toLowerCase())||!"f".equals(_gender.toLowerCase())){
      System.out.println("请重试");
   }else 
      System.out.println("成功");

它告诉我要重试,但当我将它改成:

  String _gender = "M";
   if(!"m".equals(_gender.toLowerCase())){
      System.out.println("请重试");
   }else 
      System.out.println("成功");

它告诉我成功。我不知道是不是我使用了逻辑“或”的方式有问题,但这是我猜测的。我还认为可能与检查同时检查了两者有关。我不知道,我已经在做这个已经有一段时间了,一直让我感到困惑。

英文:

In a small segment of my program I'm trying to validate if someone enter M,m,F,f and for the most part I got it, but something seems wrong when I put the statements together. For example,

   String _gender = "M";
   if(!"m".equals(_gender.toLowerCase())||!"f".equals(_gender.toLowerCase())){
      System.out.println("try again");
   }else 
      System.out.println("success");

It tells me to try again, but when I put it as:

  String _gender = "M";
   if(!"m".equals(_gender.toLowerCase())){
      System.out.println("try again");
   }else 
      System.out.println("success");

It gives me a success. I don't know if its something with how I'm using the or, but thats what I've guessed. I also think it may be something to do with the fact that the check is checking both or something. Idk I've been at this for a while and its been confusing me.

答案1

得分: 4

有一个逻辑错误。当两个equal()结果都为false时,它应该进入“再试一次”。

String _gender = "M";
if(!"m".equals(_gender.toLowerCase()) && !"f".equals(_gender.toLowerCase())){
  System.out.println("再试一次");
}else 
  System.out.println("成功");
英文:

There is logical mistake. It should go into "try again" when both equal() results are false.

String _gender = "M";
if(!"m".equals(_gender.toLowerCase()) && !"f".equals(_gender.toLowerCase())){
  System.out.println("try again");
}else 
  System.out.println("success");

答案2

得分: 0

让我们评估一下你的代码:

String _gender = "M";
if(!"m".equals(_gender.toLowerCase())||!"f".equals(_gender.toLowerCase()))

if(!true || !false) => if(false || true) => true

现在你明白为什么会得到输出 try again 了。

现在,让我们修正一下:

请改成以下方式:

String _gender = "M";
if (!("m".equals(_gender.toLowerCase()) || "f".equals(_gender.toLowerCase()))) 

这将评估为:
(!(true || "f".equals(_gender.toLowerCase()))) => (!(true)) => false

这将会给你预期的输出结果,即 success

英文:

Let's evaluate your code:

String _gender = "M";
if(!"m".equals(_gender.toLowerCase())||!"f".equals(_gender.toLowerCase()))

if(!true || !false) => if(false || true) => true

Now you understand the reason why you got the output, try again.

Now, let's fix it:

Do it as

String _gender = "M";
if (!("m".equals(_gender.toLowerCase()) || "f".equals(_gender.toLowerCase()))) 

which evaluates to
if (!(true || "f".equals(_gender.toLowerCase()))) => (!(true)) => false

which will give you the intended output, success

huangapple
  • 本文由 发表于 2020年10月14日 04:20:10
  • 转载请务必保留本文链接:https://go.coder-hub.com/64342613.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定