English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

I have the distance[] array declared globally, and xco and yco declared and initialised in the method. I also have locationx[] and locationy[] arrays declared globally, initialised with 7 integers in each.

for(int i = 0; i < 6; i++);
{
distance[i] = ((xco - locationx[i])*(xco - locationx[i])) + ((yco - locationy[i])*(yco - locationy[i]));
}

I've figured out that there must be a problem with this code, as when I try and display the contents of the distance[] array in the paint() method it shows the first six elements of the array as being 0 and only the last one with an actual number. What have I done wrong/what could the problem be?

(this code is in the mouseClicked() method).

2007-03-11 05:34:41 · 5 answers · asked by roslya 1 in Computers & Internet Programming & Design

The first answer looks like it might be somewhat helpful, if I could actually understand it properly. Could someone tell me what he's trying to say?

2007-03-11 05:47:47 · update #1

5 answers

Ok, what everyone missed is that you put a semi-colon after the "for" statement, effectively doing nothing but running i up to 5 (or 6, if the i<6 is actually a typo and it is really i<7 as others have stated). So then, it executes the code in the block with the now updated value i=6.

This is why I advocate using the Sun Java coding standard, especially in the case of loops: place the open bracket after the beginning statement, not on the next line.

for(int i = 0; i < 7; i++) {
distance[i] = ((xco - locationx[i])*(xco - locationx[i])) + ((yco - locationy[i])*(yco - locationy[i]));
}

2007-03-13 03:27:25 · answer #1 · answered by vincentgl 5 · 0 0

Its always difficult to diagnose the problem without seeing all of the relevant code. So, the information in this answer may not solve your immediate problem, but it might eventually help you explain the results you are getting:

If you really have an array of 7 values , your "for" loop should included the condition "i < 7" (or "i <=6") instead of "i < 6"

Things you might check for are whether you have any of the arrays declared in two different places, and whether you are perhaps looking at the one that is not modified by your code.

Without seeing the rest of the code that's about all I have to offer.

2007-03-11 06:05:43 · answer #2 · answered by parodister 3 · 0 0

First off, the condition should be i < 7. Second off, it sounds like either some of your values in your arrays are not initialized, or there is something wrong with your calculation in this loop. I'd suggest stepping through with a debugger to see if any of these values are zero (which would cause one or both of the expressions above to be zero). If you can't debug, then print out the values of all relevant variables, and all values in relevant arrays, in some area of your window to give insight into this problem.

2007-03-11 08:12:37 · answer #3 · answered by Anonymous · 0 0

i've got not got my Java ref with me, yet distance.length could desire to be a large determination greater effective than 6. maybe it rather is the returning the extensive form if entries interior the area array (that's 7). when you consider which you recognize how vast the locationx, locationy, and distance arrays are, merely use the consistent 6.

2016-10-01 22:45:32 · answer #4 · answered by ? 4 · 0 0

hi,its very simple to get out of this array
bcoz its a for loop ,so when it shows value ,d location is on the last element
do one thing ,either declare the variables as static

2007-03-11 05:45:01 · answer #5 · answered by luv l 1 · 0 0

fedest.com, questions and answers