Results 1 to 3 of 3
Thread: please help/critter java
- 03-19-2011, 01:09 PM #1
Member
- Join Date
- Nov 2010
- Location
- Beirut, Lebanon
- Posts
- 36
- Rep Power
- 0
please help/critter java
ok so im writing a lion class for a provided critter program.
whts important for you to know is that
every 3 moves the "L" representing "lion" changes its color randomly between three colors, i cant figure out how to do that this is what ive got and it says statement unreachable, i know why but i cant figure out any other way.
i have this initialized:
private Color [] names = {Color.RED,Color.GREEN,Color.BLUE};
and moves is calculated in another method by incrementation
in the color method:
Random rand = new Random();
int num = rand.nextInt(3);
if(num==0)
{
return names[0];
}
else if(num==1)
{
return names[1];
}
else
{
return names[2];
}
return names[num]
if(move%3==0)
{
return names[num];
}
- 03-19-2011, 02:04 PM #2
Hi.
see
Java Code:import java.util.Arrays; import java.util.List; import java.util.Random; public class RandomColor { private enum Color { RED, BLUE, YELLOW } private static List<Color> colors = Arrays.asList(Color.RED, Color.BLUE, Color.YELLOW); private static final Random rand = new Random(); public static void main(String[] ar) { int rt = rand.nextInt(3); System.out.println(colors.get(rt)); rt = rand.nextInt(3); System.out.println(colors.get(rt)); rt = rand.nextInt(3); System.out.println(colors.get(rt)); } }Skype: petrarsentev
http://TrackStudio.com
-
I just wanted to point out that this...
Java Code:Random rand = new Random(); int num = rand.nextInt(3); if(num==0) { return names[0]; } else if(num==1) { return names[1]; } else { return names[2]; } return names[num] if(move%3==0) { return names[num]; }
is the same as this...
Java Code:Random rand = new Random(); int num = rand.nextInt(3); return names[num];


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks