基本数据类型的包装类和随机数

public class BasisTest {

    /**
     *  封装类/包装类 :  把 基本数据类型转换成对象!
     * 
     *  基本数据类型               包装类
     *  byte           Byte
     *  short          Short
     *  int            Integer
     *  long           Long
     *  float          Float
     *  double         Double
     *  char           Character
     *  boolean        Boolean
     *  
     *  使用我们的封装类 ,就可以使用类中对应的方法
     *  集合中在存储基本数据类型时!只能存放封装类!
     */

    // 所有的封装类 都有将对应的基本数据类型作为参数的方法 来构造实例
    @Test
    public void test01() {
        /**
         * 除了Character来构造实例的时候,没有String类型的参数!
         * Float有三个实例化构造方法  分别是  传递 double  float  String
         */
        Byte a = new Byte((byte) 1);
        Short s = new Short((short) 1);
        Integer b = new Integer(5);
        Long l = new Long(1);
        Float f = new Float(2);
        Double c = new Double(5);
        // 上面的六个封装类都继承了Number
        Boolean boolean1 = new Boolean(true);
        Character character1 = new Character((char) 1);
        Character character2 = new Character('1');
    }

    /**
     * 六个封装类继承了Number
     * 用String来构造实例的时候,String中存放的必须是数值类型的字符串
     */
    @Test
    public void test02() {
        Byte a = new Byte("1");
        Short s = new Short("1");
        Integer b = new Integer("1");
        Long l = new Long("1");
        Float f = new Float("1");
        Double c = new Double("1");
        // 除了大小写的true 其它都返回false
        Boolean boolean1 = new Boolean("");
        System.out.println(boolean1);
        // 编译报错 Character character1 = new Character("1");
    }

    @Test
    public void test03() {
        System.out.println(Integer.toBinaryString(28)); // 转换成2进制
        System.out.println(Integer.toHexString(28)); // 转换成16进制
        System.out.println(Integer.toOctalString(28)); // 转换成8进制
    }

    /**
     * 六个封装类继承了Number,只要不是传递 字符串类型的数值
     * 都会抛出NunberFormatException
     */
    @Test
    public void test04() {
        // Integer integer = new Integer(null);
        double a = 1;
        System.out.println(a);
        Double double1 = new Double(1);
        System.out.println(double1);
    }

    /**
     * 
     *除了Character,都有对应的parse的方法
     */
    @Test
    public void test05() {
        Integer integer = new Integer(5);
//第一个参数是对应进制的写法 System.out.println(integer.parseInt("11011", 2)); /** String num = "27"; System.out.println(Integer.parseInt(num) + 1); Double.parseDouble("20"); Byte.parseByte("1"); Short.parseShort("1"); System.out.println(1 + 1 + "2"); // 22 System.out.println("1" + (1 + 2)); // 13 */ } /** * valueOf * 把基本数据类型转换成对应的封装类 * 除了Character没有传递String类型的参数 * * xxxValue * 把xxx类型转换成xxx对应的基本数据类型 */ @Test public void test06() { // 基本数据类型和对应封装类之间的转换 我们称之为 装箱和拆箱操作 int a = 5; Integer integer = Integer.valueOf(a); integer.intValue(); Double double1 = Double.valueOf(20); double1.doubleValue(); Character character = Character.valueOf('a'); character.charValue(); } @Test public void test07() { Integer a = new Integer(1); Integer b = new Integer(1); int c = 1; System.out.println(a == b); // false 都是new出来的对象 System.out.println(a == c); } @Test public void test08() { /** *底层默认执行了valueOf() *如果值在 -128到127 之间 则不会new对象 *否则 会new出来新的对象 */ Integer a = 128; Integer b = 128; System.out.println(a == b); // false } }
复制代码

 


复制代码
@Test
    public void testMath() {
        System.out.println("向上取值:" + Math.ceil(50.1));
        System.out.println("向下取值:" + Math.floor(50.9));
        System.out.println("绝对值:" + Math.abs(-50.9));
        System.out.println("最大值:" + Math.max(50, 60));
        System.out.println("最小值:" + Math.min(50, 60));
        System.out.println("随机数:" + (int) (Math.random() * 10));
        // 之前使用的Math.random() 其实是调用了Random.nextDouble();
    }

    @Test
    public void testRandom() {
        Random random = new Random();
        for (int i = 0; i < 100; i++) {
            // System.out.println(random.nextBoolean()); 随机boolean类型的值
            // System.out.println(random.nextDouble()); 0-1之间
            // System.out.println(random.nextInt()); int取值范围
            System.out.println(random.nextInt(10)); // 参数的范围 但是不包含本身 参数必须大于0
        }
    }
复制代码

 

//随机数
    @Test
    public   void  test12(){
        Scanner scanner=new Scanner(System.in);
        System.out.println("请输入一个4位数字:");
        String num=scanner.next();
        //把String类型的num转换成int
        int realNum=Integer.parseInt(num);
        //获取百位
        int bai=realNum/100%10;
        Random random=new Random();
        //定义一个标记
        boolean flag=false;
        for (int i = 1; i <=100; i++) {
        int a=    random.nextInt(10);
        System.out.println("第"+i+"次的随机数:"+a);
            if (a==bai) {
                //找到了
                flag=true;
                break;
            }
        }
        if (flag) {
            System.out.println("中奖了");
        }else {
            System.out.println("下次努力...");
        }
    }
    
}
原文地址:https://www.cnblogs.com/xiaobaizhang/p/7761495.html