From 480c62fd315457cd9e65316cfeefe330cb178d38 Mon Sep 17 00:00:00 2001 From: JRNitre Date: Mon, 20 May 2024 23:39:57 +0800 Subject: [PATCH] =?UTF-8?q?Java=20=E8=AF=BE=E7=A8=8B=E4=BD=9C=E4=B8=9A=202?= =?UTF-8?q?0240520=20=E5=AE=9E=E9=AA=8C=E5=9B=9B=20=E8=B0=83=E6=95=B4?= =?UTF-8?q?=E4=BA=86=20function=5F02=20=E4=B8=AD=20=E5=86=92=E6=B3=A1?= =?UTF-8?q?=E6=8E=92=E5=BA=8F=20=E9=80=BB=E8=BE=91=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../untitled/file1.txt | 1 + .../untitled/file2.txt | 1 + .../javaAssignment_20240520/function_01.java | 1 - .../javaAssignment_20240520/function_02.java | 47 ++++++++++++------- .../src/javaAssignment_20240520/test.java | 4 +- 5 files changed, 34 insertions(+), 20 deletions(-) create mode 100644 01_Intellij_Java_universal_20240419/untitled/file1.txt create mode 100644 01_Intellij_Java_universal_20240419/untitled/file2.txt diff --git a/01_Intellij_Java_universal_20240419/untitled/file1.txt b/01_Intellij_Java_universal_20240419/untitled/file1.txt new file mode 100644 index 0000000..8ec2ba9 --- /dev/null +++ b/01_Intellij_Java_universal_20240419/untitled/file1.txt @@ -0,0 +1 @@ +文件已被成功创建!又添加了一行文字! \ No newline at end of file diff --git a/01_Intellij_Java_universal_20240419/untitled/file2.txt b/01_Intellij_Java_universal_20240419/untitled/file2.txt new file mode 100644 index 0000000..ea719de --- /dev/null +++ b/01_Intellij_Java_universal_20240419/untitled/file2.txt @@ -0,0 +1 @@ +5236-499-4365-97-6843-2313-5993-7998-1455-9132-4886-8419-9757-997-5574 \ No newline at end of file diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_01.java b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_01.java index abff700..e3b3ef1 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_01.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_01.java @@ -7,7 +7,6 @@ package javaAssignment_20240520; import java.io.*; -import java.io.IOException; public class function_01 { private String fileName; diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_02.java b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_02.java index c0195d9..5c2a8dc 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_02.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/function_02.java @@ -10,8 +10,11 @@ import java.io.*; import java.security.SecureRandom; public class function_02 { + public static final int MaxLength = 15; + // 文件名 private String fileName; - private int[] randomArray = new int[15]; + // 随机数数组 + private int[] randomArray = new int[MaxLength]; // 构造函数, 接收需要创建的文件名 public function_02(String name ){ @@ -24,19 +27,21 @@ public class function_02 { * 生成 15 个随机整数, 并存储到数组中 * * Input: - * min 随机数最小值 - * max 随机数最大值 + * int min 随机数最小值 + * int max 随机数最大值 * Return: int[] 整型数组 * */ public int[] createRandom(int min, int max){ - // 实例化一个 secureRandom 用于生成随机数 + // 通过 SecureRandom 实例化一个 secureRandom 对象用于生成随机数 SecureRandom secureRandom = new SecureRandom(); - for (int i = 0; i < 15; i++){ + for (int i = 0; i < MaxLength; i++){ // 先生成 0 ~ max - min + 1 之间的随机数 // 后将生成结果 + min + // 最后赋值到随机数数组中 randomArray[i] = min + secureRandom.nextInt(max - min + 1); } System.out.println("Create Random Array Done!"); + // 返回生成完毕的随机数组 return randomArray; } @@ -50,7 +55,7 @@ public class function_02 { * */ public boolean createFile(){ try { - // 实例化 file 对象创建文件 + // 通过 File 实例化 file 对象用于创建文件 File file = new File(fileName); // 判断文件是否创建成功 if (file.createNewFile()){ @@ -72,16 +77,16 @@ public class function_02 { * Description: * 向文件写入数据 * - * Input: data 需要写入的数据 - * Return: boolean 是否写入成功 + * Input: String data 需要写入的数据 + * Return: boolean 是否写入成功 * */ public boolean writeToFile(String data){ try { - // 实例化 writer 对象用于写入 + // 通过 BufferedWriter 实例化 writer 对象用于向文件写入 BufferedWriter writer = new BufferedWriter(new FileWriter(this.fileName)); - // 写入数据 + // write 方法写入数据 writer.write(data); - // 写入完毕关闭文件 + // close 方法写入完毕关闭文件 writer.close(); System.out.println("Write File Done!"); return true; @@ -102,14 +107,14 @@ public class function_02 { * */ public int[] readFileToArray(){ try{ - // 实例化 reader 用于读取文件内容 + // 通过 BufferedReader 实例化 reader 对象用于读取文件内容 BufferedReader reader = new BufferedReader(new FileReader(this.fileName)); - // 读取一行的内容 + // 读取文件中一行的内容 String line = reader.readLine(); - // 以 - 为分隔符分割字符串 + // split 方法以 "-" 为分隔符拆分字符串并写入到字符串数组中 String[] strArray = line.split("-"); // 创建整型数组 - int[] intArray = new int[15]; + int[] intArray = new int[MaxLength]; for (int i = 0; i < 15; i++){ // 将字符串数组转换为整型赋值给整型数组 intArray[i] = Integer.parseInt(strArray[i]); @@ -134,8 +139,12 @@ public class function_02 { * Return: int[] 需要排序的数组 * */ public void bubbleSort(int[] array){ - for (int i = 0; !(i >= array.length - 1); i++){ - for (int j = 0; j < array.length - i - 1; j++){ + // TODO 优化冒泡排序算法 + int n = array.length; + + // 冒泡排序 + for (int i = 0; i < n; i++){ + for (int j = 0; j < n - i - 1; j++){ if (array[j] > array[j + 1]) { int temp = array[j]; array[j] = array[j + 1]; @@ -143,9 +152,13 @@ public class function_02 { } } } + System.out.println("Bubble Sort Done!"); + + // 输出排序后的数组 for (int i = 0; i < array.length; i++){ System.out.print(array[i]); + // 打印分隔符 if (i != array.length - 1){ System.out.print("-"); } diff --git a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/test.java b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/test.java index 1680f79..b8f1084 100644 --- a/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/test.java +++ b/01_Intellij_Java_universal_20240419/untitled/src/javaAssignment_20240520/test.java @@ -40,7 +40,7 @@ public class test { * 3. 读取该文件中的数据并升序排序 * * class: function_02.java - * + */ // 实例化第二个实验内容的对象 function_02 f_2 = new function_02("file2.txt"); @@ -72,6 +72,6 @@ public class test { // 调用冒泡排序算法进行排序 f_2.bubbleSort(array_copy); - */ + /**/ } }