手机java猜拳游戏代码 手机java猜拳游戏代码是什么( 四 )


user_int = 1;
}else if("布".equals(user)){
user_int = 2;
}else{
System.out.println("请正确输入石头剪子布...");
i--;
continue;
}
System.out.println("user_int"+user_int);
System.out.println("computer"+computer);
//判断输赢
if(user_int-computer==-1 || user_int-computer==2){
userWin++;
System.out.println("您赢手机java猜拳游戏代码了"+userWin+"次");
if(userWin==3){
System.out.println("恭喜!您赢了");
break;
}
}else if(computer-user_int==-1 || computer-user_int==2){
computerWin++;
System.out.println("您输了"+computerWin+"次");
if(computerWin==3){
System.out.println("对不起!您输了");
break;
}
}else{
System.out.println("和局");
i--;
continue;
}
}
}
}
求JAVA人机猜拳的代码,类似一下界面 。自己纯手打,老半天才弄出来啊
import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.AbstractAction;
import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
public class Demo2 extends JFrame {
private JLabel lb1, lb2, lb3, lb4; // 提示标签
private JTextField ta1, ta2;// 两个文本框
private JButton b1, b2, b3; // 三个按钮
private JPanel p1, p2; // 两个JPanel面板
public Demo2() {
// 初始化所有组件
lb1 = new JLabel("欢迎使用人机猜拳程序");
lb2 = new JLabel("你出拳:");
lb3 = new JLabel("电脑出拳:");
lb4 = new JLabel("结果");
ta1 = new JTextField();
ta1.setPreferredSize(new Dimension(60, 60)); // 设置大小
ta1.setEditable(false);//设置不可编辑
ta2 = new JTextField();
ta2.setPreferredSize(new Dimension(60, 60));
ta2.setEditable(false);//设置不可编辑
b1 = new JButton("剪刀");
b2 = new JButton("石头");
b3 = new JButton("布");
p1 = new JPanel();
p2 = new JPanel();
// 设置第一个面板内容
Box box = Box.createVerticalBox();
Box box1 = Box.createHorizontalBox();
box1.add(lb2);
box1.add(ta1);
box1.add(lb3);
box1.add(ta2);
box.add(lb1);
box.add(Box.createVerticalStrut(40));
box.add(box1);
box.add(Box.createVerticalStrut(10));
box.add(lb4);
box.add(new JLabel());
p1.add(box);
// 设置第二个面板
p2.setLayout(new GridBagLayout()); // 使用GridBagLayout布局管理器
p2.setPreferredSize(new Dimension(0, 60));
GridBagConstraints g2 = new GridBagConstraints();
g2.fill = GridBagConstraints.BOTH;
g2.weightx = 1.0;
g2.weighty = 1.0;
g2.gridx = 0;
g2.gridy = 0;
p2.add(b1, g2);
g2.gridx = 1;
p2.add(b2, g2);
g2.gridx = 2;
p2.add(b3, g2);
//为3个按钮添加事件
b1.addActionListener(new buttonAction());
b2.addActionListener(new buttonAction());
b3.addActionListener(new buttonAction());
this.getContentPane().add(p1);
this.getContentPane().add(p2, BorderLayout.SOUTH);
this.setTitle("机器人猜拳游戏");
this.setSize(300, 300);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
//事件类
class buttonAction extends AbstractAction{
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource()==b1){
ta1.setText("剪刀");
init(ta1.getText());
}else if(e.getSource()==b2){
ta1.setText("石头");
init(ta1.getText());

推荐阅读