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

Java 将数字的位数相乘,直到达到一位数,java,loops,methods,product,Java,Loops,Methods,Product,假设你输入数字546,然后你应该找到它的数字的乘积,即546=120,然后乘以120的数字直到,依此类推,直到你得到一个一位数。 这是我写的代码,但是循环不能正常工作,我尝试过修复它,但是没有任何改变。你能帮帮我吗 import java.util.*; public class Product { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in);

假设你输入数字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);
            }
        }

    }

}

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

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

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

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

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


祝你好运

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

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

对于不同的解决方案,您可以使用
递归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);
    }
}
import java.util.Scanner;
导入java.util.function.IntFunction;
公共类产品{
//这只是将数字减少为其数字的乘积。
静态函数产品=
(a) ->a<10?a:a%10*产品产品应用(a/10);
公共静态void main(字符串[]args){
扫描仪键盘=新扫描仪(System.in);
系统输出打印(“请输入号码”);
int n=keyboard.nextInt();
//现在继续应用lambda,直到结果小于10。
而(n>10){
n=生产应用(n);
}
系统输出println(n);
}
}

实际上,您的代码非常接近正确,唯一的错误是您没有在迭代之间重置
产品
变量。只需移动
int product=1while
循环中的code>指令

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

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

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

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