判断位数 + 逆序输出

import java.util.Scanner;

/**
 * @ClassName: Que1
 * @Description: 
 *  ●题目:判断一个整数是几位数,并按照逆序输出。
 *  ●考点:流程控制、运算符、数组
 * @Author: Wanglt   
 * @CreateDate: 2020年2月26日   
 *
 */
public class Que2 {
	public static void main(String[] args) {
		System.out.println("请输入一个整数(不小于0)");
		Scanner scanner = new Scanner(System.in);
		int num = scanner.nextInt();
		System.out.println("输入的是 " + howManyNumbers(num) + "位数");
		System.out.println("逆序数是 " + reversePrint(num));
	}

	/**
	 * @param num
	 */
	private static String reversePrint(int num) {
		if (num < 0) {
			return "参数非法";
		}
		if (num == 0) {
			return "0";
		}
		String reverseStr = "";
		while (num != 0) {
			reverseStr += num % 10;
			num /= 10;
		}
		return reverseStr;

	}

	/**
	 * @param num
	 * @return
	 */
	private static int howManyNumbers(int num) {
		if (num < 0) {
			return -1;
		}
		String strNum = String.valueOf(num);
		return strNum.length();
	}
}

你可能感兴趣的:(#,CodeTraining,java)