对于我正在上的一门课,我将创建一个程序来测试字符串是否是回文。我们应该每次只使用一个 8 个字符的字符串并以这种方式对其进行硬编码,但我想超越并制作一些东西来测试任何字符串。不幸的是,这段代码似乎返回 true,老实说,我不确定为什么。
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
}