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];
Run Code Online (Sandbox Code Playgroud)
我怎么说"如果颜色=黑色然后做这个"或相同的绿色或相同的红色"
你的意思是...
if(Color.equals("Black")) {
// then do this
} else if(Color.equals("Red"){
// then do this
}
Run Code Online (Sandbox Code Playgroud)
甚至(在Java> = 1.7)
switch(Color) {
case "Black":
// then do this
break;
case "Red":
// then do this
break;
}
Run Code Online (Sandbox Code Playgroud)