| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 4870 人关注过本帖
标题:编写一个程序模拟扔硬币的结果
只看楼主 加入收藏
lampeter123
Rank: 16Rank: 16Rank: 16Rank: 16
等 级:版主
威 望:54
帖 子:2508
专家分:6424
注 册:2009-1-30
结帖率:90.32%
收藏
 问题点数:0 回复次数:3 
编写一个程序模拟扔硬币的结果
import java.util.Random;

public class Coin {

    public static void main(String[] args) {
        Random rand = new Random();
        int x = 0;
        int y = 0;
        for(int j = 0 ; j < 1000000; j++) {
            int i = rand.nextInt(2);
            //System.out.println(i);
            if (i==1) {
                x++;
            } else {
                y++;
            }
        }
        System.out.println("x="+x);
        System.out.println("y="+y);
        System.out.println((float)x/y);
        

    }

}
搜索更多相关主题的帖子: 模拟 编写 结果 硬币 
2010-01-28 21:57
流星雨
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:JAVA风暴
等 级:版主
威 望:43
帖 子:1854
专家分:1868
注 册:2004-5-30
收藏
得分:0 
随机数字

感谢你们带我找到星空下美丽神话,无论经历多少苦痛也不放弃的梦;插上希望翅膀乘风我和你们飞翔,飞过海天尽头携手把梦想实现.....
2010-01-29 14:31
wlsj
Rank: 2
等 级:论坛游民
帖 子:49
专家分:28
注 册:2009-5-18
收藏
得分:0 
嗯,支持一个
2010-02-02 17:06
pauli
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2010-2-2
收藏
得分:0 
呵呵 正巧我今天下午也做了一个类似的程序 这个是为了一个小游戏做的TEST 请高手指教

import java.util.Random;   
import java.util.*;
public class Game     
{
    public static void main(String[] args)   
    {      
        {   
            int a, b = 0;
            Scanner input = new Scanner(System.in);
            System.out.println("Please enter the scope of the coins: ");
            a=input.nextInt();
//            for(int i=0; i<1; ++i)
            b = new Random().nextInt(a)+30;
            System.out.println("\nThe number of coins is " + b);
        }   
            
            Scanner input = new Scanner(System.in);
            System.out.println("\nPlease choose the side of the coin:(0 or 1) ");
            int Num = input.nextInt();
            System.out.println("\nThe side you chose is " + Num);
            int RandomNum = new Random().nextInt(2);
            System.out.print("\nThe side of coin is " + RandomNum);
            if (Num == RandomNum)
            {
                System.out.print("\n\nCongratulations! Please take the coins:");
            }
            else
            {
                System.out.print("\n\nSorry, Wrong side, Please wait...");
            }
    }
        
}
2010-02-02 21:23
快速回复:编写一个程序模拟扔硬币的结果
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.013065 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved