java电脑小游戏_java 小游戏

下面是小程序的代码(石头剪子布):importjava.util.Random;importjavax.swing.*;importjava.awt.BorderLayout;importjava.awt.Container;importjava.awt.event.*;publicclassSmallGame...

下面是小程序的代码(石头剪子布):

import java.util.Random;

import javax.swing.*;

import java.awt.BorderLayout;

import java.awt.Container;

import java.awt.event.*;

public class SmallGame extends JFrame{

private Random r;

private String[] box = {"剪刀","石头","布"};

private JComboBox choice;

private JTextArea ta;

private JLabel lb;

private int win=0;

private int loss=0;

private int equal=0;

public SmallGame(){

// initial();

// pack();

setTitle("游戏主界面");

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

setLocation(400, 300);

setVisible(true);

}

public static void main(String []args){

new SmallGame();

}

public void initial(){

r = new Random(); //生成随机数

choice = new JComboBox();

for(int i=0; i

}

ta = new JTextArea(3,15);

ta.setEditable(false);

// JButton okBut = new JButton("出招");

// okBut.addActionListener(new ActionListener(){

// public void actionPerformed(ActionEvent e){

// ta.setText(getResult());

// lb.setText(getTotal());

// }

// });

// JButton clearBut = new JButton("清除分数");

// clearBut.addActionListener(new ActionListener() {

// public void actionPerformed(ActionEvent e){

// ta.setText("");

// win=0;

// loss=0;

// equal=0;

// lb.setText(getTotal());

// }

// });

// lb = new JLabel(getTotal());

JPanel choicePanel = new JPanel();

choicePanel.add(choice);

choicePanel.add(okBut);

choicePanel.add(clearBut);

// JScrollPane resultPanel = new JScrollPane(ta);

// JPanel totalPanel = new JPanel();

// totalPanel.add(lb);

Container contentPane = getContentPane();

contentPane.setLayout(new BorderLayout());

contentPane.add(choicePanel, BorderLayout.NORTH); contentPane.add(resultPanel, BorderLayout.CENTER); contentPane.add(totalPanel, BorderLayout.SOUTH);

}

public String getResult() {

String tmp = "";

int boxPeop = choice.getSelectedIndex();

int boxComp = getBoxComp();

tmp += "你出:\t" + box[boxPeop];

tmp += "\n电脑出:\t" + box[boxComp];

tmp += "\n结果:\t" + check(boxPeop, boxComp);

return tmp;

}

// public int getBoxComp() {

// return r.nextInt(3);

// }

public String check(int boxPeop, int boxComp) {

String result="";

// if(boxPeop == (boxComp+1)%3) {

result="你赢了!";

win++;

}

else if(boxPeop == boxComp) {

result="平";

equal++;

}

else {

result="你输了!";

loss++;

}

return result;

}

public int getPoint() {

return (win-loss)*10;

}

public String getTotal() {

return "赢:" + win + " 平:" + equal + " 输:" + loss + " 得分:" + getPoint();

}

}

有些地方我不太明白就是前面写//的? 还有我不明白他怎么判断你赢了?请高手帮我解答下 下午要答辩了 谢谢~!

展开

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值