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];
我怎么说“if color = black then do this”或者绿色相同或红色相同”
最佳答案
你是说...
if(Color.equals("Black")) {
// then do this
} else if(Color.equals("Red"){
// then do this
}
甚至(在 Java >= 1.7 中)
switch(Color) {
case "Black":
// then do this
break;
case "Red":
// then do this
break;
}
关于java - java随机字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22799081/