zoukankan      html  css  js  c++  java
  • 基本数据类型的包装类和随机数

    /**
     *                     
     *   包装类/封装类:  把基本数据类型转换成对象!  
     *               每一个基本数据类型都有一个对应的包装类!都是位于java.lang包中!               
     *          作用: 提供我们操作的一系列方法!      
     *           例子:集合中能存放基本数据类型吗???不能! 必须是包装类! 
     *           
     *   基本数据类型                             包装类
     *   byte                Byte   
     *   short               Short  
     *   int                 Integer    
     *   long                Long   
     *   float               Float  
     *   double              Double 
     *   char                Character   
     *   boolean             Boolean 
     *                 
     */
    public class BasisTest {
    
        @Test
        public  void  test01(){
            //所有的包装类都有将对应的基本数据类型作为参数!来构造实例!
             Integer  i=new Integer(1);
             Double   d=new Double(5);
             Boolean b=new Boolean(true);
             Character c=new Character('a');
        }
        
        
        @Test
        public  void  test02(){
            //除了Character类,其他所有的包装类都可以传一个字符串作为参数!
             Integer  i=new Integer("1");
             Double   d=new Double("5");
             Boolean b=new Boolean("true");
             //Character c=new Character("a");  编译报错
        }
        
        /*
         * Byte,Short,Integer,Float,Double,Long
         * 都继承了 Number这个父类
         * 当他们的参数是String来构造实例的时候  ,如果传递Null都抛出 NumberFormatException
         * 
         * Boolean    传Null是   能正常运行
         * Character  编译就报错了
         */
        @Test
        public  void  test03(){
            //Integer  i=new Integer(null);
            //Double   d=new Double(null);
            //Character c=new Character(null); 编译报错
            //在参数是String的类型时,除了 大小写的TRUE,其余的都是false
            Boolean b=new Boolean(null);
            System.out.println(b);
        }
        
        @Test
        public  void  test04(){
            String  num="12345a";
            System.out.println(num+1);  //123451  
            //使用Integer的paseInt(数值类型的String)
            System.out.println(Integer.parseInt(num)+1);
        }
        
        /*
         * valueOf() 只能传递可以转换成对应基本数据类型的值  
         * 除Character类外,其他包装类都有方法(字符串->包装类)
         */
    
        @Test
        public  void  test05(){
            Integer i=Integer.valueOf(20);  
            Integer a=Integer.valueOf("20"); 
            Integer b=Integer.valueOf("abc");  //NumberFormatException
            Integer c=Integer.valueOf("20.0");  //NumberFormatException
        }
        
        
        //基本数据类型和对应包装类之间的转换 我们称之为  装箱和拆箱操作
        @Test
        public  void  test06(){
            int a =5;
            Integer i=new Integer(a);   //相当于装箱
            a=i.intValue(); //相当于拆箱
            Integer b=5; //相当于装箱
            int  num=b;  //相当于拆箱
        }
        
        
        
        //所有的包装类 都有对应的方法叫做    包装类Value()  作用:转换成基本数据类型 (拆箱)
        @Test
        public  void  test07(){
            Double a=new Double(50);
            Double b=new Double(50.5);
            Double c=new Double("50.5");
            double d=c.doubleValue();  //转换成基本数据类型 
            System.out.println(d);
            Float f=new Float(58.0);
            float e=f.floatValue(); //转换成基本数据类型 
            Character t =new Character('a');
            System.out.println(t.charValue()+1);
            
            Boolean l=new Boolean("true");
            System.out.println(l.booleanValue());
        }
        
        
        @Test
        public  void  test08(){
            /*
             * 除了 Character之外 ,每个包装类都有对应的parse方法
             */
            System.out.println(Integer.parseInt("123"));
            System.out.println(Double.parseDouble("123"));
            System.out.println(Boolean.parseBoolean("123"));
            System.out.println(Long.parseLong("123"));
            /*int  n =null;   编译报错
            Integer an=null;*/
        }
        
        
        
        @Test
        public  void  test09(){
            Integer a=50;
            Integer b=new Integer(50);
            int     c=50;
            System.out.println(a==b);  //false
            System.out.println(c==b);  //true    基本数据类型和对应的包装类比较 都是true
            System.out.println(c==a);  //true
        }
        
        
        @Test
        public  void  test10(){
            /*Integer a=new Integer(50);
            Integer b=new Integer(50);
            System.out.println(a==b);  //false*/
        /*    Integer a=127;
            Integer b=127;
            System.out.println(a==b); //true*/    
        
            Integer a=128;  //底层 执行了  valueOf()   new  Integer(128)
            Integer b=128;
            System.out.println(a==b); //false
            
        }
        
        
        
        
        @Test
        public  void  test11(){
            System.out.println("向上取值:"+Math.ceil(50.1));
            System.out.println("向下取值:"+Math.floor(50.2));
            System.out.println("绝对值:"+Math.abs(-50));
            System.out.println("最大值:"+Math.max(50, 60));
            System.out.println("最小值:"+Math.min(50, 60));
            System.out.println("随机数:"+(int)(Math.random()*10));
            
            Random random=new Random();
            for (int i = 1; i <=100; i++) {
                System.out.println(random.nextInt());  //int值的区间
            }
            System.out.println("*********************");
            for (int i = 1; i <=100; i++) {
                System.out.println(random.nextInt(100));  //0-99  不包含100
            }
        }
        
        
        
        //随机数
        @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("下次努力...");
            }
        }
        
    }
    

      

  • 相关阅读:
    HDU4004The Frog's Games(二分求恰当的步长)
    HDU2899Strange fuction (二分)
    HDU4190Distributing Ballot Boxes
    HDU2059龟兔赛跑(加油站)
    HDU2594 Simpsons’ Hidden Talents (kmp找寻两串s1,s2中相同的部分)
    HDU1711Number Sequence (kmp找母串ns[]中子串ms[]第一次出现时,首位的位置)
    HDU1874畅通工程续(最短路模版)
    HDU3790最短路径问题
    1655 文本计算器
    具有相同元素的排列组合模板
  • 原文地址:https://www.cnblogs.com/HHR-SUN/p/7015676.html
Copyright © 2011-2022 走看看