请问用JAVA写一个骨子算法的程序?比如1粒骨子6个面,取随机数,该怎么实现?

解决方案 »

  1.   

    import java.util.*;public class Dice{
        public static int n=6;
        public int action(){
            Random r=new Random();
            int i=r.nextInt(n);
            return i+1;
        }
        public static void main(String[] args){
            Dice d1=new Dice(),d2=new Dice(),d3=new Dice();
            System.out.println(d1.action()+"    "+d2.action()+"     "+d3.action());
        }
    }
      

  2.   

    import java.util.Random;
    public class 色子 {
        private Random 随机;
        /**
         * 
         * 构造函数
         */
        public 色子(){
            this.随机=new Random();
        }
        /**
         * 
         * 功能: 抛投色子
         * @return 点数
         */
        public int do抛投(){
            return this.随机.nextInt(6)+1;
        }
        /**
         * 
         * 功能: 主函数
         * @param args 参数
         */
        public static void main(String[] args) {
            色子 色子=new 色子();
            for(int 变量=0;变量<50;变量++)
                System.out.print(色子.do抛投());
        }
    }