编写一个程序,模拟扔硬币的结果.

package 第三天_练习题;
//编写一个程序,模拟扔硬币的结果.
public class Test2 {
	public static void main(String[] args) {
		
		int count1=0;//统计正面
		int count2=0;//统计反面
		for (int i = 0; i <1000; i++)
		{
			double d = Math.random();//产生[0,1)
			if(d>=0.5)
			{
				//System.out.println("正面");
				count1++;
			}
			else
			{
				//System.out.println("反面");
				count2++;
			}
		}
		System.out.println("正面"+count1+"\n反面"+count2);
		
	}

}

猜你喜欢

转载自blog.csdn.net/u012110719/article/details/42713897