Results 1 to 10 of 10
Thread: Generate Random Number Loop
- 08-11-2011, 05:34 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
Generate Random Number Loop
Hello, thank you for visiting this thread..
I have a problem on my board game..
First I want to generate 2 random numbers, those 2 random numbers will become the coordinates of the board and then after that It will check if the string on the button is not enemy, if the button is not enemy it will generate random numbers again and checked again..
this is the piece of the code..
the [randomIntX][randomIntY] have errors too.. I dont know how to fix it.Java Code:do{ int randomIntX = randomX.nextInt(7); int randomIntY = randomY.nextInt(8); } while(Board[randomIntX][randomIntY].getText()!= "enemy");
And how do i check if the string on the button is not "enemy" those code give me errors..
Could you please help me.. thanks!Last edited by saber210; 08-11-2011 at 05:43 PM.
- 08-11-2011, 06:09 PM #2
What are the errors?
And don't use == or != on String Objects. Use the .equals() method.How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
- 08-11-2011, 06:14 PM #3
Member
- Join Date
- Aug 2011
- Posts
- 6
- Rep Power
- 0
- 08-12-2011, 12:34 AM #4
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
the first error is cannot find the variable randomIntX and randomIntY..
Basically, I want to generate 2 random numbers when not users turn.
if(!YourTurn)
{
do{
int randomIntX = randomX.nextInt(100);
int randomIntY = randomY.nextInt(100);
}
while(!Board[randomIntX][randomIntY].getText("enemy"));
}
- 08-12-2011, 12:37 AM #5
Your problem is that you have declared the variables inside the loop and trying to use them outside the loop. The scope of the variables is limited to the enclosing braces { }.
- 08-12-2011, 02:02 AM #6
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
If i put it outside how can i fix that it will still produce random numbers while the condition on the loop are met?
Thanks!
- 08-12-2011, 02:18 AM #7
You declare the variables outside the loop but still generate and assign the random numbers to the variables inside the loop.
- 08-12-2011, 02:18 AM #8
Member
- Join Date
- Jul 2011
- Posts
- 15
- Rep Power
- 0
I got it now,, Thanks.. I use return.. to return the if the condition is met..
Another question.. I have strings and integers on my button.. How can i check if there is integer or string on the button?Last edited by saber210; 08-12-2011 at 02:33 AM.
- 08-12-2011, 02:30 AM #9
Are you serious?
Java Code:declare variables loop { assign values to variables }
- 08-12-2011, 04:49 PM #10
How to Ask Questions the Smart Way
Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!
Similar Threads
-
Can I get random.util not to generate the same number twice
By lilleza87 in forum New To JavaReplies: 3Last Post: 10-24-2010, 10:24 PM -
[SOLVED] How to generate a random number from chi-square
By aeo3 in forum AWT / SwingReplies: 3Last Post: 04-16-2009, 01:42 AM -
Trying to Generate Random number
By PeterFeng in forum New To JavaReplies: 10Last Post: 01-14-2009, 08:37 AM -
Generate a random number
By romina in forum New To JavaReplies: 1Last Post: 08-07-2007, 05:23 AM -
How to generate random number in java
By fernando in forum New To JavaReplies: 1Last Post: 08-01-2007, 07:32 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks