我正处于一个初级java课程中,并且在这个课程中遇到了困难。我的教授给了我们这个伪代码 - 创建字符串sInput
//
// prompt for input
//
// create char array (cArray) and assign sInput.toCharArray()
//
// loop to check for palindrome (set i = 0, j = sInput.length()-1, check to see if i != j; increment i, decrement j)
// check to see if current array values are !=
// print not a palindrome
// return
// end of loop
我结束了。谢谢你的任何建议!
我有
public static void main(String[] args) {
while (true) {
display(check(retrieveInput()));
}
}
public static String retrieveInput() {
Scanner scan = new Scanner(System.in);
return scan.next();
}
public static boolean check(String input) {
boolean check = false;
try {
Integer.parseInt(input);
if (input.charAt(0)==input.charAt(4) && input.charAt(1)==input.charAt(3))
check = true;
} catch(Exception e) {
check = false;
}
return check;
}
public static void display(boolean check) {
if(check) System.out.println("Is a five-digit palindrome.");
else System.out.println("Is not a five-digit palindrome.");
答案 0 :(得分:1)
希望这会有所帮助:
public static void main(String [] args){
boolean notPalindrome = false;
String number = "25852";
char[] array = number.toCharArray();
for(int i=0, j=array.length-1; i<j; i++, j--) {
if(array[i] != array[j]) {
notPalindrome = true;
break;
}
}
System.out.println(number + " is palindrome? " + !notPalindrome);
}