对于我正在使用的类,我将创建一个程序来测试字符串是否是回文。我们应该每次只使用8个字符的字符串,并以这种方式对其进行硬编码,但是我想要超越并做出测试任何字符串的东西。不幸的是,这段代码似乎总是返回真实的,我真的不知道为什么。字符数组和循环错误
public static boolean palindromeTest(String input){
//This portion declares variables necessary for testing, and modifies them if necessary.
int inputLength=input.length();
char[] Chars=input.toCharArray();
for(int j=0; j<inputLength; j++){
Character.toLowerCase(Chars[j]); //makes all characters in input lower case
//This portion performs the palindrome test
}
if(inputLength%2>0){ //if length is odd
inputLength=(inputLength-1)/2;
for(int i=0; i>0; i++){
if(Chars[i]!=Chars[inputLength-i]) //tests equality first and last in pairs via for loop
return false; //break;
}
}else{ //if length is even
inputLength=(inputLength)/2;
for(int i=0; i>0; i++){
if(Chars[i]!=Chars[inputLength-i]) //tests equality first and last in pairs via for loop
return false; //break;
}
}
return true; //if all tests are passed, input is indeed a palindrome
}
yup,循环迭代次数的上限需要改变。 –
OHHH谢谢,我不知道为什么我这么说,我的意思是我
@PeaceBlaster:请编辑您的问题。 –