• 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;
        }
        }
  • 相关阅读:
    Python strip()方法
    C#操作计划任务
    C# Task的用法
    C#异步编程之浅谈Task
    [深入学习C#]C#实现多线程的方法:线程(Thread类)和线程池(ThreadPool)
    [深入学习C#]C#实现多线程的方式:使用Parallel类
    详细的.Net并行编程高级教程--Parallel
    5天玩转C#并行和多线程编程 —— 第一天 认识Parallel
    C# 并行任务——Parallel类
    C#多线程--线程池(ThreadPool)
  • 原文地址:https://www.cnblogs.com/miaomeng/p/8762014.html
Copyright © 2020-2023  润新知