
求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
展开
展开全部
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;
}
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;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
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);
}
}
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);
}
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用个随机数用可以实现的。
追问
不用随机数 用main method 和repeat
追答
那就直接循环输出。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询