当前位置: 代码迷 >> Java相关 >> HELP ME~~~
  详细解决方案

HELP ME~~~

热度:407   发布时间:2004-10-09 16:48:00.0
HELP ME~~~

看书遇到的小题目:

编写一个显示从6种早餐中随机选择一种的程序(你可以使用任意的套餐,比如:炒蛋.鸡蛋饼.水果.麦粥.土司或者酸奶).

想了想用Math.random()*10+1产生的是1-10,用Math.random()/2*10+1产生的是1-5,就是6没办法,请各位帮帮我

搜索更多相关的解决方案: HELP  酸奶  random  Math  套餐  

----------------解决方案--------------------------------------------------------

int randomNum=(int)(Math.random()*(upperBound-lowerBound+1)+lowerBound);

//this will generate a number between lowerBound to upperBound(inclusive)

in your case: int breakfast = (int)(Math.random()*6;

each breakfast is represented by 0 1 2 3 4 5

Question:Why do you need to start from 1? you can make one 0 right? :-)

Switch will be a good idea here:)

int final EGG = 0, EGGTHING = 1, FRUIT=2,RICE=3,BRAKE = 4,MILK=5;


----------------解决方案--------------------------------------------------------

恩恩,斑竹说的有道理,脑子太死了,刚刚学的,还没打开思路.呵呵

可以*6.并且可以从0开始,谢谢你


----------------解决方案--------------------------------------------------------
  相关解决方案