编辑:发现一个错误。错误的测试用例已更新
我有一个程序,其中掷出五个骰子,并为掷骰子分配了一只手。即什么都没有,一对,两对,三个,一个满屋子,四个,一个,五个。该代码将运行1000000次,并为每个掷骰子提供诱因机会。在下面,我找到了我的代码应该输出的大致百分比:
Case 1, None alike, is 0.092533
Case 2, One pair, is 0.462799
Case 3, Two pair, is 0.231789
Case 4, Three of a kind, is 0.154192
Case 5, Full house, is 0.038595
Case 6, Four of a kind, is 0.019316
Case 7, Five of a kind, is 0.000776
但是我的代码给出以下输出:
Case 1, None alike is 7.57E-4
Case 2, One pair is 0.019422
Case 3, Two pair is 0.270331
Case 4, Three of a kind is 0.153657
Case 5, Full House is 0.270331
Case 6, Four of a kind is 0.019422
Case 7, Five of a kind is7.57E-4
我不明白为什么我的课程比例这么低。他们甚至不加一。我已经通过并测试了我的逻辑,但是从我所看到的来看,这是合理的。我希望能有另一双眼睛看一下,并让我知道它们是否可以吸引人。下面是我的代码:
import java.util.*;
public class PokerDice {
public static void main(String[] args) {
double none = 0;
double pair = 0;
double twop = 0;
double threep = 0;
double full = 0;
double fourp = 0;
double fivep = 0;
for (int i = 0; i<=1000000; i++) {
int [] rolls = new int[5];
rolls[0] = (int)(1 + (Math.random()*(6)));
rolls[1] = (int)(1 + (Math.random()*(6)));
rolls[2] = (int)(1 + (Math.random()*(6)));
rolls[3] = (int)(1 + (Math.random()*(6)));
rolls[4] = (int)(1 + (Math.random()*(6)));
int [] counts = Counts(rolls);
int length = counts.length;
int hand = 0;
for (int j = 0; j < length; j++) {
if (counts[j] == 5) {
hand = 7;
}
}
if (hand < 7) {
for (int j = 0; j < length; j++) {
if (counts[j] == 4) {
hand = 6;
}
}
}
if (hand < 6) {
int counter = 0;
for (int j = 0; j < length; j++) {
if (counts[j] == 3) {
counter++;
}
if (counts [j] == 2) {
counter++;
}
}
if (counter == 2) {
hand = 5;
}
}
if (hand < 5) {
for (int j = 0; j < length; j++) {
if (counts[j] == 3) {
hand = 4;
}
}
}
if (hand < 4) {
int newcounter = 0;
for (int j = 0; j < length; j++) {
if (counts[j] == 2) {
newcounter++;
}
}
if (newcounter == 2) {
hand = 3;
}
}
if (hand < 3) {
for (int j = 0; j < length; j++) {
if (counts[j] == 2) {
hand = 2;
}
}
}
if (hand < 2) {
hand = 1;
}
if (hand == 7) {
fivep++;
}
if (hand == 6) {
fourp++;
}
if (hand == 5) {
full++;
}
if (hand == 4) {
threep++;
}
if (hand == 3) {
twop++;
}
if (hand == 2) {
pair++;
}
if (hand == 1) {
none++;
}
}
fivep/=1000000;
fourp/=1000000;
full/=1000000;
threep/=1000000;
twop/=1000000;
pair/=1000000;
none/=1000000;
System.out.println("Poker Dice Probability Calculator");
System.out.println("Running 1,000,000 trials");
System.out.println();
System.out.println("Case 1, None alike is "+fivep);
System.out.println("Case 2, One pair is "+fourp);
System.out.println("Case 3, Two pair is "+full);
System.out.println("Case 4, Three of a kind is "+threep);
System.out.println("Case 5, Full House is "+full);
System.out.println("Case 6, Four of a kind is "+fourp);
System.out.println("Case 7, Five of a kind is"+fivep);
}
public static int[] Counts (int [] rolled) {
int one = 0;
int two = 0;
int three = 0;
int four = 0;
int five = 0;
int six = 0;
int len = rolled.length;
int [] rolltimes = new int[6];
for (int i = 0; i<len; i++) {
if (rolled [i] == 1) {
one++;
}
else if (rolled [i] == 2) {
two++;
}
else if (rolled [i] == 3) {
three++;
}
else if (rolled [i] == 4) {
four++;
}
else if (rolled [i] == 5) {
five++;
}
else if (rolled [i] == 6) {
six++;
}
}
rolltimes[0] = one;
rolltimes[1] = two;
rolltimes[2] = three;
rolltimes[3] = four;
rolltimes[4] = five;
rolltimes[5] = six;
return rolltimes;
}
}
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。