您当前的位置: 首页 >  Java

小志的博客

暂无认证

  • 0浏览

    0关注

    1217博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

java输入一串数字,如果是回文数返回Y,否则返回N

小志的博客 发布时间:2019-02-27 16:58:51 ,浏览量:0


package com.cn.test;

import java.util.Scanner;


public class HuiWenShuTest {
	public static void main(String[] args) {
		Scanner sc= new Scanner(System.in);
		System.out.println("请输入数字");
		int num=sc.nextInt();
		String str=test(num);
	}
	
	public static String test(int num){
		String str1=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 + "不是回文数");
				return "N";
			}else{
				count++;
			}
		}
		if(count ==str1.length()){
			System.out.println(str1 + "是回文数");
			return "Y";
		}
		return "";
	}
}

输出结果:

请输入数字
123456
123456不是回文数
N
请输入数字
123454321
123454321是回文数
Y
关注
打赏
1661269038
查看更多评论
立即登录/注册

微信扫码登录

0.1738s