我需要帮助,使我的随机发生器只产生三个字母:G,R,B。我该怎么做,因为B的ASCII码是66,G是71,和R是82.我感谢任何帮助。 我试着开始这样的:我怎样才能让我的程序得到三个特定的字母
public static void main(String[] args) {
// TODO code application logic here
String color=color();
System.out.println(color);
}
static String color(){
String color=null;
int randnum=(int)(Math.random()*3)+1;
if(randnum==1){
color= "B";
}
return color;
}
}
但我不明白的事情是,它还会产生其它号码。我只想要G,R,B作为随机数输出 。
这是什么语言? JavaScript的? C#? – RickyA
我的建议是将这些字母存储在一个数组中,然后使用0到2之间的随机数来获得该数组的一个随机索引,给你一个随机的字母,在这3个字母集中 –
你可以使用模3并且做你的值也在0和2之间。 – Gab