?? 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;//概率系數(shù),取值>=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 + -