【Day04-作业练习】

2.1【Math类】请编程进行以下运算:

请计算3的5次幂

请计算3.2向上取整的结果

请计算3.8向下取整的结果

请计算5.6四舍五入取整的结果

2.2【BigDecimal类】有以下double数组:double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};

请编程计算它们的总值及平均值(四舍五入保留小数点后2位)  

public class Test1 {
    public static void main(String[] args) {
        System.out.println(Math.pow(3, 5));
        System.out.println(Math.ceil(3.2));
        System.out.println(Math.floor(3.8));
        System.out.println(Math.round(5.8));

        double[] arr = {0.1,0.2,2.1,3.2,5.56,7.21};
        BigDecimal count = new BigDecimal(arr.length);
        BigDecimal sum = new BigDecimal("0");
        for (double a : arr) {
            BigDecimal bigDecimal = new BigDecimal(String.valueOf(a));
            sum = sum.add(bigDecimal);
        }
        BigDecimal avg = sum.divide(count, 2, RoundingMode.HALF_UP);
        System.out.println("总值" + sum.doubleValue());
        System.out.println("平均值" + avg.doubleValue());
    }
}

2.3【Date类、DateFormat类 LocalDateTime TimeFormatter】

请在控制台以“xxxx年xx月xx日 xx时xx分xx秒”的格式打印当前系统时间。

请编写程序,从控制台接收一个字符串“日期”,格式:yyyy-MM-dd,程序将其转换为:xxxx年xx月xx日的格式输出到控制台。

public class Test2 {
    public static void main(String[] args) throws ParseException {
        Date date = new Date();
        SimpleDateFormat dateformat = new SimpleDateFormat("yyyy年MM月dd日 HH时mm分ss秒");
        String time = dateformat.format(date);
        System.out.println(time);//2024年08月30日 20时16分46秒

        Scanner sc = new Scanner(System.in);
        System.out.print("请输入一个日期(格式:yyyy-mm-dd):");
        String now = sc.next();//2025-06-09
        LocalDate parse = LocalDate.parse(now, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
        String parse1 = parse.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
        System.out.println(parse1);
    }

}

2.4【LocalDateTime类】

请编写程序,使用LocalDateTime类获取时间对象,并分别获取年、月、日、小时、分、秒,并将它们打印到控制台。

请编程,计算并打印“1949年10月1日”那天是星期几?

代码计算500天后是几年几月几日。

public class Test3 {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println("年:" + now.getYear());
        System.out.println("月:" + now.getMonth());
        System.out.println("日:" + now.getDayOfMonth());
        System.out.println("小时:" + now.getHour());
        System.out.println("分:" + now.getMinute());
        System.out.println("秒:" + now.getSecond());

        LocalDateTime date = now.withYear(1949).withMonth(10).withDayOfMonth(1);
        System.out.println(date.getDayOfWeek());
        LocalDateTime afterDate = date.plusDays(500);
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy年MM月dd日");
        System.out.println(afterDate.format(formatter));
    }
}

 2.5 【Arrays】 请按照要求完成下面两个排序案例

//Comparator
//请以Comparable方式实现按照身高倒序排序
//请以Comparator方式实现按照年龄正序排序
public class Test5 {
    public static void main(String[] args) {
        //1. 定义学生数组对象,保存四个学生进行测试
        Teacher[] teachers = new Teacher[4];
        teachers[0] = new Teacher("蜘蛛精", 169.5, 23);
        teachers[1] = new Teacher("紫霞", 163.8, 26);
        teachers[2] = new Teacher("紫霞", 163.8, 26);
        teachers[3] = new Teacher("至尊宝", 167.5, 24);

        //2. 指定排序的规则
        Arrays.sort(teachers, new Comparator<Teacher>() {
            @Override
            请以Comparable方式实现按照身高倒序排序
            public int compare(Teacher o1, Teacher o2) {
                return o1.getHeight() > o2.getHeight() ? -1 : 1;
            }
            //请以Comparator方式实现按照年龄正序排序
//            public int compare(Teacher o1, Teacher o2) {
//                return o1.getAge() > o2.getAge() ? 1 : -1;
//            }
        });
        //3. 打印结果
        System.out.println(Arrays.toString(teachers));
    }
}

class Teacher{
    private String name;
    private double height;
    private int age;

    public Teacher(String name, double height, int age) {
        this.name = name;
        this.height = height;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getHeight() {
        return height;
    }

    public void setHeight(double height) {
        this.height = height;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Teacher{" +
                "name='" + name + '\'' +
                ", height=" + height +
                ", age=" + age +
                '}';
    }
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值