我想从预制的颜色列表中随机选择一种颜色,我不知道如何去做。像这样:
String[] randomColor = {"BLUE", "GREEN", "CYAN",
"RED", "YELLOW", "MAGENTA", "PINK"};
x = (Math.random() * 6)
graphics.setColor(Color.x);
答案 0 :(得分:4)
请改用Random.nextInt(6)
。但这不会按照你想要的方式运作。您无法使用字符串名称引用对象。您需要将Color
对象放在Color
数组中:Color[]
Color[] colors = { Color.red, Color.blue, Color.white, Color.cyan,
Color.green, Color.gray, new Color(0xFFAA00) };
Random random = new Random();
int x = random.nextInt(colors.length);
graphics.setColor(colors[x]);
答案 1 :(得分:0)
你可以试试这个
randomColor[new Random().nextInt(randomColor.length)]