java - java随机字符串数组

标签 java arrays random

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/

相关文章:

java - Java 中的 Amazon API 产品

java.util.Properties 多个值 ArrayIndexOutOfBoundsException

c# - 如何从随机池中选择一个号码,然后让号码不能重新选择

c# - 如何实现线程安全的随机数

java - Struts html 文本标签颜色属性

java - 如何使用 Firebase 从实时数据库存储和检索图像 url?

java - "Equality test with boolean literal"- 测试 boolean 值和使用 == 之间的区别

php - mysql 表中的 javascript 多维数组 - 不知道那种对象对象是什么

java - 将字符串数组转换为 vector 的最佳方法?

ruby - Ruby 1.9.2 中的分布式顺序随机数生成