【发布时间】:2019-08-24 11:54:34
【问题描述】:
尝试编写附加持久性算法。 AP是当你有一个数字并且你不断添加它的数字直到你留下一个数字为止。例如。 9991 = 9+9+9+1 = 28,2+8=10,1+0=1。因此,9991 的加法持久性为 3。需要 3 次加法才能将数字减少到一位数。
我已经对问题进行了编码,但它不断增加计数器。对我来说看起来是正确的,但不能让它工作哈哈。
Scanner reader = new Scanner(System.in);
int ui;
int b;
int sum = 0;
int count = 0;
System.out.print("Enter a number: ");
ui = reader.nextInt();
do {
do {
b = ui % 10;
ui = ui / 10;
sum += b;
} while (ui != 0);
ui = sum;
count++;
//System.out.print(b);
//System.out.print(sum);
//System.out.print(ui);
} while (ui > 10);
System.out.print(count+1);
}
【问题讨论】:
-
您是否通过 IDE 中的调试器运行程序?
-
我真的不知道如何使用调试器。它显示我的 IP 有一些错误?但是感谢您让我注意到调试器。我现在学会使用它:)
标签: java string algorithm math while-loop