public class Palindrome
{
public static void main ( String [] args )
{
Scanner scan = new Scanner ( System.in ) ; // new object named "scan"
System.out.println ( "\nPlease enter a number: " ) ;
int num = scan.nextInt () ; //store number into num
if ( ( (num /1000)==(num%10) )
&& ( ( (num/10) %10 ) == ( (num/100)%10 ) ) )
// num = 'abcd' : if (a=d) and (b=c) number is a palindrome.
System.out.println ( "\nIs a Palindrome !" ) ;
else
System.out.println ( "\nNot a Palindrome !" ) ;
} //end of method main
} //end of class Palindrome
答案 0 :(得分:3)
将任务拆分为更小,更易于管理的部分,您可以解决这些问题。例如:
检查字符串是否长度为4:
static boolean is4Digit(String p) {
return p.length()==4;
}
检查数字是否在两个数字之间:
static boolean isBetween(int nr, int low, int hi) {
return nr>low && nr<hi;
}
检查字符串是否为回文:
static boolean isPalindrome(String p) {
return p.equals(new StringBuilder(p).reverse().toString());
}
答案 1 :(得分:2)
如何添加一个真实数字的检查:第一个是正数,第二个是4位数?
我可以用两种方式解释4位非负整数。
0 <= n && n <= 9999
或
1000 <= n && n <= 9999
为了优化,我不打算从字符串解析它:
BufferedReader bufferedInput = new BufferedReader(System.in);
String line = bufferedInput.readLine();
if (line == null) { /* handle end of input case */ }
String number = line.trim();
// Check if its a palindrome
if (number.length() == 4
&& number.charAt(0) == number.charAt(3)
&& number.charAt(1) == number.charAt(2)) {
// Finally check that the characters are all digits
boolean isLegal = true;
for (int i = (number.length() + 1) / 2; --i >= 0;) {
char shouldBeADigit = number.charAt(i);
if (!('0' <= shouldBeADigit && shouldBeADigit <= '9')) {
isLegal = false;
break;
}
}
if (isLegal) {
System.out.println(number + " is a palindrome");
}
}
答案 2 :(得分:0)
更可读的方式是:
Scanner scan = new Scanner ( System.in ) ; // new object named "scan"
System.out.println ( "\nPlease enter a number: " ) ;
int num = scan.nextInt () ; //store number into num
if(num<0 || num>9999)
System.out.println("Only positive numbers with 4 digits are accepted.");
else {
int help=num;
int rev=0;
while (help > 0) {
rev = rev * 10 + help %10;
help = help / 10;
}
System.out.println("Is a palindrome: "+ (rev==num));
}
答案 3 :(得分:0)
检查输入的数字
public static boolean isNumeric(String str)
{
NumberFormat formatter = NumberFormat.getInstance();
ParsePosition pos = new ParsePosition(0);
formatter.parse(str, pos);
return str.length() == pos.getIndex();
}
答案 4 :(得分:0)
纯粹的数学实现isPalindrome(没有字符串转换)比Chris更有效(虽然当然更不直观/可读):
public static boolean isPalindrome(int number) {
if(number < 0){
throw new IllegalArgumentException("number must be positive!");
}
int powerOf10 = (int)Math.pow(10, (int)Math.floor(Math.log10(number)));
while(powerOf10 >= 10) {
number -= powerOf10 * (number % 10);
if(number < 0 || number >= powerOf10) {
return false;
}
number /= 10;
powerOf10 /= 100;
}
return true;
}
由于这是一个家庭作业问题,我将解释为什么这对读者来说是一种练习。
答案 5 :(得分:0)
您也可以使用带有捕获组和反向引用的正则表达式来执行此操作。考虑这个正则表达式捕获两个数字,然后以相反的顺序捕获两个等于前两个的数字:
private static String PALINDROME = "(\\d)(\\d)(?:\\2)(?:\\1)";
public boolean isPalindrome(String testStr) throws NoMatchFoundException {
Pattern pattern = Pattern.compile(PALINDROME);
Matcher matcher = pattern.matcher(testStr);
return matcher.matches();
}