Explorar el Código

update: 更新Demo01方法练习完整实现,新增Demo02数组作为方法参数,同步更新20260717笔记文档

WanJL hace 2 días
padre
commit
e03ef559fe

+ 32 - 12
授课代码/c260717/src/Demo01.java

@@ -65,22 +65,42 @@ public class Demo01 {
         //直接通过  方法名()   调用方法
         //add();      //调用没有参数的方法
         //调用有参数的方法
-        add(15,7);
-        add(8,25);
-        add(7,76);
-        add(9,23);
-
-    }
-        /*
-            声明一个计算2个整数相减的方法,并调用。定义参数a和b -----没有返回值、有参数的方法
-            声明一个循环输出1~100的整数的方法,并调用---没有返回值、没有参数的方法
-            声明一个获取三个变量中最大值的方法,调用方法,传入3个参数,会输出最大值
+        //add(15,7);
+        sub(15,7);
+        printFor();
+        getMax(7,8,9);
 
 
-         */
-
+    }
+    /**
+     * 声明一个计算2个整数相减的方法,并调用。定义参数a和b -----没有返回值、有参数的方法
+     * @param a 被减数
+     * @param b 减数
+     */
+    public static void sub(int a,int b){
+        System.out.println(a-b);
+    }
 
+    /**
+     * 声明一个循环输出1~100的整数的方法,并调用---没有返回值、没有参数的方法
+     */
+    public static void printFor(){
+        for (int i = 1; i <=100; i++) {
+            System.out.print(i+" ");
+        }
+        System.out.println();
+    }
 
+    /**
+     * 声明一个获取三个变量中最大值的方法,调用方法,传入3个参数,会输出最大值
+     * @param a
+     * @param b
+     * @param c
+     */
+    public static void getMax(int a,int b,int c){
+        int max=(a>b?a:b)>c?(a>b?a:b):c;
+        System.out.println(max);
+    }
 
 
 

+ 33 - 0
授课代码/c260717/src/Demo02.java

@@ -0,0 +1,33 @@
+import java.util.Arrays;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Demo02
+ * @description
+ * @create 2026/7/17
+ */
+public class Demo02 {
+    /*
+        1、声明一个数组冒泡排序的方法, bubbleSort(int[] arr) ,在方法中完成数组的排序,并且输出排序后端数组元素
+            |- 方法声明  public static void bubbleSort(int[] arr){}
+            |- 创建一个数组,调用方法时候,把数组名传入
+                int[] myScore={80,85,77,63,84,95,86,88};
+                调用方法:
+                    bubbleSort(myScore);
+     */
+    public static void bubbleSort(int[] arr){
+        System.out.println("排序前:"+ Arrays.toString(arr));
+        //排序代码
+        System.out.println("排序后:"+ Arrays.toString(arr));
+    }
+    // 选择排序
+    public static void selectionSort(int[] arr){}
+    //插入排序
+    public static void insertionSort(int[] arr){}
+
+    public static void main(String[] args) {
+        int[] myScore={80,85,77,63,84,95,86,88};
+        bubbleSort(myScore);
+    }
+}

+ 123 - 7
课堂笔记/01-JavaSE基础-笔记/20260717-笔记.md

