zoukankan      html  css  js  c++  java
  • API的使用 compare接口的用法

    Math 类

    math.直接调用

    public static void main(String[] args) {
            System.out.println(Math.E);
            System.out.println(Math.ceil(2.1)); // 上天花板数
            System.out.println(Math.floor(2.9)); // 下天花板数
            System.out.println(Math.abs(3.5)); //绝对值
            System.out.println(Math.round(3.5)); //四舍五入
            System.out.println(Math.random()); //0.0~1.0的随机数、
            System.out.println(Math.pow(2, 3)); //返回2的3次方
            System.out.println(Math.sqrt(81)); //开平方
        }

    calendar类:

    public static void main(String[] args) {
            
            //受保护的构造方法的获得实例的方式 
            
            Calendar ca = Calendar.getInstance();
            
            //日历返日期
            Date date = ca.getTime();
            System.out.println(date);
            //获取时间
            System.out.println(ca.get(ca.YEAR));
            System.out.println(ca.get(ca.MONTH)+1);
        }

    simpledateformat:日期格式化

    public static void main(String[] args) {
                
                //日期格式化  a 代表的是上午
                Date date = new Date();
                SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd-HH:mm:ss a" );
                String format = sf.format(date);
                System.out.println(format);
                
            }

    comparable接口

    实现这个接口

    class Dog implements Comparable<Dog>{
        int age;
        String name;
        public Dog(int age, String name) {
            super();
            this.age = age;
            this.name = name;
        }
        @Override
        public int compareTo(Dog o) {
            //比较信息
            if(this.age > o.age){
                return 1;
            }else{
                return -1;
            }
        }
    }
    public class CompareTest {
        
        public static void main(String[] args) {
            Dog dog1 = new Dog(2, "小胡");
            Dog dog2 = new Dog(3, "胖胖");
            System.out.println(dog1.compareTo(dog2));
        }
        
    }
    package com.qc.java1801.api;
    
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Comparator;
    import java.util.List;
    import java.util.TreeSet;
    
    class Student{
        String name;
        int score;
        public Student(String name, int score) {
            super();
            this.name = name;
            this.score = score;
        
        }
        @Override
        public String toString() {
            return "Student [name=" + name + ", score=" + score + "]";
        }
    }
    
    /*class compar implements Comparator<Student>{
    
        @Override
        public int compare(Student o1, Student o2) {
            return o1.score - o2.score;
        }
    }*/
    
    public class ComparetoTest {
    
        public static void main(String[] args) {
            List<Student> students = new ArrayList<Student>();
            students.add(new Student("张三", 90));
            students.add(new Student("李四", 80));
            students.add(new Student("王二麻", 100));
            //用了匿名内部类
            Collections.sort(students, new Comparator<Student>() {
    
                @Override
                public int compare(Student o1, Student o2) {
                    return o1.score - o2.score;
                }
            });
            System.out.println(students);
        }
    }

    comparator 和 comparable区别;

    comparable自己实现

    comparator需要第三方实现

    抓取异常和抛异常

        public static void main(String[] args) {
            Park.ticket = 10000;
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入年龄");
                
                try {
                    int age = sc.nextInt();
                    System.out.println(Park.sellticket(age));
                } catch (InputMismatchException e) {
                    
                    System.out.println("输入的格式不正确");
                } catch (Exception e) {
                    System.out.println(e.getMessage());
                }
    }
    
    
    static class Park{
        
        static int ticket;
        public static int sellticket(int age) throws Exception{
            if(age > 100 || age < 0){
                throw new Exception("年龄不合适");
                }else if(age > 60){
                    ticket = 0;
                }else if(age < 10){
                    ticket = ticket/2;
                }else{
                    return ticket;
                }
            
            return ticket;
        }
        }
  • 相关阅读:
    要回家了,想到以后..
    面试应注意的问题by JohnPhilips(转自matrix论坛)
    在痛苦中坚持,翻译啊
    数据抓取和分析~~
    不考研,就很闲吗?
    在Swing中使用高级的MVC和POJOs
    测试驱动
    开始找工作了,先记个流水帐
    又是开学的时候了
    开发者想要什么算是我的翻译作品的处女作吧,呵呵,致力于英语学习..
  • 原文地址:https://www.cnblogs.com/miaomeng/p/8762014.html
Copyright © 2011-2022 走看看