代码实现:给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。

import java.util.Scanner;
//给一个不多于5位的正整数,要求:一、求它是几位数,二、逆序打印出各位数字。
public class Test {
	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		int num = 0 ;
		String s = "0";
		System.out.println("请一个输入整数:");
		while (true) {
			s = sc.nextLine();
			try {
				 num = Integer.parseInt(s);
				break;
			} catch (NumberFormatException e) {

				System.out.println("输入非法,请重新输入整数:");
			}
		}
		System.out.println(num+"整数的长度为:"+s.length());
		StringBuffer sb = new StringBuffer();
		sb.append(s);
		sb.reverse();
		System.out.println(sb.toString());		
	}
}
原文地址:https://www.cnblogs.com/loaderman/p/6527472.html