How to get the separate digits of an int number?

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

    import java.util.Scanner;
    
    public class SeparatingDigits {
    
        public static void main( String[] args )
        {
    
            System.out.print( "Enter the digit to print separately :- ");
            Scanner scan = new Scanner( System.in );
    
            int element1 = scan.nextInt();
            int divider;
    
            if( ( element1 > 9999 ) && ( element1 <= 99999 ) )
            {
                divider = 10000;
            }
            else if( ( element1 > 999 ) && ( element1 <= 9999 ) )
            {
                divider = 1000;
            }
            else if ( ( element1 > 99) && ( element1 <= 999 ) )
            {
                divider = 100;
            }
            else if( ( element1 > 9 ) && ( element1 <= 99 ) )
            {
                divider = 10;
            }
            else 
            {
                divider = 1;
            }
    
            quotientFinder( element1, divider );
    
    
    
    
        }
    
         public static void quotientFinder( int elementValue, int dividerValue )
         {
             for( int count = 1;  dividerValue != 0; count++)
             {
                int quotientValue = elementValue / dividerValue ;
                elementValue = elementValue % dividerValue ;
                System.out.printf( "%d  ", quotientValue );
    
                dividerValue /= 10;
    
             }
         }
        }
    

    Without using arrays and Strings . ( digits 1-99999 )

    output :

    Enter the digit to print separately :- 12345

    1 2 3 4 5

提交回复
热议问题