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

前端 未结 10 570
忘了有多久
忘了有多久 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:17

    I also just started with java and with this book, and this is the code that did the tric for me. Forgive me if i did something strange... :)

    import java.util.Scanner;
    
    public class SeparateNumber
    {
        public static void main(String[] args)
        {
            Scanner in = new Scanner(System.in);
            int nr, nr1, nr2, nr3, nr4, nr5;
    
            System.out.print("Enter a number with 5 digits: ");
            nr = in.nextInt();
    
            nr1 = nr / 10000;
            nr2 = (nr % 10000) / 1000;
            nr3 = ((nr % 10000) % 1000) / 100;
            nr4 = (((nr % 10000) % 1000) % 100) / 10;
            nr5 = (((nr % 10000) % 1000) % 100) % 10;
    
            System.out.printf("%d%s%d%s%d%s%d%s%d%n", nr1, " ", nr2, " ", nr3, " ", nr4, " ", nr5);
        }
    }
    
    0 讨论(0)
  • 2020-12-22 14:24

    Below code handles splitting on any int in integer range.

    Steps:

    1. Convert int to String
    2. Convert each character of String to int by using Integer.parseInt
    3. Store int digits in int[]
    4. Print int[]

    Sample code:

    import java.util.*;
    
    public class SplitDigits{
        public static void main(String args[]) throws Exception {
            System.out.println("Enter number:");
            Scanner sc = new Scanner(System.in);
            int number = sc.nextInt();
            System.out.println("You have entered:"+number);
            String str = String.valueOf(number);
            int length = str.length();
            if ( length > 0){
                int[] digits = new int[length];
                for ( int i=0; i < length; i++){
                    digits[i] = Integer.parseInt(String.valueOf(str.charAt(i)));
                }
                for ( int i=0; i < digits.length; i++){
                    System.out.println("Digits:"+digits[i]);
                }
            }
    
        }
    }
    

    output:

    java SplitDigits
    Enter number:
    123456789
    You have entered:123456789
    Digits:1
    Digits:2
    Digits:3
    Digits:4
    Digits:5
    Digits:6
    Digits:7
    Digits:8
    Digits:9
    
    0 讨论(0)
  • 2020-12-22 14:25

    You know that the number is 5 digit long.

    What about

    number / 10 000 to retrieve the first digit.
    number = reminder
    number / 1000 to retrieve the second digit.
    number = reminder
    number / 100 to retrieve the third digit.
    number = reminder
    number / 10 to retrieve the fourth digit.
    and reminder is the 5th one.
    
    0 讨论(0)
  • 2020-12-22 14:25

    Try something like this

    String str1 = "";
    int a = 12345;
    while(a>0)
    {
      int b = a%10;
      str1 = str1 + String.valueOf(b+" ");
      a = a/10;
    }
    StringBuilder str = new StringBuilder(str1);
    System.out.println(str.reverse());
    
    0 讨论(0)
  • 2020-12-22 14:25

    Answer is:

    //your package name
    
    import java.util.Scanner; //for input the userin
    public class SeperateDigit {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
    
            System.out.printf("Enter a 5 digit number: ");
            int number = input.nextInt();
    
            int number1 = number/100000;
            int arrange = number%10000;
            int number2 = arrange/1000;
            int number3 = (arrange % 1000) /100;
            int number4 =(( arrange % 1000) % 100) / 10;
            int number5 = ((arrange % 1000) % 100) % 10;
    
            System.out.printf("%d %d %d %d %d", number1,number2,number3,number4, number5);
        }
    }
    
    

    and Result is

    Enter a 5 digit number: 422339
    4 2 3 3 9
    
    0 讨论(0)
  • 2020-12-22 14:26

    Try this:

    import java.util.*;
    public class DigitsDisplay
    {
        public static void main (String[] args)
        {
            Scanner input = new Scanner (System.in);
            int digit;
            System.out.print("Enter a positive number: ");
            digit = input.nextInt();
            int power = 1;
            while (power <= digit) {
                power *= 10;
            }
            power /= 10;
            while (power > 0) {
                System.out.println(digit/power);
                digit %= power;
                power /= 10;
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题