回文数(java编程)

第一种方法的代码如下:
import javax.swing.JOptionPane;
public class Palindrome
{
public static void main(String [] args)
{
String inputValue = JOptionPane.showInputDialog("请输入一个整数");
long inputValueLong = Long.parseLong(inputValue);
long temp = inputValueLong;
long reverseLong = 0L;
while(inputValueLong != 0)
{
reverseLong = reverseLong*10+inputValueLong%10;
inputValueLong = inputValueLong/10;
}
if(reverseLong == temp)
System.out.println("你输入的是回文数");
else
System.out.println("你输入的不是回文数");
}
}
第二种方法的代码如下:
import javax.swing.JOptionPane;
public class PalindromeUseString
{
public static void main(String [] args)
{
String inputValue = JOptionPane.showInputDialog("请输入一个整数");
StringBuffer source = new StringBuffer(inputValue);
source.reverse();
String reverseString = new String(source);
if(reverseString.equals(inputValue))
System.out.println("你输入的是回文数");
else
System.out.println("你输入的不是回文数");
}
}

这两种方法都能实现对回文数的判断,第一种只能针对整数,第二种似乎能针对所有的字符。

相关主题
相关文档
最新文档