String [] rnum = {"Black", "Red", "Black", "Red", "Black", "Red", "Black","Red",
"Black", "Red", "Black", "Red", "Black", "Red", "Black", "Red","Black", "Red", "Green"};
int A = rnum.length;
//the "Math.random() method will get you a random color
int random = (int) (Math.random() * A);
//randomize the strings
String Color = rnum[random];
String数组,我怎么说,“如果颜色为黑色,然后做这个”或同为绿色或同为红色的”在Java中随机
color.equals(“Black”) –
您可以为此使用枚举。但是如果你不想使用枚举,你会发现这是非常罕见的情况之一,当使用'=='而不是'.equals'来比较字符串时。 –
我同意大卫华莱士,枚举仍然是最好的方式,还可以与枚举巫术(在Java 7之前),如果一些颜色被添加到用例,没有太多的工作来重构代码。 – chillworld