Java 寻找快乐数字

Java 寻找快乐数字,java,for-loop,numbers,repeat,Java,For Loop,Numbers,Repeat,所以,对于我的AP计算机科学课,我们必须找到所有的快乐数字,并在最后打印出来。我知道我可以把这段代码做得简单得多,但是,这样对我来说更有意义。这是我的问题:在我的for循环中,它只运行第一个数字,如果它是一个快乐的数字,它会打印出所说的数字,如果它不是,它不会打印任何内容,但它根本不会转到下一个数字。谢谢你的帮助!我的代码如下: public class HappyNumbers { public static void main(String [] args) {

所以,对于我的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;

                    }
                }
            }
        }
    }
}
公共类happyNumber
{
公共静态void main(字符串[]args)
{
布尔完成=假;
整数和=0;
对于(int x=1;xdone = true;