Separating the Digits in an Integer - Deitel's Java book 中的练习

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25204481/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 07:34:28  来源:igfitidea点击:

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

java

提问by howlfascinated

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

来自 Deitel 的“Java How To Program”第 10 版的练习:

2.30 (Separating the Digits in an Integer) Write an application that inputs one number consisting of five digits from the user, separates the number into its individual digits and prints the digits separated from one another by three spaces each. For example, if the user types in the number 42339, the program should print

2.30(分隔整数中的数字)编写一个应用程序,从用户输入一个由 5 位数字组成的数字,将数字分隔成单独的数字,然后打印每个数字之间用三个空格分隔的数字。例如,如果用户输入数字 42339,程序应该打印

4   2   3   3   9

Assume that the user enters the correct number of digits. What happens when you enter a number with more than five digits? What happens when you enter a number with fewer than five digits?[Hint: It's possible to do this exercise with the techniques you learned in this chapter. You'll need to use both division and reminder operations to "pick off" each digit.]

假设用户输入了正确的位数。当您输入一个超过五位数的数字时会发生什么?当你输入一个少于五位数的数字时会发生什么?[提示:可以用你在本章中学到的技巧来做这个练习。您需要同时使用除法和提醒操作来“挑选”每个数字。]

Could someone explain to me how should I go about "picking off" individual integers using division and reminder operators?

有人可以向我解释我应该如何使用除法和提醒运算符“挑选”单个整数?

EDIT: control structures (if / else and the like) are not allowed yet, those are explored in future chapters. Variables, arithmetic operators and comparison operators only.

编辑:尚不允许使用控制结构(if / else 等),这些将在以后的章节中进行探讨。仅变量、算术运算符和比较运算符。

回答by Lo?c Gammaitoni

You know that the number is 5 digit long.

你知道这个数字是 5 位数字。

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.

回答by Krystian

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.

我对除法的小提示 - 你可以除以 10 并看看,这个运算给出了什么...... 更多,如果你想学习编程,你必须推断。

回答by W. Clemons

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;
        }
    }
}

回答by Ottoman079

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... :)

我也刚开始使用 java 和这本书,这是为我完成这项工作的代码。如果我做了一些奇怪的事情,请原谅我...... :)

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);
    }
}

回答by Ravindra babu

Below code handles splitting on any int in integer range.

下面的代码处理整数范围内任何 int 的拆分。

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[]
  1. 将整数转换为字符串
  2. 使用 Integer.parseInt 将 String 的每个字符转换为 int
  3. 在 int[] 中存储 int 数字
  4. 打印 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

回答by SparkOn

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());

回答by Lehka

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");
    }


}

回答by Md. Mahbubur Rahman

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