求一个JAVA“拿火柴小游戏”的程序 要求如下!!!求!!!!!!!!!!!!!!!! 70
要界面的 !!!!!!!!!!!! 跪求!!!!!!!!!!!!!!!!! 展开
按照题目要求,人拿完火柴后计算机自动拿火柴,判断胜利者。鼠标点击ok或者键盘按enter键即可提交人拿的火柴个数。图形界面如下,
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,"很遗憾,你输了!");
}
}
}
);
}
}
import java.util.Random;
import java.util.Scanner;
public class PickMatch {
public static void main(String[] args) {
Scanner c = new Scanner(System.in);
Random r = new Random();
int total = r.nextInt(100);
while(true){
int n = 0;
n = total%4;
total = total-n;
System.out.println("电脑拿 "+n+" 根,还剩 "+total+" 根");
if(total==0){
System.out.println("不好意思,你输了!");
break;
}
int m = 0;
while(m==0){
try{
m = c.nextInt();
}catch (Exception e) {
System.out.println("输入有误,请重新输入!");
}
if(m>3||m<0){
m = 0;
System.out.println("输入有误,值只能在1到3之间,请重新输入!");
}
}
total = total-m;
System.out.println("你拿 "+m+" 根,还剩 "+total+" 根");
if(total==0){
System.out.println("恭喜你,获胜了!");
break;
}
}
}
}
界面没有写,只是写了控制台的小程序,代码的核心算法就是 在我拿完之后给对家剩下的只能4的倍数.
给加个界面呗!那样我就不用在编写了。谢谢了!!!!
.......,要回家买菜、做饭、洗衣服、带孩子了。。。。