Logic Error: Return true if the first instance of "x" in the string is immediately followed by another "x"

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

Logic Error: Return true if the first instance of "x" in the string is immediately followed by another "x"

问题

我正在解决一个 CodingBat Java 问题,我尝试了一些解决方案:

boolean doubleX(String str) {
  if (str.length() >= 2){
    for (int i = 0; str.charAt(i) == 'x' && str.charAt(i+1) == 'x'; i++){
      return true;
    }
  }
  return false;
}
boolean doubleX(String str) {
  if (str.length() >= 2){
    for (int i = 0; i < str.length()-1; i++){
      if (str.charAt(i) == 'x' && str.charAt(i+1) == 'x') {
        return true;
      } else {
        return false;
      }
    }
  }
  return false;
}

这两个解决方案在这个测试中产生了相同的错误结果:

doubleX("axxbb") → true

但我的代码输出却是 false

我还尝试了以下解决方案,它可以正常工作,但我不明白为什么。&& 和嵌套的 if 语句之间有什么区别?

boolean doubleX(String str) {
    for (int i = 0; i < str.length()-1; i++){
      if (str.charAt(i) == 'x') {
        if (str.charAt(i+1) == 'x') {
          return true;
        } else {
          return false;
        }
      }
    }
  return false;
}

请帮我解释一下。非常感谢。

英文:

I'm doing a CodingBat Java problem and I have tried some of my solutions:

boolean doubleX(String str) {
  if (str.length() &gt;= 2){
    for (int i = 0; str.charAt(i) == &#39;x&#39; &amp;&amp; str.charAt(i+1) == &#39;x&#39;; i++){
      return true;
    }
  }
  return false;
}
boolean doubleX(String str) {
  if (str.length() &gt;= 2){
    for (int i = 0; i &lt; str.length()-1; i++){
      if (str.charAt(i) == &#39;x&#39; &amp;&amp; str.charAt(i+1) == &#39;x&#39;) {
        return true;
      } else {
        return false;
      }
    }
  }
  return false;
}

These two produced the same error with this test

doubleX(&quot;axxbb&quot;) → true

But my code turned out to be false

I also tried the below solution and it worked but I don't understand why. What is the difference between &amp;&amp; and nested if statements?

boolean doubleX(String str) {
    for (int i = 0; i &lt; str.length()-1; i++){
      if (str.charAt(i) == &#39;x&#39;) {
        if (str.charAt(i+1) == &#39;x&#39;) {
          return true;
        } else {
          return false;
        }
      }
    }
  return false;
}

Please help me explain. Thank you very much in advance

答案1

得分: 0

在你的第一个例子中 - 循环中的第二个语句定义了执行代码块的条件 - 因为字符0和字符1不是'x',它退出循环并返回false。

在你的第二个例子中 - 由于if语句在循环中,所以第一次执行时会再次看到字符0和字符1不是'x',因此在下一次循环迭代之前就返回false了。

第三个例子 - 你是正确的,只有在确认第一个找到的x之后紧跟着第二个x时才会返回false。

英文:

In your first example - the second statement in your for loop defines the condition for executing the code block - since char 0 and char 1 are not 'x', it exits the loop and returns false.

In your second example - your if statement is in the loop so the first execution again sees that char 0 and char 1 are not 'x' so it returns false before the next loop iteration.

3rd example - you are are correctly returning false Only after you confirmed the first x found was followed by a second x.

答案2

得分: 0

以下是翻译好的内容:

当你将"axxbb"作为此函数的参数发送时:

boolean doubleX(String str) {
  if (str.length() >= 2){
    for (int i = 0; str.charAt(i) == 'x' && str.charAt(i+1) == 'x'; i++){
      return true;
    }
  }
  return false;
}

在这个 for 循环中发生了以下情况:

  1. 你初始化了 i=0
  2. 它检查条件 str.charAt(i) == 'x' && str.charAt(i+1) == 'x' - 条件为 false
  3. 条件为 false,因此退出循环

条件在第一次检查时失败,因此循环在此结束。

在这个函数中:

boolean doubleX(String str) {
  if (str.length() >= 2){
    for (int i = 0; i < str.length()-1; i++){
      if (str.charAt(i) == 'x' && str.charAt(i+1) == 'x') {
        return true;
      } else {
        return false;
      }
    }
  }
  return false;
}

for 循环的第一次迭代将执行以下操作:

  1. 检查是否 &#39;a&#39;=='x' 并且 &#39;x&#39;=='x' - 条件不为真,因此直接进入 else 语句以执行其中的代码
  2. else 语句返回 false,你的函数在此结束

问题在于这个 for 循环不会执行多于一次,因为一旦第一个条件为 false,它总是会返回 false,不会进行更多的迭代。

即使你提供的第三个函数也不能正确处理像 "axaxx" 这样的字符串,因为一旦第一个条件 if (str.charAt(i) == 'x') 成功,它接下来要做的是检查下一个字符是否也是 x。因为下一个字符不是 x,它会进入 else 语句并返回 false。

要获得正确的行为,你需要在 for 循环中添加一个条件,检查连续的两个字符是否为 x,如果是,则返回 true。不要在循环中添加 else 语句。如果循环结束且条件从未满足过,则可以返回 false。

英文:

When you send "axxbb" as an argument in this function:

boolean doubleX(String str) {
  if (str.length() &gt;= 2){
    for (int i = 0; str.charAt(i) == &#39;x&#39; &amp;&amp; str.charAt(i+1) == &#39;x&#39;; i++){
      return true;
    }
  }
  return false;
}

This is what's happening in the for loop:

  1. you're initializing i=0
  2. it checks the condition str.charAt(i) == &#39;x&#39; &amp;&amp; str.charAt(i+1) == &#39;x&#39; - the condition is false
  3. condition is false so it exits the loop

The condition fails on first check so the loop ends there.

In this function:

boolean doubleX(String str) {
  if (str.length() &gt;= 2){
    for (int i = 0; i &lt; str.length()-1; i++){
      if (str.charAt(i) == &#39;x&#39; &amp;&amp; str.charAt(i+1) == &#39;x&#39;) {
        return true;
      } else {
        return false;
      }
    }
  }
  return false;
}

This is what the first iteration of the for loop will do:

  1. check if &#39;a&#39;==&#39;x&#39; and &#39;x&#39;==&#39;x&#39; - the condition is not true so it goes straight to the else statement to execute the code there
  2. the else statement returns false and your function ends there

The problem is that the for loop doesn't execute more than once because once the first condition is false, it will always return false and won't do more iterations.

Even the third third function you provided won't give you correct behavior when you send a string like "axaxx" because once the first condition if (str.charAt(i) == &#39;x&#39;) succeeds the next thing that it's going to do is check if the next character is also x. Because the next character is not x, it will go to the else statement and return false.

What you need to do for a correct behavior is have a condition in the for loop that checks if 2 subsequent characters are x and return true if they are. Don't add an else statement in the loop. If the loop finishes and the condition was never met, then you can return false.

huangapple
  • 本文由 发表于 2020年9月17日 05:47:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63928375.html
匿名

发表评论

匿名网友

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

确定