需要帮助编写一个添加多个数字的java程序?

时间:2015-03-31 01:21:12

标签: java

我正在尝试编写这个java程序,它要求用户输入一个数字并计算一个数字所具有的位数,并将每个数字乘以它的十进制值。例如,我输入546:程序应该说这个数字有3位数,应该相乘:
        5 * 100 = 500
4 * 10 = 40
1 * 6 = 6

到目前为止,这是我的代码:我对此代码遇到的问题是它没有计算正确的数字量。如果我输入545,它表示只有一个数字,当它分开时,它没有给出正确的答案。

public static void main(String[] args) {
    Scanner keyboard = new Scanner(System.in);
    System.out.print("Please enter a number: ");
    int n = keyboard.nextInt();
    int i;
    for (i = 0; n > 0; i++) {
        n /= 10;
        for (i = 0; n > 0; i++) {
            n /= 10;
            System.out.println((n%100000) / 10000);
            System.out.println((n%10000) / 1000);                 
            System.out.println((n%1000) / 100);
            System.out.println((n%100) / 10);
            System.out.println(n%10);
        }
        System.out.println("Number of digits: " + i);
    }
}

5 个答案:

答案 0 :(得分:0)

要获取用户输入值中的位数,您可以获取值并在将其转换为String后检查值的长度。这将为您提供数字的确切长度。

例如,如果变量" uservalue"在下面的代码片段中是用户输入的值,执行片段后的输出,它将numberOfDigit打印为" 3"。

public static void main (String args[]){
    int uservalue=546;
    int numberOfDigit=0;
    String userValueinStr=String.valueOf(uservalue);
    numberOfDigit=userValueinStr.length();
    System.out.println("Number of digits in user entered value is::"+numberOfDigit );

    //Now as you got the no. of digits, you can go ahead and add the your logic of   multiplication
}

答案 1 :(得分:0)

问题是你有一组嵌套for循环,它们都调用n / = 10.更容易看出你是否缩进了所有内容:

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

     System.out.print("Please enter a number: ");
     int n = keyboard.nextInt();
     int i;
     for (i = 0; n > 0; i++) {
         n /= 10;

         for (i = 0; n > 0; i++) {
             n /= 10;
             System.out.println((n%100000) / 10000);
             System.out.println((n%10000) / 1000);                 
             System.out.println((n%1000) / 100);
             System.out.println((n%100) / 10);
             System.out.println(n%10);
         }
         System.out.println("Number of digits: " + i);
     }
 }

删除后,代码似乎按预期工作,并告诉你545确实有3位数。以下是修订后的代码:

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Please enter a number: ");
    int n = keyboard.nextInt();
    int i;

    for (i = 0; n > 0; i++) {
        n /= 10;
        System.out.println((n%100000) / 10000);
        System.out.println((n%10000) / 1000);                 
        System.out.println((n%1000) / 100);
        System.out.println((n%100) / 10);
        System.out.println(n%10);
    }

    System.out.println("Number of digits: " + i);

}

答案 2 :(得分:0)

似乎您尝试使用任意值执行相同的任务。而是尝试合并一些通用的算法。在这个贴子的例子中有些东西:

public class CountDigits {

    private void performTask () {
        int number = 546;
        int temp = number;
        int digit = 0;
        int counter = 1;
        while ( temp != 0 ) {
            digit = temp % 10;
            System.out.println ( digit + " * " + getMultiplier ( counter ) + " = " + ( digit * getMultiplier ( counter ) ) );
            ++counter;
            temp /= 10;
        }
    }

    private int getMultiplier ( int power ) {
        int value = 1;
        for ( int i = 1; i < power; ++i ) {
            value *= 10;
        }

        return value;
    }

    public static void main ( String[] args ) {
        new CountDigits ().performTask ();
    }

}

<强>输出:

C:\Mine\java\bin>java CountDigits
9 * 1 = 9
1 * 10 = 10
1 * 100 = 100
1 * 1000 = 1000


C:\Mine\java\bin>java CountDigits
6 * 1 = 6
4 * 10 = 40
5 * 100 = 500

答案 3 :(得分:0)

int n = 546;
for( int i = 0, dig = (int) Math.log10( n ) + 1; i < dig; ++i )
{
    int mult = (int) Math.pow( 10, dig - i - 1 );
    int a = (n / mult) % 10;
    System.out.println( a + " * " + mult + " = " + (a * mult) );
}

结果:

5 * 100 = 500
4 * 10 = 40
6 * 1 = 6

答案 4 :(得分:0)

int n = 546;
int exponent = (int) (Math.log10(n));

for (int i = exponent; i >= 0; i--) {
    int displayNum = (n / (int) Math.pow(10, i)) * (int) Math.pow(10, i);
    System.out.println(displayNum);
    n = n - displayNum;
}

输出

System.out﹕ 500
System.out﹕ 40
System.out﹕ 6