随笔都是学习笔记
随笔仅供参考,为避免笔记中可能出现的错误误导他人,请勿转载。
import java.util.Random;

class Coin{
    private int one = 0;
    private int zero = 0;
    private Random random = new Random();
    public void throwCoin(int count){    // 抛硬币的行为,传入抛出次数
        for (int i = 0; i < count; i++) {
            int num = this.random.nextInt(2);
            if (num == 1){
                one ++;
            }else {
                zero ++;
            }
        }

    }

    public int getOne() {
        return one;
    }

    public int getZero() {
        return zero;
    }
}
public class MAIN {
    public static void main(String[] args) {
        Coin coin = new Coin();
        coin.throwCoin(100);
        System.out.println("'正面'出现了 " + coin.getOne() + " 次。");
        System.out.println("'反面'出现了 " + coin.getZero() + " 次。");
    }
}

 

posted on 2022-02-12 15:02  时间完全不够用啊  阅读(157)  评论(0编辑  收藏  举报