Java程序检查回文
回文数是颠倒后保持不变的数字,例如121、313、525等。
示例
现在让我们看一个检查回文的例子-
public class Palindrome {
public static void main(String[] args) {
int a = 525, revVal = 0, remainder, val;
val = a;
System.out.println("Number to be checked = "+a);
while( a != 0 ) {
remainder = a % 10;
revVal = revVal * 10 + remainder;
a /= 10;
}
if (val == revVal)
System.out.println("Palindrome!");
else
System.out.println("不是回文!");
}
}输出结果
Number to be checked = 525 Palindrome!
示例
现在让我们检查回文字符串,例如“aba”,“wow”等-
public class Demo {
public static void main (String[] args) {
String str = "ABA";
String strRev = new StringBuffer(str).reverse().toString();
if (str.equals(strRev))
System.out.println("Palindrome!");
else
System.out.println("不是回文!");
}
}输出结果
Palindrome!
热门推荐
10 毕业升高中祝福语简短
11 写信古语祝福语简短精辟
12 祝福语大全儿子结婚简短
13 姐妹结婚新婚祝福语简短
14 感谢父母祝福语大全简短
15 朋友之间祝福语大全简短
16 毕业祝福语小长句简短
17 感谢母亲祝福语 简短精辟
18 关于孩子毕业祝福语简短