📄 betgame.java
字号:
//修正了一个BUG
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class BetGame extends JFrame implements ActionListener{
private final double fac = 5;//概率系数,取值>=2, 越接近2越不易获胜
private JRadioButton a,b,c;
private JLabel value;
private JLabel rs;
private JButton bet,exit;
private JComboBox co;
private int coin = 10000;;
public BetGame(){
super("真钱游戏");
this.getContentPane().setLayout(null);
value = new JLabel("资金:"+coin);
value.setFont(new Font("simsun",Font.PLAIN,12));
value.setBounds(16,12,100,22);
this.getContentPane().add(value);
a=new JRadioButton("正面");
b=new JRadioButton("反面");
c=new JRadioButton("",true);
ButtonGroup g = new ButtonGroup();
g.add(a);g.add(b);g.add(c);
a.setBounds(16,48,68,25);
b.setBounds(88,48,68,25);
this.getContentPane().add(a);
this.getContentPane().add(b);
JLabel l = new JLabel("下注");
l.setBounds(16,88,48,25);
this.getContentPane().add(l);
co = new JComboBox(new String[]{"5","10","20","50","100","200","500","全部",});
co.setBounds(68,88,88,25);
this.getContentPane().add(co);
rs = new JLabel();
rs.setBounds(16,120,100,25);
rs.setFont(value.getFont());
this.getContentPane().add(rs);
bet = new JButton("开始");
bet.setBounds(16,158,68,25);
bet.addActionListener(this);
this.getContentPane().add(bet);
exit=new JButton("离开");
exit.setBounds(95,158,68,25);
exit.addActionListener(this);
this.getContentPane().add(exit);
this.setSize(198,238);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
new BetGame().setVisible(true);
}
public void actionPerformed(ActionEvent e) {
if(e.getSource()==bet){
if(coin<=0){
JOptionPane.showMessageDialog(this,"您的帐户余额不足,请充值!");
return;
}
if(c.isSelected()){
JOptionPane.showMessageDialog(this, "请选择正面或反面");
return;
}
int its = co.getItemCount();
int id = co.getSelectedIndex();
if(id<its-1)
{
if(Integer.parseInt(co.getSelectedItem().toString())>coin){
JOptionPane.showMessageDialog(this,"您的投注额超过了您的余额!");
return;
}
}
double rate = .5;
rate-=id/(its*fac);//投注额越多中将概率相应降低
System.out.println("获胜概率 :"+rate);
boolean win = Math.random()<rate;
if(win){
if(id==its-1)
coin*=2;
else
coin+=Integer.parseInt(co.getSelectedItem().toString());
}
else{
if(id==its-1)
coin=0;
else
coin-=Integer.parseInt(co.getSelectedItem().toString());
}
rs.setText(win?a.isSelected()?"正面":"反面":a.isSelected()?"反面":"正面");
value.setText("资金:"+coin);
// co.setSelectedIndex(0);
// c.setSelected(true);
}
else
System.exit(0);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -