0
点赞
收藏
分享

微信扫一扫

Java、正面或反面

芒果六斤半 2022-01-31 阅读 65

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


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 条评论