How to get the separate digits of an int number?

前端 未结 30 2567
陌清茗
陌清茗 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:23

    import java.util.Scanner;
    
    class  Test 
    {  
        public static void main(String[] args)   
        {  
            Scanner sc = new Scanner(System.in); 
    
    
        int num=sc.nextInt(); 
        System.out.println("Enter a number (-1 to end):"+num);
        int result=0;
        int i=0;
        while(true) 
        { 
          int n=num%10;
          if(n==-1){
            break;
          }
          i++;
          System.out.println("Digit"+i+" = "+n);
          result=result*10+n;
          num=num/10; 
    
    
          if(num==0) 
          { 
            break; 
          } 
        }
        }
    }
    

提交回复
热议问题