背景:又到了不知道吃啥的时候了,每次到饭点,就不知道吃啥,或者有好几个,不知道选啥,就用随机数帮我选了算了,哈哈哈。
//参数样例:eatSomething("饺子,清汤面,米饭,烤鱼",10)
public static void eatSomething(String IWantEat, int whileSize){
System.out.println("开始时间:"+new Date());
//这边是用中文逗号进行切割,可以换成其他的,根据需要来使用
String[] eats = IWantEat.split(",");
if(null != eats && 0 < eats.length){
int eatsLen = eats.length;
int[] eatSize = new int[eatsLen];
Random ra = new Random();
while (0 < whileSize){
int size = ra.nextInt(eatsLen);
eatSize[size]++;
whileSize --;
}
//用于记录出现次数最多的食物下标
int maxIndex = 0;
int max = 0;
for (int i=0; i<eatsLen; i++) {
if(max < eatSize[i]){
max = eatSize[i];
maxIndex = i;
}
System.out.println(eats[i]+",出现了"+eatSize[i]+"次");
}
System.out.println("不知道吃啥?那就吃"+eats[maxIndex]);
}else{
System.out.print("你啥也不想吃!!!");
}
}
结果样例展示:
不知道啥时候能用上这有趣的代码,哈哈哈