开发者

Java Hashmap returning a value from a given key, using random

开发者 https://www.devze.com 2023-02-22 05:08 出处:网络
I have a bit of a problem in regards to getting my program to work. I have created a hashmap that has asetting and the hashmap can hold 4 key/value pairs.

I have a bit of a problem in regards to getting my program to work. I have created a hashmap that has a setting and the hashmap can hold 4 key/value pairs.

Now each key (0,1,2,3) is attached to a String 'value' that represents a colour ("white", "red"... etc).

Now I am using random to give me a random number from 0 to 3 to which I assign to an int variable.

I then use this variable to see if the collection contains this int key (which it will) and 开发者_运维问答then I wanted to assign that value assosiated with that key to a String variable to which I will then use in a method to change the color of a GUI panel (generating a random color when the event is triggered).

// changing yellow with the String variable representing the value 
// from the hashmap found by matching the key with the random int.
centerPanel.setBackground(Color.yellow);  

Can anyone help me out please? It's nearly 12am here and can probably figure it out in the morning but am having mind blanks!!


It seems to me this is begging for an array Color[4] indexed from 0 to 3 - rather than changing your random int to a String or Integer key and doing a Hash lookup.


Totally fake class that shows how to use an array with your random

public class foo
{
    private Color[] colors = { Color.red, Color.green, Color.blue, Color.yellow };

    public Color getColor()
    {
        return colors[getRandom(0, 3)];
    }

    private int getRandom(int min, int max)
    {
        return 2;
    }

    enum Color {
        red, green, blue, yellow;
    }
}


Use an array instead:

String[] colors = new String[]{"white", "red"... etc};
int random = random.nextInt(colors.length);

String randomColor = colors[random];

Edit:Substitute String for your Color class (or primitive) if you want to.


You need a method that converts the String into a actual color. I would use something like this:

public static Color stringToColor(final String value) {
    if (value == null) {
      return Color.black;
    }
    try {
      // get color by hex or octal value
      return Color.decode(value);
    } catch (NumberFormatException nfe) {
      // if we can't decode lets try to get it by name
      try {
        // try to get a color by name using reflection
        final Field f = Color.class.getField(value);

        return (Color) f.get(null);
      } catch (Exception ce) {
        // if we can't get any color return black
        return Color.black;
      }
    }
  }

(from 2D-Graphics/Convertsagivenstringintoacolor.htm">http://www.java2s.com/Tutorial/Java/0261_2D-Graphics/Convertsagivenstringintoacolor.htm)


For anyone interested in how I solved this (thanks to the help of other on here and tutorials on google):

    // fields
    private Color1[] colors = { Color1.red, Color1.green, Color1.blue, Color1.yellow };
    private int a2;
    private String getRandomColor;

    //constructor calling methods to generate a random value to 'a2' (0 to 3) and then setting variable a2 to the color as a string

    public SamTester()
    {
        setA2();
    }

    public void setA2()
    {
        a2 = (int)(Math.random()*4);
        getRandomColor = getColor().toString();
    }

    public Color1 getColor()
    {
       return colors[a2];
    }
________________________________________________________________________

        //inner class  
      class LeftClicked implements ActionListener
      {
          public void actionPerformed(ActionEvent e)
           {
               if(getRandomColor == "red")
               {
                   centerPanel.setBackground(Color.red);
               }
               else if (getRandomColor == "blue")
               {
                   centerPanel.setBackground(Color.blue);
               }
               else if (getRandomColor == "green")
               {
                   centerPanel.setBackground(Color.green);
               }
               else
               {
                   centerPanel.setBackground(Color.yellow);
               }
               setA2();
         }
        }

________________________________________________________________________

//basic enums class to represent the constant colors.

    public enum Color1
   {
    red, green, blue, yellow;
   }

Cheers all!

0

精彩评论

暂无评论...
验证码 换一张
取 消