有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java将数字的位数相乘,直到达到一位数

假设您输入数字546,然后您应该找到其数字的乘积,即546=120,然后将120的数字相乘,依此类推,直到得到一个数字。 这是我写的代码,但是循环不能正常工作,我尝试过修复它,但是没有任何改变。你能帮帮我吗

import java.util.*;
public class Product {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        int a = keyboard.nextInt();
        System.out.println("Please input the number");
        numberOfProducts(a);


    }
    public static void numberOfProducts(int n){
        int product = 1;
        while(n>10) {
            int current = n;
            while (current != 0) {
                product = product * (current % 10);
                current = current / 10;
                n = product;
                System.out.println(product);
            }
        }

    }

}

共 (5) 个答案

  1. # 1 楼答案

    import java.util.*;
    public class Product {
        public static void main(String[] args) {
            Scanner keyboard = new Scanner(System.in);
            int a = keyboard.nextInt();
            System.out.println("Please input the number");
            numberOfProducts(a);
        }
          public static void numberOfProducts(int n){
            int product = 1; 
      
            while (n != 0)   { 
            product = product * (n % 10); 
            n = n / 10; 
            } 
        
            if(product > 10) {
                System.out.println("Intermediate result="+product);
                numberOfProducts(product);
            }
            else
                System.out.println("Final 1 digit product="+product);
    
        }
    }
    
  2. # 2 楼答案

    查看代码后,您的问题似乎出现在以下表达式中: current % 10。 模运算给出除法10的余数。 在输入120的情况下,该操作的结果将是0

    按照应用程序逻辑的其余部分,迭代变量将设置为零,立即结束循环

    我不会给你复制粘贴代码来解决这个问题,因为它看起来像是一个编程课程作业。不过我会帮你解决的

    我建议的解决方法是改变解决这个问题的方法,而不是试图用数学的方法来解决这个问题,而是用一种利用Java编程语言的方法。 您可以将输入从整数更改为字符串。 在这种情况下,可以使用String。length()以确保退出循环时满足您的要求。 在循环中,将字符串拆分为长度为1的子字符串。之后,你只需要把这些乘以

    当循环退出时(因为字符串长度不再大于1),您将得到预期的结果

    祝你好运

  3. # 3 楼答案

    对于不同的解决方案,您可以使用recursive lambda

    import java.util.Scanner;
    import java.util.function.IntFunction;
    
    public class Product {
        // this simply reduces the number to the product of its digits.
        static IntFunction<Integer> prod =
                (a) -> a < 10 ? a : a % 10 * Product.prod.apply(a / 10);
    
        public static void main(String[] args) {
    
            Scanner keyboard = new Scanner(System.in);
            System.out.print("Please input the number");
            int n = keyboard.nextInt();
    
            // Now continue applying the lambda until the result is < 10.
            while (n > 10) {
                n = prod.apply(n);
            }
            System.out.println(n);
        }
    }
    
  4. # 4 楼答案

    我认为您看起来像下面的代码:

    import java.util.Scanner;
    
    public class Main {
        public static int numberOfProducts(String number) {
            int product = 1;
            do {
                for (int i = 0; i < number.length(); ++i){
                    // This line converts the every digit of the number string to an integer.
                    product *= number.charAt(i) - '0';
                }
                // Remove this line, if you don't want to print the product of each iteration.
                System.out.println(number);
                // Update number with the new product.
                // This line converts the int product to a new string.
                number = "" + product;
            } while (product > 10);
            return product;
        }
        public static void main(String[] args) {
            System.out.print("Please input the number: ");
            Scanner keyboard = new Scanner(System.in);
            int a = keyboard.nextInt();
            // Treat number as a string for easier indexing.
            String number = "" + a;
            System.out.println(numberOfProducts(number));
        }
    }
    

    当上述代码以546作为输入运行时,它输出:

    Please input the number: 546
    546
    120
    0
    
  5. # 5 楼答案

    实际上,您的代码非常接近于正确,唯一的错误是您没有在迭代之间重置product变量。只需将int product = 1;指令移动到外部while循环中即可

    另外,如果您希望在末尾有一个位数,那么您的条件应该是while(n >= 10)while(n > 9),因为10仍然是2位数,所以我们需要再次迭代

    最后一句话:有时把代码分成更小的部分更容易。它更容易理解,也更容易测试/调试!例如,您可以先创建一个返回单个迭代结果的函数productOfDigits(n),然后创建另一个重复调用前一个函数的函数productOfDigitsUntilSingleDigit(n)