当前位置: 代码迷 >> J2SE >> 摇骰子游戏注释解决办法
  详细解决方案

摇骰子游戏注释解决办法

热度:305   发布时间:2016-04-24 01:29:18.0
摇骰子游戏注释
写详细中文注释,各种详细,适合菜鸟的菜鸟级别的人学习的,20分只给一人。
该程序已经编译通过,能正常RUN。
Java code
//Java extension packagesimport java.awt.*;import java.awt.event.*;import javax.swing.*;public class Craps extends JApplet implements ActionListener{    //constant variables for game status    final int WON=0, LOST=1,CONTINUE=2;        //other variables used    boolean firstRoll=true;       int sumOfDice=0;    int myPoint=0;    int gameStatus=CONTINUE;        //    JLabel die1Label,die2Label,sumLabel,pointLabel;    JTextField die1Field,die2Field,sumField,pointField;    JButton rollButton;    public void init()    {        Container container =  getContentPane();        container.setLayout(new FlowLayout());                //        die1Label =new JLabel("Die 1");        container.add(die1Label);        die1Field=new JTextField(10);        die1Field.setEditable(false);        container.add(die1Field);                //create label and text field for die 2        die2Label=new JLabel("Die 2");        container.add(die2Label);        die2Field=new JTextField(10);        die2Field.setEditable(false);        container.add(die2Field);                //create label 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);                    //process one roll of dice    }public void actionPerformed(ActionEvent actionEvent)    {        //first roll of dice        if (firstRoll)        {            sumOfDice =rollDice();  //roll dice            switch (sumOfDice)            {              //win on first roll            case 7: case 11:                gameStatus=WON;                pointField.setText("");  //clear point field                break;                                //lose on first roll            case 2: case 3: case 12:                gameStatus = LOST;                pointField.setText("");  //clear point field                        break;                                //remember point                default:                    gameStatus = CONTINUE;                    myPoint = sumOfDice ;                    pointField.setText(Integer.toString(myPoint));                    firstRoll = false;                    break;            }        }        else {sumOfDice =rollDice();                    if(sumOfDice ==myPoint)            gameStatus=WON;            else if(sumOfDice ==7)            gameStatus =LOST;        }        displayMessage();            }    public int rollDice()    {        int die1,die2,sum;                //pick random die values          die1=1+(int)(Math.random()*6);  //random    随机的         die2=1+(int)(Math.random()*6);                sum=die1+die2;                die1Field.setText(Integer.toString(die1));        die2Field.setText(Integer.toString(die2));        sumField.setText(Integer.toString(sum));        return sum;    }    public void displayMessage()    {        if(gameStatus==CONTINUE)        showStatus("Roll again");                else        {            if(gameStatus==WON)                showStatus("Player wins."+"Click Roll Dice to play again");            else                showStatus("Player loses."+"Click Roll Dice to play again");            firstRoll=true;                    }                }}
  相关解决方案