| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1042 人关注过本帖
标题:HELP ME~~~
只看楼主 加入收藏
hjf4000love
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2004-5-18
收藏
 问题点数:0 回复次数:2 
HELP ME~~~

看书遇到的小题目:

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

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

搜索更多相关主题的帖子: HELP 酸奶 random Math 套餐 
2004-10-09 16:48
Anstey
Rank: 1
等 级:新手上路
帖 子:94
专家分:0
注 册:2004-4-14
收藏
得分:0 

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;


Anstey. Cheers. I love CAPPUCCINO~~
2004-10-10 07:10
hjf4000love
Rank: 1
等 级:新手上路
帖 子:13
专家分:0
注 册:2004-5-18
收藏
得分:0 

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

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


交个朋友:289869722
2004-10-10 13:40
快速回复:HELP ME~~~
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016660 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved