I'm writing a code segment that calculates and displays the number of holes upon which a golfer made an eagle. Which is two fewer than the par value for the hole. This is what I have come up with.

int[] par = new int[18];
      int[] strokes = new int[18];
      for (int i = 0; i < par.length; i++)
              par[i] = (int)(Math.random() * 2) + 1; //calculates eagle

              strokes[i] = (int)(Math.random() * 5) + 1; //calculates number of strokes per hole                
           for (int i = 0; i < strokes.length; i++)
           if (par[i] > strokes[i])
              System.out.println(i + 1);

Is it correct or am I making an error somewhere?

Hi :)

Your code has no compiling errors.
The question is if it does what you want it to do?

First you fill the par array with random numbers between 1 and 2.
Then you fill the strokes array with random numbers between 1 and 5.
After that you ask the system to print out every index (=holes) where the number in par is larger than the number in strokes.

The way you have set this up means that you will get the numbers of those holes where stroke where 1 and par where 2 because this is the only time when it is possible for par to be larger than strokes.

So if i have understood you right this does not fulfill your requirements.

Would it not be a better idea to decide how many strokes that equals par for every hole and then initiate the par array with those numbers. Then let the stroke array be filled with random numbers from 1 to the largest number in the par array + 4, or something like that.
After that you ask the system to print out only those index (=holes) where the value of par is exactly the same as the value of strokes+2

Use System.out.println(); inside your code to test it . This way you can see what results you get, what is filled in the arrays and so forth.

Hope this gave you some help.
Good luck :)

This article has been dead for over six months. Start a new discussion instead.