- //回文数:是一种数字。如:98789, 这个数字正读是98789,倒读也是98789
- //正读倒读一样,所以这个数字就是回文数
- import java.util.Scanner;
- public class HuiWenShu {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- System.out.println("请输入一个整数:");
- int num = sc.nextInt();
- String str1 = num + "";
- // String str1 = Integer.toString(num);
- // String str1 = String.valueOf(num);
- StringBuilder str2 = new StringBuilder(str1);
- str2.reverse();
- int count = 0;
- for (int i = 0; i < str1.length(); i++) {
- if (str1.charAt(i) != str2.charAt(i)) {
- System.out.println(str1 + "不是回文数");
- break;
- } else {
- count++;
- }
- }
- if (count == str1.length()) {
- System.out.println(str1 + "是回文数");
- }
- }
- }
java 回文数
最新推荐文章于 2024-02-29 18:56:38 发布