@@ -82,16 +82,130 @@ public static void main(String[] args) {
 }
 ```
 
-### 4. 方法练习
+### 4. 方法练习(完整实现)
 
-> 以下为课堂练习,使用上述方法知识实现:
+> 以下为课堂练习,使用上述方法知识完成实现:
 
-1. 声明一个计算 2 个整数相减的方法,定义参数 a 和 b —— 没有返回值、有参数的方法
-2. 声明一个循环输出 1~100 的整数的方法并调用 —— 没有返回值、没有参数的方法
-3. 声明一个获取三个变量中最大值的方法,调用方法传入 3 个参数,会输出最大值
+#### 练习 1:两数相减
+
+```java
+/**
+ * 声明一个计算2个整数相减的方法
+ * @param a 被减数
+ * @param b 减数
+ */
+public static void sub(int a, int b) {
+    System.out.println(a - b);
+}
+```
+
+调用方式:`sub(15, 7);` // 输出 8
+
+#### 练习 2:循环输出 1~100
+
+```java
+/**
+ * 声明一个循环输出1~100的整数的方法
+ */
+public static void printFor() {
+    for (int i = 1; i <= 100; i++) {
+        System.out.print(i + " ");
+    }
+    System.out.println();
+}
+```
+
+#### 练习 3:三个数中取最大值
+
+```java
+/**
+ * 声明一个获取三个变量中最大值的方法
+ * @param a
+ * @param b
+ * @param c
+ */
+public static void getMax(int a, int b, int c) {
+    int max = (a > b ? a : b) > c ? (a > b ? a : b) : c;
+    System.out.println(max);
+}
+```
+
+> **知识点**:使用**三元运算符**嵌套实现三个数的最大值比较。
+
+#### main 方法调用
+
+```java
+public static void main(String[] args) {
+    sub(15, 7);       // 两数相减
+    printFor();       // 输出1~100
+    getMax(7, 8, 9);  // 三个数取最大值
+}
+```
+
+---
+
+## 二、方法进阶 —— 数组作为方法参数(Demo02)
+
+### 1. 概念
+
+方法不仅可以接收 `int`、`double` 等基本类型参数,还可以接收**数组类型**的参数。
+
+```java
+public static void 方法名(数据类型[] 数组名) {
+    // 方法体
+}
+```
+
+### 2. 示例:将排序算法封装为方法
+
+```java
+import java.util.Arrays;
+
+public class Demo02 {
+
+    // 冒泡排序方法
+    public static void bubbleSort(int[] arr) {
+        System.out.println("排序前:" + Arrays.toString(arr));
+        // 排序代码(略)
+        System.out.println("排序后:" + Arrays.toString(arr));
+    }
+
+    // 选择排序方法(占位)
+    public static void selectionSort(int[] arr) {}
+
+    // 插入排序方法(占位)
+    public static void insertionSort(int[] arr) {}
+
+    public static void main(String[] args) {
+        int[] myScore = {80, 85, 77, 63, 84, 95, 86, 88};
+        bubbleSort(myScore);  // 传入数组名
+    }
+}
+```
+
+### 3. 关键点
+
+| 项目 | 说明 |
+|------|------|
+| **参数声明** | `(int[] arr)` — 参数类型为 `int[]` 数组 |
+| **调用方式** | `bubbleSort(myScore);` — 直接传入数组名(不带方括号) |
+| **数组toString** | `Arrays.toString(arr)` — 将数组转换为可读的字符串形式 |
+| **封装意义** | 将排序算法封装到方法中,方便复用和测试 |
+
+### 4. Arrays.toString() 工具方法
+
+`Arrays.toString()` 是 `java.util.Arrays` 类提供的静态方法,用于将数组转换为格式化的字符串:
+
+```java
+int[] arr = {80, 85, 77, 63, 84, 95, 86, 88};
+System.out.println(Arrays.toString(arr));
+// 输出:[80, 85, 77, 63, 84, 95, 86, 88]
+```
 
 ---
 
+## 四、课后作业 —— 三种排序算法复习
+
 ## 二、课后作业 —— 三种排序算法复习
 
 > 本日作业巩固了 7 月 16 日学习的三种排序算法。
@@ -208,7 +322,7 @@ for (int i = 1; i < studentScores.length; i++) {
 
 ---
 
-## 、课后作业 —— Scanner 学生信息采集系统(HomeWork04)
+## 、课后作业 —— Scanner 学生信息采集系统(HomeWork04)
 
 > 使用 `Scanner` 键盘输入,采集学生信息并计算 BMI 指数。
 
@@ -313,11 +427,13 @@ public static void main(String[] args) {
 
 ---
 
-## 、本日总结
+## 、本日总结
 
 | 知识点 | 主要内容 |
 |--------|----------|
 | **Java 方法** | 方法的定义、4 种声明方式、参数传递、方法调用 |
+| **方法练习实现** | `sub` 两数相减、`printFor` 输出1~100、`getMax` 三元运算符求最大值 |
+| **数组作为方法参数** | `bubbleSort(int[] arr)` 将数组传入方法、`Arrays.toString()` 打印数组 |
 | **冒泡排序** | 相邻比较、交换、提前结束优化 |
 | **选择排序** | 每轮选最大值、降序排列 |
 | **插入排序** | 已排序/未排序分区、从后往前插入 |