java第二天 随机数

Random的包先生成实例 Random rm = new Random();

new Random().nextInt(); -- 返回int范围内的一个随 机整数
new Random().nextInt( n ); -- 返回0~n之间的一个随 机整数,包括0但不包括n
new Random().nextDouble(); -- 返回0~1之间的一个随 机小数,包括0但不包括1

package cn.tedu.var;

import java.util.Random;
import java.util.Scanner;

public class RandomDemo {
    public static void main(String[] args) {
        Random rm = new Random();
        Scanner sc = new Scanner(System.in);
        int real = rm.nextInt(10);
        int i =1;
        System.out.println("请猜出1-10的数字");
        while (i<=5){

            int you = sc.nextInt();
            if (you == real){
                System.out.println("恭喜你才对了:"+you);
                break;
            }else if (you >real){
                System.out.println("你猜的数字偏大了");
            }else {
                System.out.println("你猜的数字小了");
            }
            i++;
            if (i==6){
                System.out.println("你太笨了 ");
            }

        }
    }
}
posted @ 2022-10-28 13:07  竹石2020  阅读(16)  评论(0编辑  收藏  举报