java投掷骰子,求点数的出现概率

Python07

java投掷骰子,求点数的出现概率,第1张

很简单 。

int [] num = new int[3] //数组长度为3

int diag=0 //统计出现5的次数

for(int i=0i>3i++){//掷骰子三次 ,也就是三个骰子

随机数字 num[i]=(int)(Math.random()*5)+1 //随机1-6的数字

if(num[i]==5){

diag++ //当骰子等于5的时候,统计次数加1;

}

}

double a=diag/3 //骰子出现5的次数,除以骰子总数,得到概率。

System.out.println(a)

Dice代码如下:

import java.util.Random

//(1)创建一个“骰子”类,命名为Dice。

public class Dice {

//提示:初始化两个Dice对象。

//(2)“骰子”类有两个属性:①最大值为固定值6,②点数为1-6之间的整数。属性均设置为private。

private static int max = 6

private int point

// (3)“骰子”类有两个构造函数:①无形参的构造函数,将点数默认值设置为1;②有1个形参的构造函数,将形参赋值给点数。

public Dice() {

this.point = 1

}

public Dice(int point) {

this.point = point

}

// (4)自动生成骰子点数的get和set方法。

public static int getMax() {

return max

}

public static void setMax(int max) {

Dice.max = max

}

public int getPoint() {

return point

}

public void setPoint(int point) {

this.point = point

}

// (5)编写一个表示“掷骰子”的方法,将点数和函数返回值设置为1-6之间的随机整数。

//提示:Math.random() //随机选取>=0.0且<1.0的double值

//double转int的方法:(int) double值 //转换后会舍弃小数点后面的值

public int throwDice() {

int result = 0

while (true) {

int random = (int) (Math.random() * 10)

if (random >0 &&random <= max) {

result = random

break

}

}

return result

}

}

测试类方法如下:

import java.math.BigDecimal

import java.util.ArrayList

import java.util.List

//(6)新建一个类,在其main()函数中调用Dice类,实现以下功能:

public class TestDice {

public static void main(String[] args) {

// ①掷两个骰子,显示每个骰子的点数,以及点数之和

Dice dice = new Dice()

int one = dice.throwDice()

int two = dice.throwDice()

System.out.println("两次点数之和:" + (one + two))

// ②输入设置两个骰子的点数,显示两个骰子的点数之和。

Dice dice2 = new Dice(2)

Dice dice3 = new Dice(6)

System.out.println("所设置的点数之和:" + (dice2.getPoint() + dice3.getPoint()))

// ③连续10次掷两个骰子,显示每次掷骰子的结果,以及两个骰子10次点数的平均值。

Dice dice4 = new Dice()

List<Integer>points1 = new ArrayList<>()

List<Integer>points2 = new ArrayList<>()

for (int i = 0i <10i++) {

int first = dice4.throwDice()

System.out.println("第一个骰子掷:" + (i + 1) + "次点数是:" + first)

int second = dice4.throwDice()

System.out.println("第二个骰子掷:" + (i + 1) + "次点数是:" + second)

points1.add(first)

points2.add(second)

}

long sum1 = points1.stream().reduce(Integer::sum).orElse(0)

System.out.println("第一个骰子10次点数的平均值:" + new BigDecimal(Long.valueOf(sum1).toString()).divide(new BigDecimal(Integer.valueOf(points1.size()).toString())))

long sum2 = points2.stream().reduce(Integer::sum).orElse(0)

System.out.println("第二个骰子10次点数的平均值:" + new BigDecimal(Long.valueOf(sum2).toString()).divide(new BigDecimal(Integer.valueOf(points2.size()).toString())))

}

}

public class Test {

public static void main(String[] args){

DieGame dieGame = new DieGame()

if (dieGame.play()) {

System.out.println("你赢了!")

} else {

System.out.println("你输了!")

}

}

}

class Die {

private int faceValue

public int getFaceValue() {

return faceValue

}

public void setFaceValue(int faceValue) {

this.faceValue = faceValue

}

public void roll() {

this.faceValue = (int) (Math.random() * 6 + 1)

}

}

class DieGame {

private Die die1 = new Die()

private Die die2 = new Die()

public boolean play() {

die1.roll()

System.out.println("第一次点数:" + die1.getFaceValue())

die2.roll()

System.out.println("第二次点数:" + die2.getFaceValue())

if (die1.getFaceValue() + die2.getFaceValue() == 7) {

return true

} else {

return false

}

}

}