Java第二次实验

1. 本章学习总结
本章学习了Java多方面知识:控制台输入推荐用Scanner.nextLine()避免问题,还涉及 IDE 操作与 String.split;身份证排序用Arrays.sort和String.subString,结构化编程且注意输入方法;需了解 StringBuilder 优于+拼接字符串;动态数组第一维预定义、第二维动态定;ArrayList有add等方法,可替代数组,可查JDK文档深入学习。
2. 书面作业
2.1 综合小测验

QQ20250924-192451
2.2 身份证排序

点击查看代码
import java.util.*;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = Integer.parseInt(scanner.nextLine());
        List<String> idCards = new ArrayList<>();
        for (int i = 0; i < n; i++) {
            idCards.add(scanner.nextLine());
        }
        while (true) {
            String command = scanner.nextLine();
            if ("sort1".equals(command)) {
                sort1(idCards);
            } else if ("sort2".equals(command)) {
                sort2(idCards);
            } else {
                System.out.println("exit");
                break;
            }
        }
        
        scanner.close();
    }
    private static void sort1(List<String> idCards) {
        List<String> dateList = new ArrayList<>();
        for (String idCard : idCards) {
            if (idCard.length() >= 14) {
                String year = idCard.substring(6, 10);
                String month = idCard.substring(10, 12);
                String day = idCard.substring(12, 14);
                dateList.add(year + "-" + month + "-" + day);
            }
        }
        Collections.sort(dateList);
        for (String date : dateList) {
            System.out.println(date);
        }
    }
    private static void sort2(List<String> idCards) {
        List<IdCardWithDate> cardList = new ArrayList<>();
        for (String idCard : idCards) {
            if (idCard.length() >= 14) {
                String dateStr = idCard.substring(6, 14);
                cardList.add(new IdCardWithDate(idCard, dateStr));
            }
        }
        Collections.sort(cardList, new Comparator<IdCardWithDate>() {
            public int compare(IdCardWithDate o1, IdCardWithDate o2) {
                return o1.date.compareTo(o2.date);
            }
        });
        for (IdCardWithDate card : cardList) {
            System.out.println(card.idCard);
        }
    }
    static class IdCardWithDate {
        String idCard;
        String date;
        public IdCardWithDate(String idCard, String date) {
            this.idCard = idCard;
            this.date = date;
        }
    }
}
2.3 StringBuilder
点击查看代码
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            int begin = scanner.nextInt();
            int end = scanner.nextInt();
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < n; i++) {
                sb.append(i);
            }
            String result = sb.substring(begin, end);
            System.out.println(result);
        }
        scanner.close();
    }
}
2.4 动态数组
点击查看代码
import java.util.Arrays;
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        while (scanner.hasNextInt()) {
            int n = scanner.nextInt();
            printMultiplicationTable(n);
        }
        scanner.close();
    }
    private static void printMultiplicationTable(int n) {
        String[][] multiplicationTable = new String[n][];
        for (int i = 1; i <= n; i++) {
            multiplicationTable[i-1] = new String[i];
            for (int j = 1; j <= i; j++) {
                multiplicationTable[i-1][j-1] = i + "*" + j + "=" + (i * j);
            }
        }
        for (int i = 1; i <= n; i++) {
            for (int j = 1; j <= i; j++) {
                String item = i + "*" + j + "=" + (i * j);
                
                if (j == 1) {
                    System.out.print(item);
                } else {
                    String prevItem = i + "*" + (j-1) + "=" + (i * (j-1));
                    int totalWidth = 7;
                    int spacesNeeded = totalWidth - prevItem.length();
                    for (int k = 0; k < spacesNeeded; k++) {
                        System.out.print(" ");
                    }
                    System.out.print(item);
                }
            }
            System.out.println();
        }
        System.out.println(Arrays.deepToString(multiplicationTable));
    }
}
**3. PTA实验总结及码云上代码提交记录**

QQ20250924-192805

QQ20250924-193138

QQ20250924-193855
这些实验让我对字符串操作、数组使用和性能优化有了更深入的认识,编程能力也得到了提升。

posted @ 2025-09-24 19:45  arctic1  阅读(7)  评论(0)    收藏  举报