Results 1 to 3 of 3
  1. #1
    farahm is offline Member
    Join Date
    Nov 2010
    Location
    Beirut, Lebanon
    Posts
    36
    Rep Power
    0

    Default 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];
    }

  2. #2
    Petr's Avatar
    Petr is offline Senior Member
    Join Date
    Jan 2011
    Location
    Russia
    Posts
    620
    Rep Power
    4

    Default

    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

  3. #3
    ozzyman's Avatar
    ozzyman is offline Senior Member
    Join Date
    Mar 2011
    Location
    London, UK
    Posts
    797
    Blog Entries
    2
    Rep Power
    4

    Default

    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];

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •