定义一个名为GenericCoin的类表示一枚没有面值也没有名字的硬币。这枚硬币应该有一个顶面与一个底面,并且可以标明哪一面朝上。应该能够抛掷这枚硬币使之随机掉落,或者顶面朝上,或者底面朝上。
a.编写一个程序将两枚硬币各抛掷50次。记录并报告每枚硬币各有多少次顶面朝上,报告那一枚硬币顶面朝上的次数多
b.编写一个程序来玩一个简单的抛硬币游戏。要求用户猜测隐蔽会是顶面还是底面朝上,然后跑硬币,并将结果告诉用户,宣告用户的猜测是否正确,并将结果裂成表格。用户可以持续的玩这个游戏,想玩多久就玩多久。当用户退出时,显示一个游戏总结,包括:抛硬币的次数,顶面朝上的次数,底面朝上的次数,猜测正确的次数,和猜测正确的百分比。并可以应用户要求以图形演示每次抛硬币的结果。
GenericCoin类,非常简单,里面有个方法cast表示抛一次硬币动作,用了java里的随机数和取绝对值操作(防止出现负数),这里用了枚举类表示硬币的正面和反面
package com.crazycoder2010.datastructure.ch01;import java.util.Random;public class GenericCoin {//表示硬币的正面和反面public static enum FACE{FONT,BACK;}private FACE face;public FACE getFace() {return face;}public void cast(){int index = 1;Random random = new Random();index = random.nextInt()%2;this.face = FACE.values()[Math.abs(index)];}
}
测试称许a-模拟两个硬币一起抛做数据对比
package com.crazycoder2010.datastructure.ch01;import com.crazycoder2010.datastructure.ch01.GenericCoin.FACE;public class GenericCoinTest {public static void main(String[] args) {GenericCoin coin1 = new GenericCoin();int index1 = countFace(coin1,50);GenericCoin coin2 = new GenericCoin();int index2 = countFace(coin2,50);System.out.println("Coin 1:"+index1);System.out.println("Coin 2:"+index2);}private static int countFace(GenericCoin coin,int times){int total = 0;for(int i = 0;i }
这个称许的输出结果为:
Coin 1:23
Coin 2:21
测试程序b-提供ui界面给用户,这里用的是jframe
package com.crazycoder2010.datastructure.ch01;import java.awt.FlowLayout;
import java.awt.HeadlessException;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;import com.crazycoder2010.datastructure.ch01.GenericCoin.FACE;public class GenericCoinTestUI extends JFrame {private static int index = 0;private List results = new ArrayList();// 保存所有猜测结果private Map actual = new HashMap();//实际硬币展示的数量,key:正面/反面,value:对应的硬币正面或反面出现的次数private static final long serialVersionUID = 2948063316297165651L;public GenericCoinTestUI() throws HeadlessException {super();setTitle("抛硬币游戏");setSize(500, 550);setResizable(false);this.getContentPane().setLayout(new FlowLayout());//游戏开始之前反面/正面出现的实际次数都为0actual.put(FACE.FONT, 0);actual.put(FACE.BACK, 0);this.addWindowListener(new WindowAdapter() {@Overridepublic void windowClosing(WindowEvent e) {StringBuffer message = new StringBuffer("----游戏总结----\n");message.append("猜测总次数:").append(results.size()).append("\n");message.append("正面次数:").append(actual.get(FACE.FONT)).append("\n");message.append("反面次数:").append(actual.get(FACE.BACK)).append("\n");int success = doSummarySuccess(results);message.append("猜中次数:").append(success).append("\n");message.append("猜中百分比:").append(Float.valueOf(success)*100/results.size()).append("%\n");JOptionPane.showMessageDialog(null, message);System.exit(0);}});final AbstractTableModel tableModel = new MyTable(this.results);JTable table = new JTable(tableModel);JScrollPane scrollPane = new JScrollPane(table);final JComboBox faces = new JComboBox();faces.addItem("正面");faces.addItem("反面");this.getContentPane().add(faces);JButton throwButton = new JButton("猜猜");this.getContentPane().add(throwButton);final JLabel label = new JLabel();this.getContentPane().add(label);throwButton.addActionListener(new ActionListener() {@Overridepublic void actionPerformed(ActionEvent e) {GenericCoin coin = new GenericCoin();coin.cast();actual.put(coin.getFace(), actual.get(coin.getFace())+1);Guess guess = new Guess(++index, FACE.values()[faces.getSelectedIndex()],coin.getFace() == FACE.values()[faces.getSelectedIndex()]);results.add(guess);label.setText(guess.isRight()?"猜中了!":"没猜中!");tableModel.fireTableDataChanged();}});this.getContentPane().add(scrollPane);}public static void main(String[] args) {new GenericCoinTestUI().setVisible(true);}private int doSummarySuccess(List guesses){int total = 0;for(Guess guess : guesses){if(guess.isRight()){total++;}}return total;}private static class MyTable extends AbstractTableModel {private String[] header = { "索引", "猜测", "猜中" };private List guesses;private MyTable(List guesses){this.guesses = guesses;}@Overridepublic int getRowCount() {return guesses.size();}@Overridepublic String getColumnName(int column) {return header[column];}@Overridepublic int getColumnCount() {return header.length;}@Overridepublic Object getValueAt(int rowIndex, int columnIndex) {Guess guess = guesses.get(rowIndex);if(columnIndex == 0){return guess.getIndex();}else if(columnIndex == 1){return guess.getFace();}else if(columnIndex == 2){return guess.isRight();}return null;}}private static class Guess {//这个类表示用户的一次猜测行为private int index;//猜测的索引,第一次猜猜,第二次猜测。。。private FACE face;//用户猜测的硬币面-正面或反面private boolean right;//这次猜测活动用户是否猜中public Guess(int index, FACE face, boolean right) {this.index = index;this.face = face;this.right = right;}public int getIndex() {return index;}public FACE getFace() {return face;}public boolean isRight() {return right;}}
}
看一下运行结果界面: