Search code examples
loopsnested-loops

My Loop Is Not Running


When I run this code for a Project Euler problem(39) it doesn't run the last loop, it just skips it. I had a similar issue with the nested loops beforehand, but I managed to fix those...somehow. I want to simplify my code, but I need it to work first.

I tried if(true){} and for some reason, that may have helped. I didn't remove it because I wanted to keep it as close to what I have running as possible.

thanks. i had a bad habit of using == instead of <= ect because a friend played a prank on me. annoyed the crap out of me. made me not ues <= as much.

thanks again

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);
    }
}

}


Solution

  • First::

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

    Second::

    // 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;
    }
    

    Third::

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