Java第二次实验

news/2025/9/24 19:49:38/文章来源:https://www.cnblogs.com/arctic1/p/19109854

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/916162.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

详细介绍:【2025PolarCTF秋季个人赛】WEB方向wp

详细介绍:【2025PolarCTF秋季个人赛】WEB方向wppre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", …

英语_阅读

当然可以,以下是英文与中文交替的逐句翻译:Six-year-old Brooke Neitzel wanted a dollhouse. 六岁的布鲁克奈策尔想要一个娃娃屋。 So she ordered one just by telling the familys smart speaker what she wanted…

Nuget安装以及西门子PLC通信

安装S7NetPlus库 ​添加命名空间 ,using S7.Net​创建一个西门子PLC通信对象——成员变量实例化 ,this:表示的是当前对象建立简介 ,对象名.方法名 , this.siemens.Open()​读取变量 , 装箱的一个过程 ,解析变量 ,…

wordpress捐赠按钮如何做网站导航栏的seo优化

2.1.9 调度算法 知识总览 学习各种调度算法的思路 算法思想算法规则这种调度算法是用于作业调度还是进程调度&#xff1f;抢占式或是非抢占式优点和缺点是否会导致饥饿&#xff08;某进程/作业长期得不到服务&#xff09; 2.1.9.1 先来先服务 知识点说明英文名FCFS&#xff0…

网站怎么做成二维码做网站找顺的

二叉树垂直遍历题目描述输入输出示例输入实例输出DFSBFS更简单的方法二叉树垂直遍历题目描述对于一个二叉树&#xff0c;输出它的垂直遍历结果&#xff1b;对于同一列的节点&#xff0c;按照从左向右&#xff0c;从上向下的顺序排列。例如&#xff0c;对于以下二叉树&#xff1…

与狗狗做网站互联网广告价格

创建工程&#xff1a; 2.1.程序的耦合 耦合&#xff1a;耦合指的就是对象之间的依赖关系。对象之间的耦合越高&#xff0c;维护成本越高。 案例&#xff1a;没有引入IOC容器时系统的Web层、业务层、持久层存在耦合 /*** 持久层实现类*/ public class UserDaoImpl implements U…

每日反思(2025_09_24)

今天跟着左神视频学些了快速排序以及逆序对和荷兰国旗问题,了解了双指针三指针递归解决问题,并解决了leetcode第75题颜色分类,正在解决力扣LCR170题。 明天继续跟视频学习,并写涉及到的力扣题,晚上总结这三天的学…

普通用户之间免密互信时因权限问题致使配置失败

[student@master ~]$ ssh-copy-id user1@node1 /usr/bin/ssh-copy-id: INFO: Source of key(s) to be installed: "/home/student/.ssh/id_rsa.pub" /usr/bin/ssh-copy-id: INFO: attempting to log in with…

安装Flask库

安装Flask库 1、下载安装python官网下载 python-3.9.10-amd64.exe 安装,勾选Add Python 3.9 to PATH 2、验证安装cmd输入:python --version cmd输入:pip --version 升级pip:python -m pip install --upgrade pip …

《新概念英语》在线朗读,单句点读,随时随地在线学习。

新概念英语-全四册 New Concept English 在线课文点读 在线朗读,单句点读,随时随地在线学习。📕 第一册:《First Things First》 目标:打基础,日常交流入门内容概述:共144课,都是非常短的小对话和故事。 涉及…

深入解析:[p2p-Magnet] docs | HTTP API与Web界面 | 搜索查询引擎

pre { white-space: pre !important; word-wrap: normal !important; overflow-x: auto !important; display: block !important; font-family: "Consolas", "Monaco", "Courier New", …

南京网站制作学校怎么注册一个网站

戳蓝字“CSDN云计算”关注我们哦&#xff01; 文 | 阿晶、丹丹、王银发于上海华为HC大会现场出品 | CSDN云计算&#xff08;ID&#xff1a;CSDNcloud&#xff09; 科技的不断发展正逐步加速智能世界的到来。一直&#xff0c;华为致力于提供经济且充裕的算力&#xff0c;力图像使…

为什么用MyEclipse做网站ps如何做音乐网站

观察者模式是一种设计模式&#xff0c;其中一个对象&#xff08;称为主题&#xff09;维护一组依赖于它的对象&#xff08;称为观察者&#xff09;&#xff0c;当主题的状态发生变化时&#xff0c;它会通知所有观察者。这种模式常用于实现分布式事件处理系统。 下面是一个简单…

如何做分类网站信息营销网站设计的内容有哪些

学习网络开发过程中不想“污染”macOS&#xff0c;考虑到之后部署网络应用主要是与linux打交道&#xff0c;所以安装了 ubuntu 虚拟机以满足短期的知识学习需求。十里安装了 ubuntu 虚拟机&#xff0c;一般就是在 mac 中 ssh 连接 ubuntu 虚拟机在终端下进行操作学习&#xff0…

P10004 [集训队互测 2023] Permutation Counting 2

把排列写成一条路径 \(p_1\to p_2\to\cdots\to p_n\)。那么 \([p_i<p_{i+1}]\) 就是第 \(i\) 步往右走,\([p^{-1}_i<p^{-1}_{i+1}]\) 是 \(i\) 要先于 \(i+1\) 访问。 如果我们已知了 \(p^{-1}_{i}<p^{-1}_{…

java数组拷贝主要有四种方法,浅拷贝

java数组拷贝主要有四种方法,浅拷贝java数组拷贝主要有四种方法,浅拷贝 在Java中,数组拷贝可以通过多种方式实现,每种方式有其特定的用途和性能特点。下面列举四种常见的方法: 1. 使用System.arraycopy() System.…

毕赤酵母细胞工厂升级:CRISPR 技术破局传统局限,解锁多基因代谢工程新可能

在合成生物学与代谢工程的推动下,微生物细胞工厂已成为 bulk 化学品、高价值天然产物及重组蛋白的核心生产平台 —— 其温和的反应条件、高特异性的催化能力,可大幅降低传统化学合成的污染与能耗。在众多微生物宿主中…

日总结 7

今天上课老师让我们讲我们为大作业准备的ppt,详细内容就是介绍自己的产品,每个队3分钟时间,所以ppt的量要少而精,跟我们要介绍产品时一样,要在有限的时间内介绍出自己产品的核心和亮点,同时ppt的制作也有讲究,不…

React学习教程,从入门到精通,React Router 语法知识点及使用手段详解(28)

React学习教程,从入门到精通,React Router 语法知识点及使用手段详解(28)2025-09-24 19:33 tlnshuju 阅读(0) 评论(0) 收藏 举报pre { white-space: pre !important; word-wrap: normal !important; overflow-x…

网站制作网站设计沈阳关键字优化公司

目录 一.format 函数简介 1.format 函数不设置下标2.format 函数设置下标 二.format 函数实战三.猜你喜欢 零基础 Python 学习路线推荐 : Python 学习目录 >> Python 基础入门 一.format 函数简介 format 函数主要是用来构造字符串&#xff0c;基本语法是通过 {} 符号操…