For some reason my for loop is not running and I can't seem to figure out why. It keeps skipping straight to the return statement. Thanks.
出于某种原因,我的for循环没有运行,我似乎无法找出原因。它一直跳到return语句。谢谢。
public char[] palindromeCheck(char[] original) {
char[] reversed = new char[original.length];
for (int i = original.length; i <0; i--) {
switch (original[i]){
case 'A':
original[i] = Character.toLowerCase(original[i]);
break;
case 'E':
original[i] = Character.toLowerCase(original[i]);
break;
case 'I':
original[i] = Character.toLowerCase(original[i]);
break;
case 'O':
original[i] = Character.toLowerCase(original[i]);
break;
case 'U':
original[i] = Character.toLowerCase(original[i]);
break;
default:
original[i] = Character.toUpperCase(original[i]);
break;
}
reversed[i] = original[i];
}
return reversed;
}
6 个解决方案
#1
6
for (int i = original.length; i <0; i--)
The condition is always false since a length is always >= 0. Since you're indexing an array, you probably want to loop from length-1 to 0 like
条件总是假的,因为长度总是> = 0.因为你正在索引一个数组,你可能想要从length-1循环到0
for (int i = original.length - 1; i >= 0; i--)
#2
1
Change:
for (int i = original.length; i < 0; i--) {
to:
for (int i = original.length; i >= 0; i--) {
The other posts are correctly addressing the issue, but changing your condition so the loop will not reach 0. If you want to reach 0, use >=
as above.
其他帖子正确解决了这个问题,但改变了你的条件,所以循环不会达到0.如果要达到0,请使用> =,如上所述。
#3
1
Change:
for (int i = original.length; i <0; i--) {
to:
for (int i = original.length; i >= 0; i--) {
Otherwise it just doesn't enter the loop, since the length is bigger than zero and the condition will always evaluate to false
.
否则它只是不进入循环,因为长度大于零并且条件总是评估为假。
#4
0
probably you have written wrong condition in for loop which will always false
可能你在for循环中写了错误的条件,总是假的
for (int i = original.length; i <0; i--)
a length is always >= 0. I think you should write i > 0
.
长度总是> = 0.我认为你应该写i> 0。
#5
0
The for loop had the ending expression reversed.
for循环使结束表达式反转。
public char[] palindromeCheck(char[] original) {
char[] reversed = new char[original.length];
for (int i = original.length; i>0; i--) {
switch (original[i]){
case 'A':
original[i] = Character.toLowerCase(original[i]);
break;
case 'E':
original[i] = Character.toLowerCase(original[i]);
break;
case 'I':
original[i] = Character.toLowerCase(original[i]);
break;
case 'O':
original[i] = Character.toLowerCase(original[i]);
break;
case 'U':
original[i] = Character.toLowerCase(original[i]);
break;
default:
original[i] = Character.toUpperCase(original[i]);
break;
}
reversed[i] = original[i];
}
return reversed;
}
#6
0
You need to be careful, when you write the conditions in the For Loop. The code below works for your case.
在For循环中编写条件时需要注意。以下代码适用于您的情况。
for (int i = original.length; i >0; i--) {
//Your code
}
In future, while you are in debug mode. Learn how to identify these mistakes.In this case you will notice, that the control directly jumps out of the for loop and analyze each single conditions inside the loop.
将来,当您处于调试模式时。学习如何识别这些错误。在这种情况下,您会注意到控件直接跳出for循环并分析循环内的每个单独条件。
#1
6
for (int i = original.length; i <0; i--)
The condition is always false since a length is always >= 0. Since you're indexing an array, you probably want to loop from length-1 to 0 like
条件总是假的,因为长度总是> = 0.因为你正在索引一个数组,你可能想要从length-1循环到0
for (int i = original.length - 1; i >= 0; i--)
#2
1
Change:
for (int i = original.length; i < 0; i--) {
to:
for (int i = original.length; i >= 0; i--) {
The other posts are correctly addressing the issue, but changing your condition so the loop will not reach 0. If you want to reach 0, use >=
as above.
其他帖子正确解决了这个问题,但改变了你的条件,所以循环不会达到0.如果要达到0,请使用> =,如上所述。
#3
1
Change:
for (int i = original.length; i <0; i--) {
to:
for (int i = original.length; i >= 0; i--) {
Otherwise it just doesn't enter the loop, since the length is bigger than zero and the condition will always evaluate to false
.
否则它只是不进入循环,因为长度大于零并且条件总是评估为假。
#4
0
probably you have written wrong condition in for loop which will always false
可能你在for循环中写了错误的条件,总是假的
for (int i = original.length; i <0; i--)
a length is always >= 0. I think you should write i > 0
.
长度总是> = 0.我认为你应该写i> 0。
#5
0
The for loop had the ending expression reversed.
for循环使结束表达式反转。
public char[] palindromeCheck(char[] original) {
char[] reversed = new char[original.length];
for (int i = original.length; i>0; i--) {
switch (original[i]){
case 'A':
original[i] = Character.toLowerCase(original[i]);
break;
case 'E':
original[i] = Character.toLowerCase(original[i]);
break;
case 'I':
original[i] = Character.toLowerCase(original[i]);
break;
case 'O':
original[i] = Character.toLowerCase(original[i]);
break;
case 'U':
original[i] = Character.toLowerCase(original[i]);
break;
default:
original[i] = Character.toUpperCase(original[i]);
break;
}
reversed[i] = original[i];
}
return reversed;
}
#6
0
You need to be careful, when you write the conditions in the For Loop. The code below works for your case.
在For循环中编写条件时需要注意。以下代码适用于您的情况。
for (int i = original.length; i >0; i--) {
//Your code
}
In future, while you are in debug mode. Learn how to identify these mistakes.In this case you will notice, that the control directly jumps out of the for loop and analyze each single conditions inside the loop.
将来,当您处于调试模式时。学习如何识别这些错误。在这种情况下,您会注意到控件直接跳出for循环并分析循环内的每个单独条件。