无障碍浏览
无障碍浏览

【2023级】智能医学工程专业-计算机程序设计-第03章-数据类型

作者: 阮晓龙 发布时间: 2024/9/4 7:28:55

基本信息:

  • 章节名称:智能医学工程专业-计算机程序设计-第03章-数据类型

  • 授课教师:信息技术学院互联网技术教学团队(与河南方和信息科技有限公司合作建设)

  • 完成时间:2024年09月

  • 适用年级:2023级

  • 适用专业:智能医学工程


文档内容:


学习资源:

  • 代码:eg3.1

public class PrimitiveTypesDemo {
    public static void main(String[] args) {
        byte age = 18;
        short year = 2024;
        int population = 780000000;
        long distance = 150000000000L;
        float pi = 3.14f;
        double e = 2.71828;
        char grade = 'A';
        boolean isJavaFun = true;

        System.out.println("Age: " + age);
        System.out.println("Year: " + year);
        System.out.println("Population: " + population);
        System.out.println("Distance: " + distance);
        System.out.println("Pi: " + pi);
        System.out.println("Euler's Number: " + e);
        System.out.println("Grade: " + grade);
        System.out.println("Is Java Fun: " + isJavaFun);
    }
}


  • 代码:eg3.2

import java.util.*;

public class BuiltInTypesDemo {
    public static void main(String[] args) {
        String message = "Hello, Java!";
        int[] numbers = {1, 2, 3, 4, 5};

        System.out.println("Message: " + message);
        System.out.print("Numbers: ");
        for (int number : numbers) {
            System.out.print(number + " ");
        }

        System.out.println();

        List<Integer> list = new ArrayList<>();
        list.add(1);
        list.add(2);
        list.add(3);
        list.add(4);
        list.add(5);
        for (Integer number : list) {
            System.out.println(">>>number = " + number);
        }
    }
}


  • 代码:eg3.3.1

public class StringDemo {
    public static void main(String[] args) {
        String greeting = "Hello";
        String name = "World";
        String message = greeting + ", " + name + "!";
        
        System.out.println("Message: " + message);
        System.out.println("Uppercase: " + message.toUpperCase());
        System.out.println("Substring: " + message.substring(7));

        //换一种写法
        StringBuffer sb = new StringBuffer();
        sb.append(greeting);
        sb.append(", ");
        sb.append(name);
        sb.append("!");

        System.out.println(">>>Substring: " + sb.toString());
    }
}


  • 代码:eg3.3.2

public class StringBufferDemo {
    public static void main(String[] args) {
        StringBuffer sb = new StringBuffer("Hello");
        sb.append(" World");
        sb.insert(5, ",");
        sb.replace(6, 11, "Java");

        System.out.println(">>>StringBuffer: " + sb.toString());
    }
}


  • 代码:eg3.4

public class WrapperClassDemo {
    public static void main(String[] args) {
        int num = 100;
        Integer numObj = Integer.valueOf(num); // 将 int 转换为 Integer 对象

        System.out.println("Primitive int: " + num);
        System.out.println("Wrapper Integer: " + numObj);

        // 自动装箱和拆箱
        Integer autoBoxed = num; // 自动装箱
        int autoUnboxed = autoBoxed; // 自动拆箱

        System.out.println("Auto-boxed Integer: " + autoBoxed);
        System.out.println("Auto-unboxed int: " + autoUnboxed);
    }
}


  • 代码:eg3.5

import java.math.BigInteger;

public class BigIntegerDemo {
    public static void main(String[] args) {
        BigInteger bigNum1 = new BigInteger("123456789012345678901234567890");
        BigInteger bigNum2 = new BigInteger("987654321098765432109876543210");

        BigInteger sum = bigNum1.add(bigNum2);
        BigInteger product = bigNum1.multiply(bigNum2);

        System.out.println("Sum: " + sum);
        System.out.println("Product: " + product);
    }
}


  • 代码:eg3.6.1

import java.util.Date;
import java.text.SimpleDateFormat;

public class DateDemo {
    public static void main(String[] args) {
        Date now = new Date();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        System.out.println("Current Date: " + sdf.format(now));
    }
}


  • 代码:eg3.6.2

import java.util.Calendar;

public class CalendarDemo {
    public static void main(String[] args) {
        Calendar cal = Calendar.getInstance();
        cal.add(Calendar.DAY_OF_MONTH, 5); // 加5天

        System.out.println("Date after 5 days: " + cal.getTime());
    }
}


  • 代码:eg3.6.3

//Java 8引入了全新的日期和时间API,这是因为旧版的java.util.Date和java.util.Calendar不够灵活和易用。以下是一些使用Java 8日期/时间特性的示例:

//获取当前日期和时间:
import java.time.LocalDateTime;
public class Main {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        System.out.println(">>>当前日期和时间:" + now);
    }
}



//解析和格式化日期:
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
public class Main {
    public static void main(String[] args) {
        LocalDateTime now = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        
        // 格式化日期
        String formattedDate = now.format(formatter);
        System.out.println(">>>格式化后的日期:" + formattedDate);
    }
}


//计算日期时间的差异:
import java.time.LocalDateTime;
import java.time.temporal.ChronoUnit;
public class Main {
    public static void main(String[] args) {
        LocalDateTime dateTime1 = LocalDateTime.of(2023, 1, 1, 10, 30);
        LocalDateTime dateTime2 = LocalDateTime.now();
        
        long daysDiff = ChronoUnit.DAYS.between(dateTime1, dateTime2);
        System.out.println(">>>两个日期之间相差的天数:" + daysDiff);
    }
}


//使用java.time.Duration计算时间差:
import java.time.Duration;
import java.time.LocalTime;
public class Main {
    public static void main(String[] args) {
        LocalTime time1 = LocalTime.of(10, 30);
        LocalTime time2 = LocalTime.now();
        
        Duration duration = Duration.between(time1, time2);
        long secondsDiff = duration.getSeconds();
        System.out.println(">>>两个时间之间相差的秒数:" + secondsDiff);
    }
}


//使用java.time.Period计算日期差:
import java.time.LocalDate;
import java.time.Period;
public class Main {
    public static void main(String[] args) {
        LocalDate date1 = LocalDate.of(2023, 1, 1);
        LocalDate date2 = LocalDate.now();
        
        Period period = Period.between(date1, date2);
        int yearsDiff = period.getYears();
        int monthsDiff = period.getMonths();
        int daysDiff = period.getDays();
        System.out.println(">>>两个日期之间的差异:\n年份:" + yearsDiff + "\n月份:" + monthsDiff + "\n天数:" + daysDiff);
    }
}




软件资源: