Java Lychrel数程序错误
我正在尝试制作lychrel数字程序。但我做不到。标准是,列出10000以下的Lychrel数,Lychrel数检查迭代有限。我被设定为30,但我还没有得到答案。 如果检查完成30次迭代,则应列出编号。我没有找到解决办法。帮帮我Java Lychrel数程序错误,java,Java,我正在尝试制作lychrel数字程序。但我做不到。标准是,列出10000以下的Lychrel数,Lychrel数检查迭代有限。我被设定为30,但我还没有得到答案。 如果检查完成30次迭代,则应列出编号。我没有找到解决办法。帮帮我 public class LychrelNumber { static final int MAX_NUMBER = 10000; static final int MAX_ITERATION = 30; int iterationCount = 0; void l
public class LychrelNumber {
static final int MAX_NUMBER = 10000;
static final int MAX_ITERATION = 30;
int iterationCount = 0;
void listTheLychrelNymber() throws Exception {
long i = 0;
long temp;
for (int j = 0; j < MAX_NUMBER; j++) {
iterationCount = 0;
temp = j;
for (i = 0; i < MAX_ITERATION; i++) {
long first = temp;
long second = reverseTheNumber(temp);
long third = first + second;
long fourth = reverseTheNumber(third);
if (third == fourth) {
break;
} else {
temp = third;
if (i == MAX_ITERATION) {
System.out.println("Lychrel Numbers are :" + j);
}
}
}
}
}
long reverseTheNumber(long n) {
long reverse = 0;
while (n != 0) {
reverse = reverse * 10;
reverse = reverse + n % 10;
n = n / 10;
}
return reverse;
}
public static void main(String[] args) {
try {
LychrelNumber lychrelNumber = new LychrelNumber();
lychrelNumber.listTheLychrelNymber();
} catch (Exception e) {
}
}
公共类LychrelNumber{
静态最终整数最大值=10000;
静态最终int MAX_迭代=30;
int迭代计数=0;
void list thlychrelnymber()引发异常{
长i=0;
长温;
对于(int j=0;j
}
它是成功构建的。但是我没有得到输出。看看你的i循环(我把代码缩短了一点)
for(i=0;i
如您所见,当我达到MAX_迭代时,您停止循环,但如果i==MAX_迭代,则只在循环中打印Lychrel数(这当然不会发生)。我得到了解决方案
if (i == (MAX_ITERATION-1)) {
System.out.println("Lychrel Numbers are:" + j);
}
在这里,我在检查状况时犯了错误 不,你错了。我更新了我的解决方案。无论如何,谢谢你。现在,请添加评论以提高我自己。
if (i == (MAX_ITERATION-1)) {
System.out.println("Lychrel Numbers are:" + j);
}