java编程问题,英文的,帮忙解决一下,谢谢!

Inthegameofcraps,apasslinebetproceedsasfollows.Twosix-sideddicearerolled;thefirstroll... In the game of craps, a pass line bet proceeds as follows. Two six-sided dice are rolled; the first roll of the dice in a craps round is called the “come out roll”. A come out roll of 7 or 11 automatically wins, and a come out roll of 2,3,or 12 automatically loses. If 4,5,6,8,9,or 10 is rolled on the come out roll, that number becomes the point. The player keeps rolling the dice until either 7 or the point is rolled. If the point is rolled first, then the player wins the bet. If a 7 is rolled first, then the player loses.
Write a program that simulates a game of craps using these rules without human input. Instead of asking for a wager, the program should calculate whether the player would win or lose. The program should simulate rolling the two dice and calculate the sum. Add a loop so that the program plays 10,000 games \. Add counters that count how many times the player wins, and how many times the player loses. At the end of the 10,000 games, computer the probability of winning [i.e.,Wins/(Wins+Losses)] and output this value. Over the long run, who is going to win the most games, you or the house?
展开
 我来答
md851127
2007-04-11
知道答主
回答量:1
采纳率:0%
帮助的人:0
展开全部
import java.awt.*;
import java.awt.event.*;

import javax.swing.*;

public class Craps extends JApplet implements ActionListener
{
//constant variables for game used
public static void main(String[] args){

final int WON = 0, LOST = 1, CONTINUE = 2;
//other variables used
boolean firstRoll = true; //true if first roll of dice
int sumofDice = 0; //sum of the dice
int myPoint = 0; //point if no win/loss on first roll
int gameStatus = CONTINUE;

//graphical user interface component
JLabel die1Label, die2Label, sumLabel, pointLabel;
JTextField die1Field, die2Field, sumField, pointField;
JButton rollButton;

//set up GUI components
public void init()
{
//obtain content pane and change its layout to
//a FlowLayout
Container container = getContentPane();
Container.setLayout(new FlowLayout());

//creat label and text field for die1
die1Label = new JLabel( "Die 1 ");
Container.add( die1Label );
die1Field = new JTextField( 10 );
die1Field.setEditable( false );
Container.add(die1Field );

//create label and text field for die2
die2Label = new JLabel( "Die 2 ");
Container.add( die2Label );
die2Field = new JTextField( 10 );
die2Field.setEditable( false );
Container.add(die2Field );

//create lavel and text field for sum
sumLabel = new JLabel("Sum is ");
Container.add( sumLabel );
sumField = new JTextField( 10 );
sumField.setEditable( false );
Container.add( sumField );

//create label and text field for point
pointLabel = new JLabel( "Point is ");
Container.add( pointLabel );
pointField = new JTextField( 10 );
pointField.setEditable( false );
Container.add( pointField );

//create button user clicks to roll dice
rollButton = new JButton("Roll Dice");
rollButton.addActionListener( this );
Container.add( rollButton );

}//end method init

//process one roll of dice
public void actionPerformed( ActionEvent actionEvent )
{
//first roll of dice
if ( firstRoll )
{
sumofDice = rollDice();

switch ( sumofDice )
{
case 7: case 11:
gameStatus = WON;
pointField.setText("");
break;
case 2: case 3: case 12:
gameStatus = LOST;
pointField.setText("");
break;

//rememeber point
default:
gameStatus = CONTINUE;
myPoint = sumofDice;
pointField.setText( Integer.toString(myPoint));
firstRoll = false;
break;

}//end switch
}//end if

else
{
sumofDice = rollDice(); //roll dice

//determine game status
if ( sumofDice == myPoint )
gameStatus = WON;
else
if (sumofDice == 7)
gameStatus = LOST;
}

//display message indication game status
displayMessage();
}//end method actionPerformed

//roll dice, calculate sum and display result
public int rollDice()
{
int die1, die2,sum;

//pick random die values
die1 = 1 + ( int )(Math.random() * 6 );
die2 = 1 + ( int )(Math.random() * 6 );

sum = die1 + die2;

//display result
die1Field.setText( Integer.toString( die1 ));
die2Field.setText( Integer.toString( die2 ));
sumField.setText(Integer.toString(sum));

return sum;
}//end method rollDice

public void displayMessage()
{
//game should continue
if( gameStatus == CONTINUE )
showStatus( "Roll again." );
//game won ro lost
else
{
if ( gameStatus == WON )
showStatus( "Player wins." +
"Click Roll Dice to play again.");
else
showStatus("Player loss." +
" Click Roll Dice to play again.");

//next roll is first roll of new game
firstRoll = true;
}
}//end method displayMessage

}//end class
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式