我的循环没有运行

时间:2013-12-06 05:21:18

标签: loops nested-loops

当我为Project Euler问题(39)运行此代码时,它不运行最后一个循环,它只是跳过它。我之前遇到过与嵌套循环类似的问题,但我设法修复了......不知何故。我想简化我的代码,但我需要它先工作。

我试过if(true){}并且由于某种原因,这可能有所帮助。我没有删除它,因为我希望尽可能地保持它尽可能接近。

感谢。我养成了使用==而不是< =等的坏习惯,因为朋友在我身上玩恶作剧。惹恼了我的废话。让我不是那么多。

再次感谢

public class euler39
{

    public static void main(String[] args)
    {
        int[] right = new int[1010];
        // int a = 0;
        // int b = 0;
        // int c = 0;
        int s = 0;
        int high = 0;
        for(int i = 0; i == 1009; i++ )
        {
            right[i] = 0;
        }
        if(true)
        {
            for(int a = 0; a <= 999;)
            {
            a++ ;
            // System.out.println(a);
            if(true)
            {
                for(int b = 0; b <= (999 - a);)
                {
                    b++ ;
                    // System.out.println(b);
                    if(true)
                    {
                        for(int c = 0; c <= 999 - (a + b);)
                        {
                            c++ ;
                            // System.out.println(c);
                            // System.out.println(" testing " + a + ", " + b
                            // + ", " + c);
                            if(Math.pow(c, 2) == (Math.pow(a, 2) + Math
                                    .pow(b, 2)))
                            {
                                System.out.println("high = " + (a + b + c));
                                right[ (a + b + c)] = (right[ (a + b + c)] + 1);
                                System.out.println("high = " + (a + b + c));
                            }
                        }
                    }
                }
            }
        }
    }
    System.out.print("high = tyut ");
    if(true)
    {
        for(; s == 999; s++ )
        {
            System.out.println(right[s]);
            if(right[s] > high)
            {
                right[s] = high;
            }
        }
        //System.out.println(high);
    }
}

}

1 个答案:

答案 0 :(得分:0)

首先::

int[] right = new int[1010](); // The brackets here will 
                               //initialize the memory to 0

二::

// No need for this, as the memory is already initialized to 0
// Changed i == 1009 to i <= 1009
for(i = 0; i <= 1009; i++ )
{
 right[i] = 0;
}

第三::

// Changed s == 999 to s <= 999
for(; s <= 999; s++ )
{
}