gpt4 book ai didi

java - 寻找快乐的数字

转载 作者:太空宇宙 更新时间:2023-11-04 13:18:54 24 4
gpt4 key购买 nike

因此,对于我的 AP 计算机科学类(class),我们必须找到所有快乐数字并在最后打印出来。我知道我可以更简单地完成这段代码,但是,这样对我来说更有意义。这是我的问题:在我的 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;

}
}
}
}
}
}

最佳答案

因为你设置了

done = true; 

第 25 行

关于java - 寻找快乐的数字,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33292446/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com