作为作业,我要写两种方法;一种是使用 public static int(int number) 的反转方法,而另一种是使用 public static boolean isPalindrome(int number) 的回文方法。我已经为此工作了几个小时,老实说,我很难过。我不是要求为我完成我的作业,只是帮助了解从这里去哪里。谢谢。我当前的代码如下;
public class Exercise
{
public static void main(String[] args)
{
System.out.println("Please enter an integer. ");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
boolean Final = isPalindrome(number);
System.out.println(Final);
}
public static int reverse(int number)
{ // missing return?
int y;
int n;
for (n = 0; n <= number; n++)
{ // parameters
y = number % 10; // remainder
number = number / 10; // gets rid of last digit
n = n * 10 + y; // sets reverse values
return n; // returns reversed number
}
}
public static boolean isPalindrome(int number)
{
int n = reverse(number); // call reverse method
boolean result; // declare result
if (n = number)
{ // incompatible types?
result = true;
System.out.println("The number " + number + " is a " + "Palindrome" + ".");
}
else if (n != number)
{
result = false;
System.out.println("The number " + number + " is a Palindrome" + ".");
}
return result; // not initialized?
}
}