How to get the separate digits of an int number?

前端 未结 30 1961
陌清茗
陌清茗 2020-11-22 03:03

I have numbers like 1100, 1002, 1022 etc. I would like to have the individual digits, for example for the first number 1100 I want to have 1, 1, 0, 0.

How can I get

30条回答
  •  悲哀的现实
    2020-11-22 03:13

    I see all the answer are ugly and not very clean.

    I suggest you use a little bit of recursion to solve your problem. This post is very old, but it might be helpful to future coders.

    public static void recursion(int number) {
        if(number > 0) {
            recursion(number/10);
            System.out.printf("%d   ", (number%10));
        }
    }
    

    Output:

    Input: 12345
    
    Output: 1   2   3   4   5 
    

提交回复
热议问题