【问题标题】:Multiplying digits of a number until you reach a one digit number将数字的数字相乘,直到得到一位数字
【发布时间】:2020-06-20 03:10:02
【问题描述】:
假设你输入了数字 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);
}
}
}
}
【问题讨论】:
标签:
java
loops
methods
product
【解决方案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));
}
}
上述代码运行时,以546为输入,输出:
Please input the number: 546
546
120
0
【解决方案2】:
查看您的代码后,您的问题似乎出在以下表达式中:
current % 10。
模运算为您提供除以十的余数。
在您输入 120 的情况下,该操作的结果将为 0。
在您的应用程序逻辑的其余部分之后,您的迭代变量将设置为零,立即结束您的循环。
我不会给你复制粘贴代码来解决这个问题,因为它看起来像是一个编程课程作业。不过我会帮你解决的。
我建议的解决方法是改变你解决这个问题的方法,而不是尝试用数学方法来解决这个问题,而是以一种利用 Java 编程语言的方式来解决。
您可以将输入从整数更改为字符串。
在这种情况下,您可以使用 String.length() 来确保在退出循环时满足您的要求。
在您的循环中,您将字符串拆分为长度为 1 的子字符串。然后,您只需将它们相乘。
当循环退出时(因为字符串长度不再大于 1)你会得到你想要的结果。
祝你好运!
【解决方案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】:
实际上,您的代码非常接近正确,唯一做错的事情是您没有在迭代之间重置 product 变量。您只需将int product = 1; 指令移到外部while 循环内即可。
另外,如果你想要一个数字结尾,你的条件应该是while(n >= 10)或while(n > 9),因为10仍然是2位,所以我们需要再迭代一次!
最后一句话:有时将代码分成更小的部分更容易。它更容易理解,更容易测试/调试!例如,您可以先创建一个返回单次迭代结果的函数 productOfDigits(n),然后再创建一个重复调用前一个函数的函数 productOfDigitsUntilSingleDigit(n)。
【解决方案5】:
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);
}
}