Java、正面或反面

阅读 65

2022-01-31

        编写程序,模拟抛硬币一百万次,显示出现正面和反面的次数。


package pack2;

import java.security.SecureRandom;

public class Count {

	public static void main(String[] args) {
		count();
	}

	//硬币正反面出现次数
	public static void count() {
		int n = 1_000_000, positive, negative, guess;
		positive = negative = 0;
		
		for (int i = 0; i < n; i++) {
			guess = new SecureRandom().nextInt(2);
			if(guess == 0) positive++;
			else negative++;
		}
		System.out.println("Positive: "+positive+"\nNegative: "+negative);
	}
}

 

精彩评论(0)

0 0 举报