Climbing的专栏
Life is limited, but art is long
hpdlzu80100
登录
注册
全站
当前博客
空间
博客
好友
相册
留言
huang Climbing
ID:hpdlzu80100
[修改头像]
共
1560
次访问,排名
2万外
好友
5
人,关注者
6
人
热爱生活,积极向上,流自己的汗,吃自己的饭!
hpdlzu80100的文章
原创 23 篇
翻译 0 篇
转载 2 篇
评论 0 篇
最近评论
软件项目交易
订阅我的博客
文章分类
IT Industry and Career
(RSS)
Java Learning
(RSS)
Temp
(RSS)
收藏
相册
存档
2008年05月(1)
2008年04月(24)
掷双骰结果统计(Show statistical data of Craps (Rolling of two dice))
新一篇: 掷双骰儿游戏的公平性分析(Evaluate the justice of Craps game)
//
Show statistical data of Craps (Rolling of two dice)
//
Java how to program, 5/e, Exercise 7.15
import
javax.swing.
*
;
import
java.awt.
*
;
import
java.awt.event.
*
;
import
java.text.
*
;
public
class
DiceRolling
extends
JApplet
implements
ActionListener
{
int
[] diceResult;
double
[] possibility;
int
rollingTotal;
double
possibilityTotal;
JLabel RollingLabel;
JButton RollingButton;
JTextArea output;
DecimalFormat twoDigits
=
new
DecimalFormat(
"
0.000
"
);
public
void
init()
{
Container container
=
getContentPane();
container.setLayout(
new
FlowLayout());
RollingLabel
=
new
JLabel(
"
Press Rolling Button to roll 2 dice 36,000 times
"
);
container.add(RollingLabel);
RollingButton
=
new
JButton(
"
Roll
"
);
container.add(RollingButton);
RollingButton.addActionListener(
this
);
output
=
new
JTextArea();
container.add(output);
diceResult
=
new
int
[
12
];
possibility
=
new
double
[
12
];
output.append(
"
Sum of two dice\t\tFrequency\tPossibility\n
"
);
}
public
void
actionPerformed (ActionEvent event)
{
output.setText(
"
Sum of two dice\t\tFrequency\tPossibility\n
"
);
rollingTotal
=
0
;
possibilityTotal
=
0.0
;
for
(
int
i
=
0
;i
<
11
;i
++
)
diceResult[i]
=
0
;
possibility[i]
=
0.0
;
}
for
(
int
i
=
0
;i
<
36000
;i
++
)
{
for
(
int
j
=
0
;j
<
11
;j
++
)
if
(rollDice()
==
j
+
2
)
diceResult[j]
++
;
rollingTotal
++
;
}
for
(
int
i
=
0
;i
<
11
;i
++
)
{
possibility[i]
=
((
double
)diceResult[i]
/
36000
);
possibilityTotal
+=
Double.parseDouble(twoDigits.format(possibility[i]));
output.append(Integer.toString(i
+
2
)
+
"
\t\t
"
+
diceResult[i]
+
"\t
"
+
twoDigits.format(possibility[i])
+
"\n
"
);
}
output.append(
"
Total \t\t
"
+
rollingTotal
+
"
\t
"
+
twoDigits.format(possibilityTotal)
+
"\n
"
);
}
public
int
rollDice()
{
int
dice1
=
(
int
)(
1
+
Math.random()
*
6
);
int
dice2
=
(
int
)(
1
+
Math.random()
*
6
);
int
sum
=
dice1
+
dice2;
return
sum;
}
}
发表于 @
2008年04月17日 12:15:00
|
评论(
loading...
)
|
编辑
旧一篇: 简单航班订票模拟系统(Airline Reservation Sysytem, a simple simulation)
评论:没有评论。
发表评论
姓 名:
主 页:
校验码:
看不清,换一张
登录