求java试题答案 写一个有关掷骰子的c++编程问题

writeamethodthatwillsimulatetheresultsofrollingtwofairdicebyprintingtworandomintegerv... write a method that will simulate the results of rolling two fair dice by printing two random integer values in the range 1 to 6 along with their total. Sample output from a call to the method could be: 4 and 3 -- a total of 7 展开
 我来答
woshixiaoda
2011-03-21 · TA获得超过637个赞
知道小有建树答主
回答量:319
采纳率:100%
帮助的人:255万
展开全部
public static void main(String[] args)throws Exception{
throwDice(2);
}

public static void throwDice(int diceNum){
int dice1Value = getRandomValue(6);
System.out.println("The value of dice1 is "+dice1Value);
int dice2Value = getRandomValue(6);
System.out.println("The value of dice1 is "+dice2Value);
System.out.println("The total value is "+(dice1Value+dice2Value));
}
private static int getRandomValue(int seed){
Random random = new Random();
int result = -1;
while( (result=random.nextInt(6))<=0 );
return result;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
喜只来屋梨仗1a
2011-03-21 · TA获得超过173个赞
知道小有建树答主
回答量:83
采纳率:0%
帮助的人:102万
展开全部
public class Test {
public static void main(String []s){
rollingTwoDice();
}
public static void rollingTwoDice(){
int a =(int) (6*Math.random()+1);
int b =(int) (6*Math.random()+1);
System.out.printf("%d and %d -- a total of %d\n",a,b,a+b);
}
}
本回答被提问者采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
神魔鬼怪妖佛仙
2011-03-21 · 超过28用户采纳过TA的回答
知道答主
回答量:121
采纳率:0%
帮助的人:94.5万
展开全部
用个随机数用可以实现的。
追问
不用随机数  用main method 和repeat
追答
那就直接循环输出。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(1)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式