Write the methods with the following headers // Return the reversal of an integer, i.e., reverse(456) returns 654 int reverse(int number) // Return true if number is a palindrome int isPalindrome(int number) Use the reverse method to implement isPalindrome. A number is a palindrome if its reversal is the same as itself. Write a test program that prompts the user to enter an integer and reports whether the integer is a palindrome.
时间: 2024-02-27 11:57:21 浏览: 95
Sure! Here are the methods you requested:
```java
public static int reverse(int number) {
int reversedNumber = 0;
while (number != 0) {
reversedNumber = reversedNumber * 10 + number % 10;
number /= 10;
}
return reversedNumber;
}
public static boolean isPalindrome(int number) {
return number == reverse(number);
}
```
And here's a program that uses these methods to check if a user-entered integer is a palindrome:
```java
import java.util.Scanner;
public class PalindromeChecker {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.print("Enter an integer: ");
int number = input.nextInt();
if (isPalindrome(number)) {
System.out.println(number + " is a palindrome");
} else {
System.out.println(number + " is not a palindrome");
}
}
}
```
Hope this helps! Let me know if you have any further questions.
阅读全文