1 import java.util.Scanner; 2 3 public class Solution 4 { 5 public static void main(String[] args) 6 { 7 Scanner input = new Scanner(System.in); 8 System.out.print("Enter a number: "); 9 int number = input.nextInt(); 10 11 input.close(); 12 13 System.out.println(number + " is a palindrome: " + isPalindrome(number)); 14 } 15 16 public static int reverse(int number) 17 { 18 String strOfNumber = ""; 19 if(number < 10) 20 strOfNumber += number + ""; 21 else 22 strOfNumber += "" + number % 10 + reverse(number / 10); 23 return Integer.parseInt(strOfNumber); 24 } 25 26 public static boolean isPalindrome(int number) 27 { 28 int backupNumber = reverse(number); 29 if(number == backupNumber) 30 return true; 31 else 32 return false; 33 } 34 }
时间: 2024-11-10 08:10:22