一个Java程序,它获取一个整数,将各个数字分开,并给出数字的总和

时间:2018-12-04 22:40:57

标签: java

下面是我的代码,我已经能够得到数字的总和,但是我不知道如何清楚地分离并打印整数的各个数字。可以在我的“执行”循环中完成吗?

import java.util.*;

public class Ch5PgEx1 {

  static Scanner console = new Scanner(System.in);

  public static void main(String[] args) {

    int num;
    int sum;
    int remainder;
    int count;

    System.out.print("Please enter an integer: ");
    num = console.nextInt();
    System.out.println();

    sum = 0;
    count = 0;

    do {
        remainder = num % 10;
        sum = sum + remainder;
        num = num / 10;
        count++;
    }
    while (num > 0);


    System.out.print("There are " + count + " digits in your number. \nThe sum of the individual digits is:" + sum);
    System.out.println();
  } 
}

3 个答案:

答案 0 :(得分:1)

  1. 要打印它们,只需添加打印行

    do {
        remainder = num % 10;
        sum = sum + remainder;
        num = num / 10;
        count++;
        System.out.println(remainder);
    } while (num > 0);
    
    /*
    Please enter an integer: 123456789
    
    9
    8
    7
    6
    5
    ...
    
  2. 将它们存储在数组中

    int[] res = new int[Integer.toString(num).length()];
    do {
        remainder = num % 10;
        sum = sum + remainder;
        num = num / 10;
        res[count++] = remainder;  // LINE 1
    } while (num > 0);
    
    /*
    Please enter an integer: 123456789
    
    [9, 8, 7, 6, 5, 4, 3, 2, 1]
    ...
    

    [1, 2, 3, 4, 5, 6, 7, 8, 9]的方式拥有数组:

    • LINE 1更改为res[res.length - ++count] = remainder;

答案 1 :(得分:0)

不完全确定这是否是您想要的东西。它确实简化了代码,并根据余数构建了数组:

整数数组

int arraySize = String.valueOf(num).length();
List<Integer> collectList = IntStream.range(0, arraySize).map(i -> (int) (num / Math.pow(10, i) % 10)).boxed()
        .collect(Collectors.toList());
Integer[] collect = collectList.toArray(new Integer[0]);

System.out.print("There are " + collectList.size()
        + " digits in your number. \nThe sum of the individual digits is:" + collectList.stream()
        .reduce(Integer::sum).orElse(0));
System.out.println();

字符数组

如果您想使用字符,也许这也可以帮助您:

int arraySize = String.valueOf(num).length();
List<Integer> collectList = IntStream.range(0, arraySize).map(i -> (int) (num / Math.pow(10, i) % 10)).boxed()
        .collect(Collectors.toList());
Character[] collect = collectList.stream().map(number -> String.valueOf(number).charAt(0))
        .toArray(Character[]::new);

System.out.print("There are " + collectList.size()
        + " digits in your number. \nThe sum of the individual digits is:" + collectList.stream()
        .reduce(Integer::sum).orElse(0));
System.out.println();

答案 2 :(得分:0)

嗯,我为您做了一个简单的例子。看看吧。

public static void main(String[] args) {

        Scanner scanner = new Scanner(System.in);
        System.out.println("Pleaseee, input your numbers.");
        String text = scanner.nextLine();

        int[] arrayOfNumbers = new int[text.length()];

        for (int i = 0; i < text.length(); i++) {
            arrayOfNumbers[i] = text.charAt(i) - '0';
        }
        /*
        // prints every number
        for( int number : arrayOfNumbers){
            System.out.printf(" %s ",number);
        }
        */
        // sums every number
        int sumOfNumbers = IntStream.of(arrayOfNumbers).sum();

        String listOfNumbers = IntStream.of(arrayOfNumbers).mapToObj(Integer::toString).collect(Collectors.joining(", "));

        System.out.printf("Ho-ho-ho! List of numbers is: %s \n", listOfNumbers);
        System.out.printf("Ho-ho-ho! The sum of your numbers is: %d \n", sumOfNumbers);

    }