我现在在过一个作业,别的我都做好了,就是有一个method不会写。要求是说随机选取n个数在1到6之间,然后把随机得到的这n个数加在一起,而这个n是在使用的时候用户来决定的。
而这整个的project是要作出一个程序:你自己设定一个数字,比如说是50, 然后扔11次色子,算有多少几率这11次色子的和是你所设定的数字,也就是50。我所写的main class如下:
import java.util.Scanner;public class DiceDriver{ public static void main(String[] args){
Scanner scan = new Scanner(System.in);
SimpleDice d = new SimpleDice();
System.out.println("Enter number of tosses");
int runs = 100000;
int theNumber = scan.nextInt();
System.out.println("Enter times you want to throw");
int throwTimes = scan.nextInt();
int someCt = 0;
int tossResult;
for(int j = 0; j < runs; j++){
tossResult = d.throwDice();
if(tossResult == theNumber) someCt++;
}
System.out.println("some fraction: " + ((double)someCt)/runs);
}
}我现在要试着写SimpleDice这个class.
public class SimpleDice{
public int tossDie(){
return(1+ (int)(Math.random()*6));
}
public int throwDice(){再往下就是要写如何把n个随机数加在一起的method了,我就不会写了,希望大家快帮帮忙,还有2天这个作业就要交了。
而这整个的project是要作出一个程序:你自己设定一个数字,比如说是50, 然后扔11次色子,算有多少几率这11次色子的和是你所设定的数字,也就是50。我所写的main class如下:
import java.util.Scanner;public class DiceDriver{ public static void main(String[] args){
Scanner scan = new Scanner(System.in);
SimpleDice d = new SimpleDice();
System.out.println("Enter number of tosses");
int runs = 100000;
int theNumber = scan.nextInt();
System.out.println("Enter times you want to throw");
int throwTimes = scan.nextInt();
int someCt = 0;
int tossResult;
for(int j = 0; j < runs; j++){
tossResult = d.throwDice();
if(tossResult == theNumber) someCt++;
}
System.out.println("some fraction: " + ((double)someCt)/runs);
}
}我现在要试着写SimpleDice这个class.
public class SimpleDice{
public int tossDie(){
return(1+ (int)(Math.random()*6));
}
public int throwDice(){再往下就是要写如何把n个随机数加在一起的method了,我就不会写了,希望大家快帮帮忙,还有2天这个作业就要交了。
________________为什么要扔11次?