如何在Java中读取数字的每个数字

时间:2016-10-04 18:48:37

标签: java while-loop

我正在尝试运行一个程序,该程序输出以整数形式输入的每个数字的总和。我如何阅读数字并输出每个数字?

示例:输入为4053,输出为“4 + 0 + 5 + 3 = 12”。

import java.util.Scanner; 

public class Digits{
    public static void main(String args[]) {
    //Scans in integer   
        Scanner stdin = new Scanner(System.in);
        System.out.println("Enter in a number: ");
        int number = stdin.nextInt();

        //Set sum to zero for reference
        int sum = 0;
        int num = number; //Set num equal to number as reference

        //reads each digit of the scanned number and individually adds them                                  together
        //as it goes through the digits, keep dividing by 10 until its 0.
        while (num > 0) {
            int lastDigit = num % 10; 
            sum = sum + lastDigit;
            num = num/10;
        }
    }
}

这是我用来计算各个数字之和的代码,现在我只需要帮助输出各个数字。任何提示和技巧都将非常感激。

5 个答案:

答案 0 :(得分:1)

import java.util.Scanner; 

public class Digits{
    public static void main(String args[]) {
    //Scans in integer   
        Scanner stdin = new Scanner(System.in);
        System.out.println("Enter in a number: ");
        int number = stdin.nextInt();

        //Set sum to zero for reference
        int sum = 0;
        int num = number; //Set num equal to number as reference

        //reads each digit of the scanned number and individually adds them                                  together
        //as it goes through the digits, keep dividing by 10 until its 0.
        String numToString = "";

        while (num > 0) {
            int lastDigit = num % 10; 
            numToString +=lastDigit+" + ";
            sum = sum + lastDigit;
            num = num/10;
        }
        //eliminate the last + sign 
        numToString = numToString.substring(0,numToString.lastIndexOf("+")).trim();
        System.out.println(numToString +" = " +sum);
    }
}

答案 1 :(得分:0)

我不确定你输出的含义是什么,但是你可以将数字作为字符串读取并取出每个字符并将其解析为整数

        Scanner stdin = new Scanner(System.in);
    System.out.println("Enter in a number: ");
    String number = stdin.next();
     int[] result = new int[number.length];
     for(int i=0;i<number.length;i++) {
         result[i] = Integer.parseInt(number.charAt(i)+"");
      }
     return result;

答案 2 :(得分:0)

您可以像String一样读取它,然后除以数字。

git xxx

答案 3 :(得分:0)

没有扫描仪,你可以做

StringBuilder sb = new StringBuilder();
String sep = "";
int ch;
long sum = 0;
while((ch = System.in.read()) > ' ') {
    if (ch < '0' || ch > '9') {
         System.out.println("Skipping " + (char) ch);
         continue;
    }
    sb.append(sep).append((char) ch);
    sep = " + ";
    sum += ch - '0';
}
sb.append(" = ").append(sum);
System.out.println(sb);

答案 4 :(得分:0)

试试这个:

import java.util.*;

public class Digits {

    public static void main(String [] args)
    {
        Scanner input = new Scanner (System.in);

        System.out.println("Enter number -> ");
        int number = input.nextInt();
        int sum = 0;

        String numStr = "" + number;

        while(number > 0)
        {
            int lastDigit = number % 10;
            sum += lastDigit;
            number = number / 10;
        }

        for(int i = 0; i < numStr.length();i++)
        {
            System.out.print(numStr.charAt(i));

            // Dont print an extra + operator at the end
            if( i == numStr.length() - 1) continue;

            else
                System.out.print(" + ");
        }

        System.out.print(" = " + sum);
    }
}