布尔逻辑和for循环

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

Boolean Logic and For Loops

问题

问题:
给定一个字符串,如果字符串中第一个出现的"x"后面紧跟着另一个"x",则返回true。

为什么第一段代码在评估第一个条件后立即退出,而第二段代码在for循环中继续执行?它们之间有什么区别?

我认为以下代码会起作用:

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

但是这段代码只有在第一个"x"后面紧跟着另一个"x"时才返回true。然而,以下代码确实解决了这个问题:

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 was wondering if those familiar with codingbat's problems might be able to help me with understanding the differences between these two solutions and why one doesn't work.
The problem:
Given a string, return true if the first instance of "x" in the string is immediately followed by another "x".

Why does the first code exit immediately after evaluating the first condition, while the other continues through the for loop? What is the difference between them?

I thought the following code would do the trick:

boolean doubleX(String str) {
  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;
  }
  return false;
}

However this code only evaluates true when the first x is followed by an x. However the following code does solve the problem:

 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;
	}

答案1

得分: 1

第一个函数对于类似&quot;xaxx&quot;的字符串返回true,其中第二个x后面跟着另一个x,但第一个x后面没有。它会继续检查后续字符,如果第一个x后面没有跟着x,而不是在达到第一个x时停止。

英文:

The first function returns true for a String like &quot;xaxx&quot;, where the second x is followed by another x, but the first x is not.

It keeps checking subsequent characters if the first x is not followed by an x, instead of stopping when the first x is reached.

答案2

得分: 0

以下是翻译好的部分:

你可以将第一个代码重写如下:

    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;
              }
          }
      }
      return false;
    }

现在你可能明白了其中的区别。这个第一个代码仅在第一个 'x' 的出现后,如果立即在 i+1 处接收到第二个 'x' 才返回 'true'。在这里没有 'false' 的返回。

而第二个代码总是在第一个 'x' 出现后立即返回 `true` 或 `false`,无论是否在 i+1 处接收到另一个 'x'。
英文:

You can re-write first code as below:

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;
          }
      }
  }
  return false;
}

Now probably you understand the difference. This first code returns 'true' for first occurrence of 'x' only if second occurrence of 'x' is received immediately at i+1. There is no 'false' return there.

While the second code always returns after first occurrence of x' as either true or false, no matter if another 'x' is received at i+1 or not.

huangapple
  • 本文由 发表于 2023年7月17日 12:40:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701526.html
匿名

发表评论

匿名网友

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

确定