java火柴游戏_求一个“JAVA拿火柴小游戏”的程序要求如下!!!!!!!跪求!!!!!...

这是一个使用Java编写的火柴游戏程序,玩家可以输入拿走的火柴数,计算机自动响应并判断胜负。游戏界面简洁,包含OK按钮,通过Random类生成随机数决定计算机的行动。当火柴数量减到0时,程序会显示胜利者。
摘要由CSDN通过智能技术生成

展开全部

按照题目要求,人拿完火柴后计62616964757a686964616fe58685e5aeb931333332643930算机自动拿火柴,判断胜利者。鼠标点击ok或者键盘按enter键即可提交人拿的火柴个数。图形界面如下,

c0f6590b3f53eeaa3be301fcd77a06cb.png

import java.awt.*;

import java.awt.event.ActionListener;

import java.awt.event.MouseAdapter;

import java.awt.event.MouseEvent;

import java.sql.PreparedStatement;

import java.sql.ResultSet;

import java.util.Random;

import javax.swing.JOptionPane;

import javax.swing.JFrame;

import javax.swing.JButton;

import javax.swing.JLabel;

import javax.swing.JOptionPane;

import javax.swing.JPanel;

import javax.swing.JTextField;

import javax.swing.JPasswordField;

public class game extends javax.swing.JFrame implements ActionListener{

private JPanel frame;

private JTextField pwd;

private JTextField jLabel1;

private JTextField jLabel2;

private JButton bntOk;

private JLabel l1;

private JLabel l2;

private JLabel l3;

int total;

public static void main(String[] args)

{

game ff=new game();

}

public game(){

initGUI();

Random r=new Random();

total=r.nextInt(100);

jLabel2.setText(String.valueOf(total));

this.setSize(500, 500);

this.setLocation(300, 400);

this.setLocationRelativeTo(null);

this.setVisible(true);

this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

getRootPane().setDefaultButton(bntOk);

bntOk.addActionListener(this);

}

public void actionPerformed(java.awt.event.ActionEvent evt)

{

int a=Integer.parseInt(jLabel1.getText());

if((a>3)||(a==0)){

JOptionPane.showMessageDialog(game.this,"输入错误\n请重新输入");

return;

}

total=total-a;

jLabel2.setText(String.valueOf(total));

if(total<=0){

JOptionPane.showMessageDialog(game.this,"恭喜你,胜利了!");

return;

}

Random r=new Random();

int b=r.nextInt(4);

while(b==0)

b=r.nextInt(4);

pwd.setText(String.valueOf(b));

total=total-b;

jLabel2.setText(String.valueOf(total));

if(total<=0){

JOptionPane.showMessageDialog(game.this,"很遗憾,你输了!");

}

}

private void initGUI(){

frame=new JPanel();

getContentPane().add(frame, BorderLayout.CENTER);

frame.setLayout(null);

bntOk =new JButton();

bntOk.setText("OK");

bntOk.setBounds(150, 200, 66, 30);

frame.add(bntOk);

jLabel2= new JTextField();

jLabel2.setBounds(150, 50, 100, 50);

frame.add(jLabel2);

l1=new JLabel();

l1.setText("火柴数量");

l1.setBounds(90, 50, 100, 50);

frame.add(l1);

jLabel1= new JTextField();

jLabel1.setText("");

jLabel1.setBounds(200,149, 77, 22);

frame.add(jLabel1);

l2=new JLabel();

l2.setText("我拿火柴");

l2.setBounds(100, 149, 77, 22);

frame.add(l2);

l3=new JLabel();

l3.setText("对方拿火柴");

l3.setBounds(100, 249, 77, 22);

frame.add(l3);

pwd = new JTextField();

pwd.setBounds(200, 249, 77, 22);

frame.add(pwd);

bntOk.addMouseListener(new MouseAdapter()

{

@Override

public void mouseClicked(MouseEvent e1) {

int a=Integer.parseInt(jLabel1.getText());

if((a>3)||(a==0)){

JOptionPane.showMessageDialog(game.this,"输入错误\n请重新输入");

return;

}

total=total-a;

jLabel2.setText(String.valueOf(total));

if(total<=0){

jLabel2.setText("0");

JOptionPane.showMessageDialog(game.this,"恭喜你,胜利了!");

return;

}

Random r=new Random();

int b=r.nextInt(4);

while(b==0)

b=r.nextInt(4);

pwd.setText(String.valueOf(b));

total=total-b;

jLabel2.setText(String.valueOf(total));

if(total<=0){

JOptionPane.showMessageDialog(game.this,"很遗憾,你输了!");

}

}

}

);

}

}

  • 0
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值