|
发表于 2013-9-30 19:27:23
|
显示全部楼层
本帖最后由 565123 于 2013-9-30 19:37 编辑
- import javax.swing.JOptionPane;
- public class C03t14 {
- public static void main(String[] args) {
- int count = 0; // 统计次数
- int correctCount = 0; // 统计正确次数
- StringBuilder str = new StringBuilder();
- do {
- str.delete(0, str.length());
- int coin = (int) (Math.random() * 10) % 2;
- int answer = JOptionPane.showConfirmDialog(null, "正面请选择是,反面请选择否",
- null, JOptionPane.YES_NO_OPTION);
- if (coin == 1)
- str.append("本次投掷为正面, ");
- else
- str.append("本次投掷为反面, ");
- if ((coin == 1 && answer == JOptionPane.YES_OPTION)
- || (coin == 0 && answer == JOptionPane.NO_OPTION)) {
- str.append("恭喜你,猜对了");
- correctCount++;
- } else
- str.append("很遗憾,猜错了");
- JOptionPane.showMessageDialog(null, str);
- count++;
- } while (JOptionPane.showConfirmDialog(null, "接着猜么?", null,
- JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION);
- double x = (double) correctCount / count * 100;
- JOptionPane.showMessageDialog(null, "你猜测准确率为" + x + "%.");
- }
- }
复制代码 |
|