Java三章学习内容(枚举,包装类,Math)

一.枚举(enum)
固定常量组成的类型

     public enum Curriculum {                    //创建枚举类型,录入所需要固定的信息,逗号隔开
L1,L2,L3

 public class Kecheng {                            //在自定义类型中使用枚举类型进行设置属性
private  Curriculum lv

public void fangfa( Curriculum lv){                            //定义相应方法
switch ( lv) {
case L1:
System.out.println("大数据开发工程师");
break;


case L2:
System.out.println("大数据发掘工程师");
break;


case L3:
System.out.println("大数据架构师");
break;
}
}

 public class MeijuText {                                         //在测试类实例化对象调用方法,只能使用规定枚举类型
        public static void main(String[] args) {
        Kecheng kc = new Kecheng();
        kc.fangfa( Curriculum.L1);
    }
}

二.包装类
基本类型转换为对象,转换为对象可提供相应的方法,集合不能存放基本数据类型,需要先包装类型

1. 创建一个Integer类型的包装对象
Integer intValue=new Integer(21);                //基本数字类型方法

Integer intValue=new.Integer("21");            //字符串类型方法

2. 使用包装类的valueOf()方法创建包装类对象

Integer intValue=Integer.valueOf("21");        

3.包装类转换成基本数据类型
Integer intValue=new Integer(21);
int i =intValue.intValue();

jdk1.5后都支持自动转换,包装类型所需作为引用对象时才使用,滥用会导致系统更大的负担,基本类型则不会!!!

三.Math
随机数:  int random = (int)(
Math.random()*10);   随机1~10的整数

System.out.println((int)((Math.random()*9+1)*100000));  生成6位

System.out.println((int)((Math.random()*9+1)*10000));   生成5位

System.out.println((int)((Math.random()*9+1)*1000));    生成4位


 public class Suiji {                                                                //该代码目的是随机两个一样的随机数
            public static void main(String[] args) {
            Random random = null;
            Random random2 = null;

            int num =(int) ( Math.random()*10 );

            random=new Random(num);
            random2=new Random(num);

            int rand1 = random.nextInt(10);
            int rand2 = random2.nextInt(10);
            System.out.println(rand1+"--"+rand2);
    }

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值