关于JAVA的题目:剪刀石头布的小程序!急急急!

编写要求就是必须要用method的方法来写。如果是if语句什么的就不需要了。谢谢大家了啊!... 编写要求就是必须要用method的方法来写。如果是if 语句什么的就不需要了。谢谢大家了啊! 展开
 我来答
匿名用户
2013-12-15
展开全部
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 final 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() {
setTitle("Small Game");
initial();
pack();
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocation(400, 300);
setVisible(true);
}

public void initial() {
r = new Random();

choice = new JComboBox();
for(int i=0; i<box.length; i++) {
choice.addItem(box[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 getBoxPeop(String str) {
return choice.getSelectedIndex();
}

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();
}

public static void main(String[] args) {
SmallGame game = new SmallGame();
}
}
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式