2011-04-04 49 views
2

我在使程序运行方面存在一些问题。我创建了一个具有设置的hashmap,hashmap可以保存4个键/值对。Java Hashmap使用随机值从给定密钥返回一个值

现在每个键(0,1,2,3)都附加到一个表示颜色(“白色”,“红色”等)的字符串'值'。

现在我使用随机给我一个从0到3的随机数,我将其分配给一个int变量。

然后,我使用这个变量来查看集合是否包含这个int键(它会),然后我想将与该键关联的值赋给一个String变量,然后我将在该方法中使用该变量来更改GUI面板的颜色(当事件被触发时生成随机颜色)。

// 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); 

任何人都可以帮我吗?现在已经将近12点了,可能在早上可以弄清楚,但是我的脑子里有空白!

+0

“返回使用随机值”让我想要做的' public int hashCo de(){return(int)(Math.random()* Integer.MAX_VALUE); }':-) – corsiKa 2011-04-04 22:57:08

回答

4

在我看来,这是乞求一个从0到3索引的数组Color [4] - 而不是将您的随机int更改为一个字符串或整数键并做一个哈希查找。


完全假冒类,展示了如何使用数组与随机

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; 
    } 
} 
+0

嗨,是的,这是我最初做的,但我如何从数组集合中返回一个随机值。我尝试生成一个随机数,将其分配给一个int变量,然后将该变量与数组的索引值进行比较,但我没有找到如何返回存储数组值的索引int值! – 2011-04-04 23:06:48

+0

感谢编辑,像一个魅力..作品!!感谢大家的意见。 – 2011-04-04 23:22:12

+0

@Jason - 也许你现在可以接受其中一个答案 – 2011-04-15 23:15:04

3

使用,而不是一个数组:

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

String randomColor = colors[random]; 

编辑:你的Color类替代字符串(或原始的),如果你想。

+0

我会试试这个,谢谢! – 2011-04-04 23:07:29

2

您需要一种将字符串转换为实际颜色的方法。我会用这样的:

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

(从2D图形/ Convertsagivenstringintoacolor.htm“> http://www.java2s.com/Tutorial/Java/0261_2D-Graphics/Convertsagivenstringintoacolor.htm)

0

对于(对谷歌在这里感谢其他的帮助就和教程),有兴趣的人我是如何解决这个!

// 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; 
    } 

干杯所有