写详细中文注释,各种详细,适合菜鸟的菜鸟级别的人学习的,20分只给一人。
该程序已经编译通过,能正常RUN。
//Java extension packages
import 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;
}
}}
该程序已经编译通过,能正常RUN。
//Java extension packages
import 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;
}
}}
玩家掷两个骰子。每个骰子的点数是1~6。骰子停下来后,计算两个骰子的点数之和。如果第一次掷的和为7或11,则玩家赢。如果和为2、3或12(称为craps),则庄家赢。如果和为4、5、6、8、9或10,则这个和就成为玩家的“点数”,继续掷骰子。 如果掷的点数之和等于点数,则玩家赢;如果等于7,则玩家输;否则,再掷骰子直至分出输赢。
希望对你有帮助。
玩家掷两个骰子。每个骰子的点数是1~6。骰子停下来后,计算两个骰子的点数之和。如果第一次掷的和为7或11,则玩家赢。如果和为2、3或12(称为craps),则庄家赢。如果和为4、5、6、8、9或10,则这个和就成为玩家的“点数”,继续掷骰子。 如果掷的点数之和等于点数,则玩家赢;如果等于7,则玩家输;否则,再掷骰子直至分出输赢。
import java.awt.*; 只包括名为awt的类 其下面的包并没有包含进去,所以还需要下面一行。
import java.awt.event.*; 按钮啊 对话框啊之类的东西
import javax.swing.*; 关于图形用户界面的包public class Craps extends JApplet implements ActionListener{ JApplet表示可以在网页里运行?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; roll用的按钮
public void init() {
Container container = getContentPane();
container.setLayout(new FlowLayout()); 声明一个容器也就是一个窗口,然后在窗口中加一些对话框或者按钮
//
die1Label =new JLabel("Die 1");
container.add(die1Label); 静态框1的标签,就是名字吧
die1Field=new JTextField(10);
die1Field.setEditable(false);
container.add(die1Field); 静态框1
//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);静态框2
//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);静态框sum
//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);静态框point
//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) { 按下按钮 然后所做的事情 与ActionListener有关系
//first roll of dice
if (firstRoll) { firstRoll为true 段程序必定执行
sumOfDice =rollDice(); //roll dice roll的两个值相加
switch (sumOfDice) {
//win on first roll
case 7: case 11: 结果是7或者11就结束了
gameStatus=WON; 改变状态
pointField.setText(""); //clear point field 第一次成功point静态框中清空
break;
//lose on first roll
case 2: case 3: case 12:
gameStatus = LOST; 结果是2 3 12就结束 改变状态
pointField.setText(""); //clear point field
break;
//remember point
default:
gameStatus = CONTINUE; 改变状态
myPoint = sumOfDice ; 如果都不是 保存第一次的结果以便下面比较
pointField.setText(Integer.toString(myPoint)); 显示第一次的结果
firstRoll = false; 第二次或以后就不会进入这个if段了
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"); 等待下一次按钮 再次执行此函数 actionPerformed
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;
}
}}