【发布时间】:2015-10-22 23:24:45
【问题描述】:
所以,对于我的 AP 计算机科学课程,我们必须找到所有的快乐数字并在最后打印出来。我知道我可以更简单地完成这段代码,但是,这样对我来说更有意义。这是我的问题:在我的 for 循环中,它只运行第一个数字,如果它是一个快乐的数字,它会打印出所说的数字,如果不是,它不会打印任何东西,但它不会去下一个数字。谢谢你的帮助!我的代码如下:
public class HappyNumbers
{
public static void main(String [] args)
{
boolean done = false;
int sum = 0;
for (int x = 1; x < Integer.MAX_VALUE; x++)
{
String a = "" + x;
while (!done)
{
int length = a.length();
if (length == 1)
{
int b = a.charAt(0) - '0';
sum = (int)Math.pow(b,2);
int y = (int)sum;
a = "" + y;
if (y == 1)
{
System.out.println(x);
done = true;
}
}
else if (length == 2)
{
int b = a.charAt(0) - '0';
int c = a.charAt(1) - '0';
sum = (int)(Math.pow(b,2) + Math.pow(c,2));
int y = (int)sum;
a = "" + y;
if (y == 1)
{
System.out.println(x);
done = true;
}
}
else if (length == 3)
{
int b = a.charAt(0) - '0';
int c = a.charAt(1) - '0';
int d = a.charAt(2) - '0';
sum =(int)(Math.pow(b,2) + Math.pow(c,2) + Math.pow(d,2));
int y = (int)sum;
a = "" + y;
if (y == 1)
{
System.out.println(x);
done = true;
}
}
else if (length == 4)
{
int b = a.charAt(0) - '0';
int c = a.charAt(1) - '0';
int d = a.charAt(2) - '0';
int e = a.charAt(3) - '0';
sum = (int)(Math.pow(b,2) + Math.pow(c,2) + Math.pow(d,2) + Math.pow(e,2));
int y = (int)sum;
a = "" + y;
if (sum == 1)
{
System.out.println(x);
done = true;
}
}
else if (length == 5)
{
int b = a.charAt(0) - '0';
int c = a.charAt(1) - '0';
int d = a.charAt(2) - '0';
int e = a.charAt(3) - '0';
int f = a.charAt(4) - '0';
sum = (int)(Math.pow(b,2) + Math.pow(c,2) + Math.pow(d,2) + Math.pow(e,2) + Math.pow(f,2));
int y = (int)sum;
a = "" + y;
if (sum == 1)
{
System.out.println(x);
done = true;
}
}
else if (length == 6)
{
int b = a.charAt(0) - '0';
int c = a.charAt(1) - '0';
int d = a.charAt(2) - '0';
int e = a.charAt(3) - '0';
int f = a.charAt(4) - '0';
int g = a.charAt(5) - '0';
sum = (int)(Math.pow(b,2) + Math.pow(c,2) + Math.pow(d,2) + Math.pow(e,2) + Math.pow(f,2) + Math.pow(g,2));
int y = (int)sum;
a = "" + y;
if (sum == 1)
{
System.out.println(x);
done = true;
}
}
else if (length == 7)
{
int b = a.charAt(0) - '0';
int c = a.charAt(1) - '0';
int d = a.charAt(2) - '0';
int e = a.charAt(3) - '0';
int f = a.charAt(4) - '0';
int g = a.charAt(5) - '0';
int h = a.charAt(6) - '0';
sum = (int)(Math.pow(b,2) + Math.pow(c,2) + Math.pow(d,2) + Math.pow(e,2) + Math.pow(f,2) + Math.pow(g,2) + Math.pow(h,2));
int y = (int)sum;
a = "" + y;
if (sum == 1)
{
System.out.println(x);
done = true;
}
}
else if (length == 8)
{
int b = a.charAt(0) - '0';
int c = a.charAt(1) - '0';
int d = a.charAt(2) - '0';
int e = a.charAt(3) - '0';
int f = a.charAt(4) - '0';
int g = a.charAt(5) - '0';
int h = a.charAt(6) - '0';
int i = a.charAt(7) - '0';
sum = (int)(Math.pow(b,2) + Math.pow(c,2) + Math.pow(d,2) + Math.pow(e,2) + Math.pow(f,2) + Math.pow(g,2) + Math.pow(h,2) + Math.pow(i,2));
int y = (int)sum;
a = "" + y;
if (sum == 1)
{
System.out.println(x);
done = true;
}
}
else if (length == 9)
{
int b = a.charAt(0) - '0';
int c = a.charAt(1) - '0';
int d = a.charAt(2) - '0';
int e = a.charAt(3) - '0';
int f = a.charAt(4) - '0';
int g = a.charAt(5) - '0';
int h = a.charAt(6) - '0';
int i = a.charAt(7) - '0';
int j = a.charAt(8) - '0';
sum = (int)(Math.pow(b,2) + Math.pow(c,2) + Math.pow(d,2) + Math.pow(e,2) + Math.pow(f,2) + Math.pow(g,2) + Math.pow(h,2) + Math.pow(i,2) + Math.pow(j,2));
int y = (int)sum;
a = "" + y;
if (sum == 1)
{
System.out.println(x);
done = true;
}
}
else if (length == 10)
{
int b = a.charAt(0) - '0';
int c = a.charAt(1) - '0';
int d = a.charAt(2) - '0';
int e = a.charAt(3) - '0';
int f = a.charAt(4) - '0';
int g = a.charAt(5) - '0';
int h = a.charAt(6) - '0';
int i = a.charAt(7) - '0';
int j = a.charAt(8) - '0';
int k = a.charAt(9) - '0';
sum = (int)(Math.pow(b,2) + Math.pow(c,2) + Math.pow(d,2) + Math.pow(e,2) + Math.pow(f,2) + Math.pow(g,2) + Math.pow(h,2) + Math.pow(i,2) + Math.pow(j,2) + Math.pow(k,2));
int y = (int)sum;
a = "" + y;
if (sum == 1)
{
System.out.println(x);
done = true;
}
}
}
}
}
}
【问题讨论】:
-
您的代码可能会卡在您的 while 循环中。您是否尝试过调试它,是否已将其设置为 true?
-
它卡在 else if 循环中。所以它只打印或不打印第一个数字。我该如何解决这个问题?
-
else if 语句不是循环并且您的代码不会卡在其中。它是否卡在您的 while 循环中?我建议您逐步完成并弄清楚发生了什么。
-
他们被困在那里,因为如果它从未达到 sum == 1,它将继续运行。所以卡在while循环中,但在else if循环中。感谢您的帮助。我能够在到期之前修复代码。谢谢。
-
我在 done == true; 下添加了另一个 if 语句在每个循环中。我添加的 if 语句是使它成为一个循环的数字,这会使它“不开心”。如果它达到这些数字之一,done = false;在完成 == false 的下面;我加了一个休息;有了这个添加,代码就可以完全运行了
标签: java for-loop numbers repeat