求大神帮我写下java火柴人小游戏代码注释
import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class GetMatch extends JFrame implements ActionListener
{
private static final long serialVersionUID = 920921981358099067L;
public int sumMatch;
private JLabel lbPer, lbCom, lbLast;
private JButton btnStart;
private JRadioButton rdbOne, rdbTwo, rdbThree;
private JTextField tfPcshow, tfLastshow;
private boolean win = true;
public GetMatch()
{
super("火柴游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setLocationRelativeTo(null);
setResizable(false);
initUI();
}
private void initUI()
{
Container contrainer = getContentPane();
ButtonGroup group = new ButtonGroup();
contrainer.setLayout(null);
lbPer = new JLabel("Person取数");
lbPer.setBounds(20, 25, 100, 25);
lbCom = new JLabel("Computer取数");
lbCom.setBounds(20, 70, 100, 25);
lbLast = new JLabel("剩余火柴数");
lbLast.setBounds(20, 115, 100, 25);
btnStart = new JButton("PC启动");
btnStart.setBounds(310, 70, 100, 25);
rdbOne = new JRadioButton("1");
rdbOne.setBounds(135, 25, 35, 35);
rdbTwo = new JRadioButton("2");
rdbTwo.setBounds(170, 25, 35, 35);
rdbThree = new JRadioButton("3");
rdbThree.setBounds(205, 25, 35, 35);
tfPcshow = new JTextField();
tfPcshow.setBounds(135, 70, 150, 25);
tfLastshow = new JTextField();
tfLastshow.setBounds(135, 115, 150, 25);
btnStart.addActionListener(this);
rdbOne.addActionListener(this);
rdbTwo.addActionListener(this);
rdbThree.addActionListener(this);
group.add(rdbOne);
group.add(rdbTwo);
group.add(rdbThree);
contrainer.add(btnStart);
contrainer.add(rdbOne);
contrainer.add(rdbTwo);
contrainer.add(rdbThree);
contrainer.add(rdbOne);
contrainer.add(rdbTwo);
contrainer.add(rdbThree);
contrainer.add(lbPer);
contrainer.add(lbCom);
contrainer.add(tfPcshow);
contrainer.add(btnStart);
contrainer.add(lbLast);
contrainer.add(tfLastshow);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnStart)
{
sumMatch = (int)(Math.random() * (50 - 20)) + 20;
tfPcshow.setText("计算机随机取了" + sumMatch + "根火柴");
tfLastshow.setText("");
}
else if(e.getSource() instanceof JRadioButton)
{
int k = Integer.parseInt(((JRadioButton)e.getSource()).getText());
;
sumMatch -= k;
if(sumMatch == 0)
{
JOptionPane.showMessageDialog(this, "你赢啦", "TEST", JOptionPane.INFORMATION_MESSAGE);
}
else if(sumMatch < 0)
{
JOptionPane.showMessageDialog(this, "出错啦!重新来过", "TEST", JOptionPane.INFORMATION_MESSAGE);
}
tfPcshow.setText("计算机取了" + computer() + "根");
tfLastshow.setText("剩余火柴" + sumMatch + "根");
if(!win)
{
JOptionPane.showMessageDialog(this, "你输啦!", "TEST", JOptionPane.INFORMATION_MESSAGE);
win = true;
}
}
}
private int computer()
{
int ret = 0;
if(sumMatch <= 3 && sumMatch > 0)
{
ret = sumMatch;
sumMatch = 0;
win = false;
}
else if(sumMatch != 0)
{
ret = (int)(Math.random() * (3 - 1)) + 1;
sumMatch -= ret;
}
return ret;
}
public static void main(String[] args)
{
GetMatch frame = new GetMatch();
frame.setVisible(true);
}
}
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
public class GetMatch extends JFrame implements ActionListener
{
private static final long serialVersionUID = 920921981358099067L;
public int sumMatch;
private JLabel lbPer, lbCom, lbLast;
private JButton btnStart;
private JRadioButton rdbOne, rdbTwo, rdbThree;
private JTextField tfPcshow, tfLastshow;
private boolean win = true;
public GetMatch()
{
super("火柴游戏");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(500, 300);
setLocationRelativeTo(null);
setResizable(false);
initUI();
}
private void initUI()
{
Container contrainer = getContentPane();
ButtonGroup group = new ButtonGroup();
contrainer.setLayout(null);
lbPer = new JLabel("Person取数");
lbPer.setBounds(20, 25, 100, 25);
lbCom = new JLabel("Computer取数");
lbCom.setBounds(20, 70, 100, 25);
lbLast = new JLabel("剩余火柴数");
lbLast.setBounds(20, 115, 100, 25);
btnStart = new JButton("PC启动");
btnStart.setBounds(310, 70, 100, 25);
rdbOne = new JRadioButton("1");
rdbOne.setBounds(135, 25, 35, 35);
rdbTwo = new JRadioButton("2");
rdbTwo.setBounds(170, 25, 35, 35);
rdbThree = new JRadioButton("3");
rdbThree.setBounds(205, 25, 35, 35);
tfPcshow = new JTextField();
tfPcshow.setBounds(135, 70, 150, 25);
tfLastshow = new JTextField();
tfLastshow.setBounds(135, 115, 150, 25);
btnStart.addActionListener(this);
rdbOne.addActionListener(this);
rdbTwo.addActionListener(this);
rdbThree.addActionListener(this);
group.add(rdbOne);
group.add(rdbTwo);
group.add(rdbThree);
contrainer.add(btnStart);
contrainer.add(rdbOne);
contrainer.add(rdbTwo);
contrainer.add(rdbThree);
contrainer.add(rdbOne);
contrainer.add(rdbTwo);
contrainer.add(rdbThree);
contrainer.add(lbPer);
contrainer.add(lbCom);
contrainer.add(tfPcshow);
contrainer.add(btnStart);
contrainer.add(lbLast);
contrainer.add(tfLastshow);
}
public void actionPerformed(ActionEvent e)
{
if(e.getSource() == btnStart)
{
sumMatch = (int)(Math.random() * (50 - 20)) + 20;
tfPcshow.setText("计算机随机取了" + sumMatch + "根火柴");
tfLastshow.setText("");
}
else if(e.getSource() instanceof JRadioButton)
{
int k = Integer.parseInt(((JRadioButton)e.getSource()).getText());
;
sumMatch -= k;
if(sumMatch == 0)
{
JOptionPane.showMessageDialog(this, "你赢啦", "TEST", JOptionPane.INFORMATION_MESSAGE);
}
else if(sumMatch < 0)
{
JOptionPane.showMessageDialog(this, "出错啦!重新来过", "TEST", JOptionPane.INFORMATION_MESSAGE);
}
tfPcshow.setText("计算机取了" + computer() + "根");
tfLastshow.setText("剩余火柴" + sumMatch + "根");
if(!win)
{
JOptionPane.showMessageDialog(this, "你输啦!", "TEST", JOptionPane.INFORMATION_MESSAGE);
win = true;
}
}
}
private int computer()
{
int ret = 0;
if(sumMatch <= 3 && sumMatch > 0)
{
ret = sumMatch;
sumMatch = 0;
win = false;
}
else if(sumMatch != 0)
{
ret = (int)(Math.random() * (3 - 1)) + 1;
sumMatch -= ret;
}
return ret;
}
public static void main(String[] args)
{
GetMatch frame = new GetMatch();
frame.setVisible(true);
}
}
...全文
相关推荐
火柴人java源码-awesome-stars:我的GitHub星星的精选列表!
火柴人java源码AwesomeStarsAcuratedlistofmyGitHubstars!GeneratedbyContentsC-SQLCipherisanSQLiteextensionth
java编写的游戏(源码、注释)
本项目提供了一个塔防游戏的源代码,附带了详细的注释,这对于初学者来说是一份宝贵的资源,可以帮助他们学习如何在Java中构建一个完整的游戏。首先,我们要了解Java作为游戏开发的语言基础。
简单的JAVA制作代码
根据提供的文件信息,我们可以推断出作者正在学习Java编程,并尝试编写一个简单的程序。下面将对这段不完整的代码进行分析并扩展相关知识点。### Java基础知识介绍#### 1.
火柴人java源码-PhalApi-Core:PhalApiV2核心
重构升级包括类名调整、注释完善、代码格式化等,同时优化项目目录结构。框架要求PHP 7.0.0以上,依赖phalapi/notorm,遵循
c语言奔跑的火柴人游戏源码.rar
资源内容:项目全套源码+完整文档源码说明: 全部项目源码都是经过测试校正后百分百成功运行。SpringBoot 毕业设计,SpringBoot 课程设计,基于SpringBoot+Vue开发的,含有代


