作为家庭作业我要写两种方法;一个是使用public static int(int number)的反转方法,而另一个是使用public static boolean isPalindrome(int number)的Palindrome方法。我一直在研究这个问题几个小时,老实说我很难过。我不是要求为我完成作业,只是帮助理解从这里开始的地方。谢谢。我目前的代码如下;
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?
}
}
答案 0 :(得分:3)
您的return n;
位于for
循环内。只需将花括号移到它上面即可。
另外,将for
循环更改为while (number > 0) {
将(n = number)
更改为(n == number)
此外,删除if
中的第二个isPalindrome
条件,您不需要它。只需添加else
。这将使编译器告诉您return result;
无法访问...只需删除它。
答案 1 :(得分:1)
反向逻辑中的问题。
试试这个
public static int reverse(int number)
{
int result = 0;
int remainder;
while (number > 0)
{
remainder = number % 10;
number = number / 10;
result = result * 10 + remainder;
}
return result;
}
答案 2 :(得分:1)
使用字符串来处理回文IMO更容易。
Souce帖子: How do you determine if a String is a palindrome?
int someNumber = 12321;
String palindrome = someNumber+"";
boolean isPalindrome = palindrome.equals(new StringBuilder(palindrome).reverse().toString());
使用您的代码,它将是:
public static void main(String[] args)
{
System.out.println("Please enter an integer. ");
Scanner input = new Scanner(System.in);
int number = input.nextInt();
String palindrome = number+"";
boolean result = palindrome.equals(new StringBuilder(palindrome).reverse().toString());
System.out.println(result);
}
答案 3 :(得分:1)
/ * Palindrome程序使用方法和接口* /
import java.util.Scanner;
interface Test // All methods inside interface are treated "Public"
{ // and abstract in nature by default
String getInput();
boolean IsPalindrome(String s);
void displayInput();
}
abstract class Sub_Test implements Test
{
public String getInput()
{
Scanner scan = new Scanner(System.in);
System.out.println("Enter a String ");
String str = scan.nextLine();
return str;
}
public boolean IsPalindrome(String s)
{
int limit = s.length() - 1;
char st1 [] = s.toCharArray();
Inner:for( int i = 0,j = limit; i < j ; i++,j--)
{
if(st1[i] == st1[j])
return true;
else
break Inner;
}
return false;
}
public void displayInput()
{
String input = getInput();
boolean word = IsPalindrome(input);
if(word)
System.out.println("Given String "+input+" is palindrome ");
else
System.out.println("Given String "+input+" is NOT palindrome ");
} // End of displayInput() method
} // End of Sub_test abstract class
public class String_Session1 extends Sub_Test {
public static void main(String[] args) {
// TODO Auto-generated method stub
String_Session1 object = new String_Session1();
object.displayInput();
}
} // End of String-Session1 Main class
/ *输出如下* /
Enter a String
Race CAR
Given String Race CAR is palindrome
答案 4 :(得分:0)
如果您正在编写号码回文。这是小费:
布尔方法
public static boolean ispalindrome(int number)
{
int num=reverse(number);
boolean res;
if (num == number){
res=true;
System.out.println("The Entered number "+number+ " is a palindrome");}
else {
res=false;
System.out.println("The Entered number "+number+" is not a palindrome");}
return res;
}
在你的主要方法中,
boolean Res=ispallindrome(number);
可用于检查病情。
答案 5 :(得分:0)
只需通过数学函数得到数字的数字计数,然后使用&#39; /&#39;进行迭代。和&#39;%&#39;操作如下。 在x =(x%divider)/ 10之后,我们应该将除法器除以100,因为我们进行了2次零操作。
public static boolean isPalindrome(int x) {
if (x < 0) return false;
if (x < 10) return true;
int numDigits = (int)(Math.log10(x)+1);
int divider = (int) (Math.pow(10, numDigits - 1));
for (int i = 0; i < numDigits / 2; i++) {
if (x / divider != x % 10)
return false;
x = (x % divider) / 10;
divider /= 100;
}
return true;
}