黑马程序员技术交流社区

标题: Math类中的random方法 [打印本页]

作者: shijialong1111    时间: 2016-7-3 19:32
标题: Math类中的random方法
3.Math类中的random方法
   求5-20之间的随机数, for循环重复输出10次.
作者: beckhamgun    时间: 2016-7-3 20:02
public class Day07_3 {
        public static void main(String[] args) {
                //int ran = (int)(Math.random()*15+5);

                for (int i=1; i<=10; i++) {
                        int ran = (int)(Math.random()*16+5);
                                System.out.println(ran);
                }
        }
}
作者: weidong10heima    时间: 2016-7-3 22:20
import java.util.Random;
/*生成5至20之间的随机整数,for循环重复输出10次.*/
public class MyDemo6_Random {
    public static void main(String[] args) {
        Random rm = new Random();
         for(int i = 0; i<10; i++){
            int sj5 = rm.nextInt(16)+5;
            System.out.println(sj5 );
        }
    }
}




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2