24 给一个不多于5位的正整数
2023-10-11 本文已影响0人
北极的大企鹅
题目:
- 给一个不多于5位的正整数.
- 要求一:求它是几位数
- 要求二:逆序打印出各位数字
1 public class _024PrintAFigure {
2
3 public static void main(String[] args) {
4 while (true) {
5 printAFigure();
6 }
7 }
8
9 private static void printAFigure() {
10 Scanner scanner = new Scanner(System.in);
11 System.out.println("请输入不多于五位的正整数:");
12 long a = scanner.nextLong();
13 String string = Long.toString(a);
14 char[] c = string.toCharArray();
15 int j = c.length;
16 if (j <= 5) {
17 System.out.println(a + "是一个" + j + "位数.");
18 System.out.println("按逆序输出是:");
19 for (int i = j - 1; i >= 0; i--) {
20 System.out.print(c[i] + " ");
21 }
22 System.out.println();
23 } else {
24 System.out.println("超出了5位数");
25 }
26 }
27
28 }