题解 | 反向输出一个四位数

反向输出一个四位数

https://www.nowcoder.com/practice/1f7c1d67446e4361bf4af67c08e0b8b0

import java.util.Scanner;

// 注意类名必须为 Main, 不要有任何 package xxx 信息
public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int a = scanner.nextInt();
        int length = getNumberLength(a);
        int[] number = new int[length];

        int temp = Math.abs(a);

        for (int i = length - 1; i >= 0; i--) {
            number[i] = temp % 10; 
            temp = temp / 10;      
        }

        for (int i = length - 1; i >= 0; i--) {
            System.out.print(number[i]);
        }
    }


    public static int getNumberLength(int num) {
        if (num == 0) {
            return 1;
        }
        num = Math.abs(num);
        int count = 0;
        while (num > 0) {
            num = num / 10;
            count++;
        }
        return count;
    }
}

全部评论

相关推荐

02-26 09:15
已编辑
蚌埠学院 golang
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务