Math类、Random类

java.lang.Math类提供了常用的数学运算方法和两个静态常量E(自然对数的底数)和PI(圆周率)

 * @Description//Math类常用方法
 */
public class LuckCard {
    public static void main(String[] args) {
        //提供数学运算的功能
//        System.out.println(Math.abs(-8.7));
//        System.out.println(Math.max(100,9));
//        System.out.println(Math.min(98.7,-7.9));
        //幸运抽奖:Math.random()生成0.0(包括)--1.0之间的随机数
        int random=(int)(Math.random()*10);
        System.out.println("系统生成的随机数为:"+random);
        Scanner input=new Scanner(System.in);
        System.out.println("请输入您的四位会员卡号");
        int no=input.nextInt();
        int baiwei=no/100%10;
        if (baiwei==random){
            System.out.println("恭喜您,中奖了!");
        }else {
            System.out.println("不好意思,谢谢参与!");
        }
    }
}
 * @Description//Random类生成随机数
 */
public class RandomDemo {
    public static void main(String[] args) {
        //创建随机数生成器
        //随机数生成器种子不同,每次生成的随机数不同的
        //随机数生成器种子相同,每次生成的随机数是相同的
        Random random=new Random(500);
        Random random1=new Random(500);
        //常用写法1:
//        Random random=new Random();
        //常用写法2:
//        Random random=new Random(时间的毫秒);
        
        //生成随机数
//        System.out.println(random.nextInt());//生成一个int范围内的随机整数
        System.out.println(random.nextInt(10));//生成一个0--10范围内的随机整数
        System.out.println(random1.nextInt(10));
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值