判断回文数方式

import java.util.Scanner;

public class WordsTest {
public static void main(String[] args) {
Scanner reader=new Scanner(System.in);
System.out.println("请输入一个字符串");
String s1=reader.next();
StringBuffer str=new StringBuffer(s1);
String s2=(str.reverse()).toString();
if(s1.equals(s2)){
System.out.println("该字符串回文");
}
else System.out.println("该字符串不回文");

}
}

2》。

       Scanner reader=new Scanner(System.in);
        System.out.println("请输入一个数字");
        int s1=reader.nextInt();

        String str1=String.valueOf(s1);
        StringBuffer str2=new StringBuffer(str1);
        str2.reverse();
        int count=0;
        for(int i=0;i<str1.length();i++){
            if(str1.charAt(i)!=str2.charAt(i)){
                System.out.println("不是回文数");
                break;
    }else{
        count++;
    }           
}
        if(count==str1.length()){
            System.out.println("是回文数");
        }
       

猜你喜欢

转载自blog.csdn.net/weixin_42597509/article/details/81131260