【发布时间】:2011-07-29 13:07:16
【问题描述】:
我在让我的程序运行方面遇到了一些问题。我创建了一个具有设置的哈希图,该哈希图可以保存 4 个键/值对。
现在每个键 (0,1,2,3) 都附加到代表颜色(“白色”、“红色”...等)的字符串“值”。
现在我使用 random 给我一个从 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 点了,可能早上就能弄明白,但脑子里一片空白!!
【问题讨论】:
-
“使用随机返回值”让我想做
public int hashCode() { return (int)(Math.random() * Integer.MAX_VALUE); }:-)