Separating the Digits in an Integer - exercise from Deitel's Java book

前端 未结 10 571
忘了有多久
忘了有多久 2020-12-22 13:44

Exercise from Deitel\'s \"Java How To Program\" 10th edition:

2.30 (Separating the Digits in an Integer) Write an application that inputs one number c

相关标签:
10条回答
  • 2020-12-22 14:27

    Here is the answer of displaying digit in same order u have enter

    import java.util.Scanner;
    
    public class oddMethod {
        public static void main(String[] args) {
            System.out.println("Enter start number:");
            int number=getNumber();
            int last=Reverse1stTime(number);
            System.out.println("Digit You Enter:"+last);
        }
    
        private static int getNumber() {
            Scanner sr = new Scanner(System.in);
            return sr.nextInt();
        }
        private static int Reverse1stTime(int number)
        { int  digit=0;
        int reverse=0;
            while(number>0)
            {
                digit=number%10;
                reverse=reverse*10+digit;
                number=number/10;
            }
           return reverseAgain(reverse);
        }
        private static int reverseAgain(int number)
        { int  digit=0;
            int reverse=0;
            while(number>0)
            {
                digit=number%10;
                System.out.println(digit);
    
                reverse=(reverse*10+digit);
                number=number/10;
            }
            return reverse;
        }
    }
    Input 12345
    Output 12345
    
    0 讨论(0)
  • 2020-12-22 14:29
    package chapter1;
    
    import java.util.Scanner;
    
    public class Exercise3 {
    
        public static void main (String[] args){
        Scanner scan = new Scanner (System.in);
        System.out.print ("Enter a number that consist of 5 digits: ");
        int num =scan.nextInt();
    
        int digit1 =num / 10000;
        int digit2 =(( num % 10000) / 1000 );  // the modulus answer of num/10000 is divided by 1000
        int digit3 =( ( num % 1000) / 100 );
        int digit4 = ( ( num% 100 ) / 10 );
        int digit5 = ( num % 10);
    
        System.out.print (digit1+"\t");
        System.out.print(digit2+"\t");
        System.out.print(digit3+"\t");
        System.out.print(digit4+"\t");
        System.out.println(digit5+"\t");
        }
    
    
    }
    
    0 讨论(0)
  • 2020-12-22 14:35

    my small hint with division - you can divide by 10 and take a look, what is that operation giving... More, you have to deduce if you want to learn programming.

    0 讨论(0)
  • 2020-12-22 14:36
    import java.util.Scanner;  
    public class DigitSeparator{  
        public static void main(String[] args){  
            Scanner input = new Scanner(System.in);  
            System.out.print("Enter a 5 digit number: ");  
            int number = input.nextInt();  
    
            System.out.print((number / 10000)+ "   ");   
            int divider = 10000;  
            int mod = number % 10000;  
    
            for(int i = 1; i <= 4; i++){  
                divider /= 10;  
                System.out.print( mod/divider +"   ");  
                mod %= divider;         
            }  
            System.out.println();  
        }  
    }  
    
    0 讨论(0)
提交回复
热议问题