英文:
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() >= 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;
}
These two produced the same error with this test
doubleX("axxbb") → 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 &&
and nested if statements?
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;
}
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 循环中发生了以下情况:
- 你初始化了
i=0
- 它检查条件
str.charAt(i) == 'x' && str.charAt(i+1) == 'x'
- 条件为 false - 条件为 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 循环的第一次迭代将执行以下操作:
- 检查是否
'a'=='x'
并且'x'=='x'
- 条件不为真,因此直接进入 else 语句以执行其中的代码 - 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() >= 2){
for (int i = 0; str.charAt(i) == 'x' && str.charAt(i+1) == 'x'; i++){
return true;
}
}
return false;
}
This is what's happening in the for loop:
- you're initializing
i=0
- it checks the condition
str.charAt(i) == 'x' && str.charAt(i+1) == 'x'
- the condition is false - 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() >= 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;
}
This is what the first iteration of the for loop will do:
- check if
'a'=='x'
and'x'=='x'
- the condition is not true so it goes straight to the else statement to execute the code there - 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) == 'x')
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论