提交 76f9cb96 编写于 作者: 依然范特西S's avatar 依然范特西S

第一次提交

上级
package dome;
import java.math.BigDecimal;
import java.util.Random;
import java.util.Scanner;
/**
* @author liuyang01
* @date 2022-09-26
* 类描述:
* 修改记录:
*/
/*掷骰子合理性判断
* 利用Math.random()模拟产生两枚骰子,点数相加
* 等于2和等于12的概率最小
* 等于7的情况有6种
* 3600次掷骰子,和等于7的概率应当有1\6
*/
public class demo01 {
public static void main(String[] args) {
System.out.println("输入掷骰子次数:");
Scanner scan = new Scanner(System.in);
Integer num = scan.nextInt();
possibility(num);
}
public static void possibility(int n){
int sum2 = 0; //两个骰子合为2
double hl2 = 0.0;//两个骰子合为2的可能性
int sum7 = 0; //两个骰子合为7
double hl7 = 0.0;//两个骰子合为7的可能性
int sum12 = 0; //两个骰子合为12
double hl12 = 0.0;//两个骰子合为12的可能性
Random r=new Random();
for(int i =0 ; i<n; i++){ //3600次
int s1 = (int)(1+Math.random()*6); // 第一个骰子
int s2 = (int)(1+Math.random()*6); // 第一个骰子
if((s1+s2) == 2){
sum2++;
}
if((s1+s2) == 7){
sum7++;
}
if((s1+s2) == 12){
sum12++;
}
}
hl2 = (double) sum2/n;
hl7 = (double) sum7/n;
hl12 = (double) sum12/n;
System.out.println("两个骰子合为2的次数:"+sum2 +"次\t" +"可能性(合理次数/"+n+"):" +String.format("%.5f", hl2));
System.out.println("两个骰子合为7的次数:"+sum7 +"次\t" +"可能性(合理次数/"+n+"):" +String.format("%.5f", hl7));
System.out.println("两个骰子合为12的次数:"+sum12 +"次\t" +"可能性(合理次数/"+n+"):" +String.format("%.5f", hl12));
BigDecimal data1 = new BigDecimal(hl7);
BigDecimal data2 = new BigDecimal((double)1/6);
if(data1.compareTo(data2)>=0){
System.out.println("掷骰子"+n+"次合理");
}else{
System.out.println("掷骰子"+n+"次不合理 或 掷骰子次数过少");
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册