This site requires JavaScript, please enable it in your browser!
Greenfoot back
bramp0wnd
bramp0wnd wrote ...

2015/1/25

Can't use the variable in my while-loop.NEED HELP!

bramp0wnd bramp0wnd

2015/1/25

#
I'm trying to return the 'x' that I have found in my loop, but it isn't working. I need some help!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
public int lookForPosition()
    {
        boolean found = false;
         
        while (found = false)
        {
            int x = Greenfoot.getRandomNumber(getWidth());
            boolean different = false;
             
            for(int i = 0; i < xPosities.length; i++)
            {
                if(xPosities[i] != x)
                {
                    different = true;
                    break;
                }
                else
                {
                    different = false;
                }
            }
 
            if(different = true)
            {
                found = true;
            }          
            else
            {
                found = false;
            }
        }
        return x;
    }
erdelf erdelf

2015/1/25

#
add this in line 4
1
int x = 0;
and change line 7 to
1
x = Greenfoot.getRandomNumber(getWidth());
you are declaring the variable in the while loop, making it inaccessible for everything outside of it
danpost danpost

2015/1/26

#
The problem is that line 5 is saying 'while (false)'. You are setting 'found' to 'false' within the round braces instead of asking if 'found' is 'false'. The single equal sign ( '=' ) is the assignment operator while the double equal sign ( '==' ) is the conditional equality symbol. It is this second one you need to use in line 5. The same goes with line 23.
erdelf erdelf

2015/1/26

#
oh yeah.. right thanks danpost.. didnt see that one
bramp0wnd bramp0wnd

2015/1/26

#
danpost wrote...
The problem is that line 5 is saying 'while (false)'. You are setting 'found' to 'false' within the round braces instead of asking if 'found' is 'false'. The single equal sign ( '=' ) is the assignment operator while the double equal sign ( '==' ) is the conditional equality symbol. It is this second one you need to use in line 5. The same goes with line 23.
Damn, you're right, thanks a lot!
danpost danpost

2015/1/26

#
It is positively outrageous how you are using these variables, 'different' and 'found'. As the method is written, lines 23 through 30 can be replaced with this:
1
2
3
4
if (different)
{
    break;
}
because you are setting 'found' to the value of 'different' and, if the value of 'different' is true, exiting the 'while' loop to finally return 'x'; so, the above will exit the 'while' loop if 'different' was set to true. Lines 17 through 20 are unnecessary as the value of 'different' is false going into the 'for' loop and once turned to a value of true the 'for' loop is exited via the 'break' statement. So, now we have this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
public int lookForPosition()
{
    boolean found = false;
    int x = 0;
    while (! found)
    {
        x = Greenfoot.getRandomNumber(getWidth());
        boolean different = false;
        for (int i = 0; i < xPosities.length; i++)
        {
            if (xPosities[i] != x)
            {
                different = true;
                break;
            }
        }
        if (different)
        {
            break;
        }
    }
    return x;
}
At this point, you can see that the boolean 'found' is not really being use to exit the 'while' loop. I will start a new post, so I can refer to the new code.
danpost danpost

2015/1/27

#
Now, as I said: the boolean 'found' does not appear to actually do anything anymore. However, the other boolean, 'different', really tracks the same state as the boolean 'found' (lines 23 through 30 of your original code points that out). So, we can just remove the 'different' variable and replace 'found' for it:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
public int lookForPosition()
{
    boolean found = false;
    int x = 0;
    while (! found)
    {
        x = Greenfoot.getRandomNumber(getWidth());
        for (int i = 0; i < xPosities.length; i++)
        {
            if (xPosities[i] != x)
            {
                found = true;
                break;
            }
        }       
    }
    return x;
}
Continue on next post.
danpost danpost

2015/1/27

#
There seems to be a flaw in the logic here. The method, as written here (which has the same logic as your original code), says to return a random int value which is 'not ALL values' (which is not 'not ANY value') in the 'xPosities' array. If you are trying to find one number that IS in the array, just change line 10 in the pevious post to this:
1
if (xPosities[i] == x)
If you are trying to find a number that is not anywhere in the array, you need to break out of the 'for' loop and continue the 'while' loop if the random values is equal to an element in the 'xPosities' array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
public int lookForPosition()
{
    boolean found = false;
    int x = 0;
    while (! found)
    {
        found = true;
        x = Greenfoot.getRandomNumber(getWidth());
        for (int i = 0; i < xPosities.length; i++)
        {
            if (xPosities[i] == x)
            {
                found = false;
                break;
            }
        }
    }
    return x;
}
bramp0wnd bramp0wnd

2015/1/28

#
Thank you very much for your time to answer to my post. I know that my method might have been very far fetched. I haven't been programming that long (few months). FYI: I passed my Java-exam this week at school! :-)
You need to login to post a reply.