gpt4 book ai didi

java - java随机字符串数组

转载 作者:行者123 更新时间:2023-11-29 07:49:01 25 4
gpt4 key购买 nike

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com