我有一個方法,檢查一個int從其他方法讀入並將其與其相反。如果數字相等並且因此是迴文,則返回true。但是,無論輸入的數字是否爲迴文,錯誤都會一直返回。等於int返回false
public class Paladin
{
public static void main()
{
boolean valid;
String inputString = JOptionPane.showInputDialog("Enter a number to be reversed: ");
int inputInt = Integer.parseInt(inputString);
valid = isPalindrome(Reverse(inputInt));
if(valid)
JOptionPane.showMessageDialog(null, inputInt + " is a palindrome");
else
JOptionPane.showMessageDialog(null, inputInt + " is not a palindrome");
public static boolean isPalindrome (int number)
{
int undoReverse = 0;
while(number > 0)
{
undoReverse = undoReverse * 10 + number % 10;
number /= 10;
}
if(number == undoReverse)
return true;
else
return false;
}
}
}