From 3559f83d073113dfa4e26c1ef5248cea1fd5ca52 Mon Sep 17 00:00:00 2001 From: xcchengguo <2395432766@qq.com> Date: Thu, 15 Dec 2022 07:20:45 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=88=91=E7=9A=84=E7=AC=AC8=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20221214 \346\200\273\347\273\223.md" | 175 ++++++++++++++++++ 1 file changed, 175 insertions(+) create mode 100644 "18 \350\260\242\345\256\270/20221214 \346\200\273\347\273\223.md" diff --git "a/18 \350\260\242\345\256\270/20221214 \346\200\273\347\273\223.md" "b/18 \350\260\242\345\256\270/20221214 \346\200\273\347\273\223.md" new file mode 100644 index 0000000..5aecd4f --- /dev/null +++ "b/18 \350\260\242\345\256\270/20221214 \346\200\273\347\273\223.md" @@ -0,0 +1,175 @@ + + +### + +# 什么是API + +API (Application Programming Interface) :应用程序编程接口 java中的API 指的就是 JDK 中提供的各种功能的 Java类,这些类将底层的实现封装了起来,我们不需要关心这 些类是如何实现的,只需要学习这些类如何使用即可,我们可以通过帮助文档来学习这些API如何 使用。 + + 如何使用API帮助文档 : + +1. 打开帮助文档 + +2. 找到索引选项卡中的输入框 + +3. 在输入框中输入Random + +4. 看类在哪个包下 + +5. 看类的描述 + +6. 看构造方法 + +7. 看成员方法 + + # 1.2 键盘录入字符串 + + ### Scanner类 : + + next() : 遇到了空格, 就不再录入数据了 , 结束标记: 空格, tab键 + + nextLine() : 可以将数据完整的接收过来 , 结束标记: 回车换行符 + + ​ 代码实现 : + + ~~~ + package com.itheima.api; + import java.util.Scanner; + public class Demo1Scanner { + /* + next() : 遇到了空格, 就不再录入数据了 + 结束标记: 空格, tab键 + nextLine() : 可以将数据完整的接收过来 + 结束标记: 回车换行符 + */ + public static void main(String[] args) { + // 1. 创建Scanner对象 + Scanner sc = new Scanner(System.in); + System.out.println("请输入:"); + // 2. 调用nextLine方法接收字符串 + //ctrl + alt + v : 快速生成方法的返回值 + String s = sc.nextLine(); + System.out.println(s); + } + } + package com.itheima.api; + import java.util.Scanner; + public class Demo2Scanner { + /* + nextInt和nextLine方法配合使用的时候, nextLine方法就没有键盘录入的机会了 + 建议: 今后键盘录入数据的时候, 如果是字符串和整数一起接受, 建议使用next方法接受字 + 符串. + */ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入整数:"); + int num = sc.nextInt(); // 10 + 回车换行 + System.out.println("请输入字符串:"); + String s = sc.nextLine(); + System.out.println(num); + System.out.println(s); + } + } + ~~~ + + ### 2.2 String类的构造方法 + + 常用的构造方法 + + ![](D:\Java库\java-base\笔记&作业\img\2022-12-13_114811.png) + + 示例代码: + + ~~~ + package com.itheima.string; + public class Demo2StringConstructor { + /* + String类常见构造方法: + public String() : 创建一个空白字符串对象,不含有任何内容 + public String(char[] chs) : 根据字符数组的内容,来创建字符串对象 + public String(String original) : 根据传入的字符串内容,来创建字符串对象 + String s = “abc”; 直接赋值的方式创建字符串对象,内容就是abc + 注意: + String这个类比较特殊, 打印其对象名的时候, 不会出现内存地址 + 而是该对象所记录的真实内容. + 面向对象-继承, Object类 + */ + public static void main(String[] args) { + // public String() : 创建一个空白字符串对象,不含有任何内容 + String s1 = new String(); + System.out.println(s1); + // public String(char[] chs) : 根据字符数组的内容,来创建字符串对象 + char[] chs = {'a','b','c'}; + String s2 = new String(chs); + System.out.println(s2); + // public String(String original) : 根据传入的字符串内容,来创建字符串对象 + String s3 = new String("123"); + System.out.println(s3); + } + } + ~~~ + + ## 2.6 用户登录案例【应用】 + + ##### 案例需求 : + + 已知用户名和密码,请用程序实现模拟用户登录。总共给三次机会,登录之后,给出相应的提示 + + ##### 实现步骤 : + + 1. 已知用户名和密码,定义两个字符串表示即可 + + 2. 键盘录入要登录的用户名和密码,用 Scanner 实现 + + 3. 拿键盘录入的用户名、密码和已知的用户名、密码进行比较,给出相应的提示。 + + 4. 字符串的内容比较,用equals() 方法实现 + + 5. 用循环实现多次机会,这里的次数明确,采用for循环实现,并在登录成功的时候,使用break结束 循 环 + + 6. 代码实现 : + + ~~~ + package com.itheima.stringmethod; + public class Demo1Equals { + public static void main(String[] args) { + String s1 = "abc"; + String s2 = "ABC"; + String s3 = "abc"; + // equals : 比较字符串内容, 区分大小写 + System.out.println(s1.equals(s2)); + System.out.println(s1.equals(s3)); + // equalsIgnoreCase : 比较字符串内容, 忽略大小写 + System.out.println(s1.equalsIgnoreCase(s2)); + } + } + ~~~ + + + +# 作业 + +``` +public class cls{ + public static void main(String[] args) { + for (int x =1;x<=100;x++){ + if (x*4.05+(100-x)*5.06==445.4){ + System.out.println("第一梯度有:"+x+"立方"+"\n"+"第二梯度有:"+(100-x)+"立方"); + } + } + } +} +``` + +``` +public class cls { + public static void main(String[] args) { + for (int x=9;x>=1;x--){ + for (int y=9;y>=x;y--){ + System.out.print(x+"*"+y+"="+x*y+"\t"); + } + System.out.println(""); + } + } +} +``` \ No newline at end of file -- Gitee From ba4a7f9d8472d848fc77257e8950bf1d8105fcbc Mon Sep 17 00:00:00 2001 From: xcchengguo <2395432766@qq.com> Date: Fri, 16 Dec 2022 13:31:07 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E6=88=91=E7=9A=84=E7=AC=AC9=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../20221216 \346\226\271\346\263\225.md" | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 "18 \350\260\242\345\256\270/20221216 \346\226\271\346\263\225.md" diff --git "a/18 \350\260\242\345\256\270/20221216 \346\226\271\346\263\225.md" "b/18 \350\260\242\345\256\270/20221216 \346\226\271\346\263\225.md" new file mode 100644 index 0000000..2c7f638 --- /dev/null +++ "b/18 \350\260\242\345\256\270/20221216 \346\226\271\346\263\225.md" @@ -0,0 +1,141 @@ + + +# 形参和实参 + +1. 形参:方法定义中的参数 等同于变量定义格式,例如:int number + +2. 实参:方法调用中的参数 等同于使用变量或常量,例如: 10 number + + + +# 1.1 方法的概念 + +##### 方法(method)是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集 + +###### 注意: 方法必须先创建才可以使用,该过程成为方法定义 方法创建后并不是直接可以运行的,需要手动使用后,才执行,该过程成为方法调用 + +## 方法的定义和调用 + +#### 2.1 无参数方法定义和调用 + +定义格式: + +```java +public static void 方法名 ( ) { +// 方法体; +} +``` + +##### 注意: 方法必须先定义,后调用,否则程序将报错 + +### 2.2 方法的调用过程 + + 总结:每个方法在被调用执行的时候,都会进入栈内存,并且拥有自己独立的内存空间,方法内部 代码调用完毕之后,会从栈内存中弹栈消失。 + +### 2.3 方法练习-奇偶数判断 + +需求:判断一个数是奇数还是偶数 + + 代码: + +```java +public class Demo1Method { +3. 带参数方法的定义和调用 +3.1 带参数方法定义和调用 +定义格式: +参数:由数据类型和变量名组成 - 数据类型 变量名 +参数范例:int a +范例: +注意: +/* +带参数方法的定义格式: +public static void 方法名 ( 参数 ) { … … } +public static void 方法名 ( 数据类型 变量名 ) { … … } +带参数方法的调用格式: +方法名 ( 参数 ) ; +方法名 ( 变量名/常量值 ) ; +tips: 参数可以是一个, 也可以是多个. +需求: 判断一个数是奇数还是偶数 +*/ +public static void main(String[] args) { +isEvenNumber(10); +} +public static void isEvenNumber(int num){ +if(num % 2 == 0){ +System.out.println("偶数"); +}else{ +System.out.println("奇数"); +} +} +} +``` + +# 带参数方法的定义和调用 + +### 3.1 带参数方法定义和调用 + +#### 定义格式: + + 参数:由数据类型和变量名组成 - 数据类型 变量名 + + 参数范例:int a + +~~~ +public static void 方法名 (参数1) { +方法体; +} +public static void 方法名 (参数1, 参数2, 参数3...) { +方法体; +} +例: + ublic static void isEvenNumber(int number){ +... +} +public static void getMax(int num1, int num2){ +... +} +注意:方法定义时,参数中的数据类型与变量名都不能缺少,缺少任意一个程序将报错 +方法定义时,多个参数之间使用逗号( ,)分隔 +~~~ + +# 作业 + +```java +import java.util.Scanner; + +public class cls { + + public static void main(String[] args) { + int max = getmax(1, 2); + System.out.println(max); + } + + public static int getmax(int a, int b) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一"); + int c = sc.nextInt(); + a = c; + System.out.println("请输入二"); + int d = sc.nextInt(); + b = d; + return a > b ? a : b; + } +} +``` + +``` +import java.util.Scanner; +public class cls { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入第一个数"); + int x = sc.nextInt(); + System.out.println("请输入第二个数"); + int y = sc.nextInt(); + System.out.println("请输入第三个数"); + int z = sc.nextInt(); + int S = x*x*x+y*y*y+z*z*z; + System.out.println("S的和是"+S); + } +} +``` -- Gitee