From 12f2b215097f37478968a53ba8c9b0d8eb502169 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=88=90=E9=B8=BF?= <3157164764@qq,com> Date: Wed, 14 Dec 2022 18:55:26 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...72\347\241\200\345\272\224\347\224\250.md" | 111 ++++++++++++++++++ ...35\346\255\245\345\272\224\347\224\250.md" | 2 +- 2 files changed, 112 insertions(+), 1 deletion(-) create mode 100644 "16 \346\236\227\346\210\220\351\270\277/20221213 String\347\232\204\345\237\272\347\241\200\345\272\224\347\224\250.md" diff --git "a/16 \346\236\227\346\210\220\351\270\277/20221213 String\347\232\204\345\237\272\347\241\200\345\272\224\347\224\250.md" "b/16 \346\236\227\346\210\220\351\270\277/20221213 String\347\232\204\345\237\272\347\241\200\345\272\224\347\224\250.md" new file mode 100644 index 0000000..c8cba27 --- /dev/null +++ "b/16 \346\236\227\346\210\220\351\270\277/20221213 String\347\232\204\345\237\272\347\241\200\345\272\224\347\224\250.md" @@ -0,0 +1,111 @@ +## String类 + +#### String的构造方法 + +常见的构造方法 + +```java +public String()// 创建一个空白字符串对象,不含有任何内容 + +public String(char[] chs)//根据字符数组的内容,来创建字符串对象 + +public String(String original)// 根据传入的字符串内容,来创建字符串对象 + +String s ="abc";//直接赋值的方式创建字符串对象,内容就是abc + +// 示例代码 + +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); +``` + +#### 创建字符串对象的区别对待 + +**通过构造方法创建** + +通过 new 创建的字符串对象,每一次 new 都会申请一个新的内存空间,虽然内容相同,但是地 + +址值不同 所以直接== 是不成立的 + +**直接赋值方式创建** + +以“”方式给出的字符串,只要字符序列相同(顺序和大小写),无论在程序代码中出现几次,JVM虚 + +拟机 都只会建立一个 String 对象,并在字符串池中维护 + +#### 字符串的比较 + + 比较基本数据类型:比较的是具体的值 + + 比较引用数据类型:比较的是对象(内存)地址值 + +##### 示例代码 + +```java +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)); + } +} +``` + +## 作业 + +1. 第一题 + + ```java + package 作业; + + public class A4 { + public static void main(String[] args) { + //输出反的乘法口诀表 + //外层循环 + for (int i =9; i>=1;i--){ + //里层循环 + for (int a =1; a <=i ; a++){ + System.out.print(a+"*"+i+"="+a*i+"\t"); + } + System.out.println(); + } + } + } + + ``` + +2. 第二题 + + ```java + package 作业; + + public class A5 { + public static void main(String[] args) { + // 已知 购买100立方 总共付款445.4 + // 4.02*(100-x)*5.06==445.4 + for (int i =1;i<=100;i++){ + if (i*4.05+(100-i)*5.06==445.4) + System.out.println("第二档"+i+"立方"+"第三档"+(100-i)+"立方"); + } + } + } + + ``` + + \ No newline at end of file diff --git "a/16 \346\236\227\346\210\220\351\270\277/2022129 \346\225\260\347\273\204\347\232\204\345\210\235\346\255\245\345\272\224\347\224\250.md" "b/16 \346\236\227\346\210\220\351\270\277/2022129 \346\225\260\347\273\204\347\232\204\345\210\235\346\255\245\345\272\224\347\224\250.md" index e26bf7f..3024cd1 100644 --- "a/16 \346\236\227\346\210\220\351\270\277/2022129 \346\225\260\347\273\204\347\232\204\345\210\235\346\255\245\345\272\224\347\224\250.md" +++ "b/16 \346\236\227\346\210\220\351\270\277/2022129 \346\225\260\347\273\204\347\232\204\345\210\235\346\255\245\345\272\224\347\224\250.md" @@ -129,7 +129,7 @@ public class A1 { } sun +=a; } - double zhdf=(sun*1.0-max-zxz)/ man.length;//最后得分 + double zhdf=(sun*1.0-max-zxz)/ (man.length)-2;//最后得分 System.out.print("\n这个数组的和是:"+sun);//\n 是br 换行的意思 System.out.print("\n这个数组的最大值是:"+max); System.out.print("\n这个数组的最小值是:"+zxz); -- Gitee From 78cb505be6c3144a7504840542d63e07612c767d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9E=97=E6=88=90=E9=B8=BF?= <3157164764@qq,com> Date: Thu, 15 Dec 2022 22:03:11 +0800 Subject: [PATCH 2/2] =?UTF-8?q?1215=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...25\347\232\204\344\275\277\347\224\250.md" | 149 ++++++++++++++++++ 1 file changed, 149 insertions(+) create mode 100644 "16 \346\236\227\346\210\220\351\270\277/20221215 java\346\226\260\346\226\271\346\263\225\347\232\204\344\275\277\347\224\250.md" diff --git "a/16 \346\236\227\346\210\220\351\270\277/20221215 java\346\226\260\346\226\271\346\263\225\347\232\204\344\275\277\347\224\250.md" "b/16 \346\236\227\346\210\220\351\270\277/20221215 java\346\226\260\346\226\271\346\263\225\347\232\204\344\275\277\347\224\250.md" new file mode 100644 index 0000000..e26b1bd --- /dev/null +++ "b/16 \346\236\227\346\210\220\351\270\277/20221215 java\346\226\260\346\226\271\346\263\225\347\232\204\344\275\277\347\224\250.md" @@ -0,0 +1,149 @@ +## 方法的概念 + +方法(method)是将具有独立功能的代码块组织成为一个整体,使其具有特殊功能的代码集 + +注意: + +方法必须先创建才可以使用,该过程成为方法定义 + +方法创建后并不是直接可以运行的,需要手动使用后,才执行,该过程成为方法调用 + +#### 无参数方法定义和调用 + +```java +public static void method ( ) { //变量名字可以改 + // 方法体; +} +``` + +#### 带参数方法定义和调用 + +```java +public static void isEvenNumber(int number){ + ... +} +public static void getMax(int num1, int num2){ + ... +} +``` + +##### 形参和实参 + +形参:方法定义中的参数 + +等同于变量定义格式,例如:int number + +实参:方法调用中的参数 + +等同于使用变量或常量,例如: 10 number + +#### 带返回值方法定义和调用 + +```java +public static boolean isEvenNumber( int number ) { + return true ; +} +public static int getMax( int a, int b ) { + return 100 ; +} +``` + +方法的返回值通常会使用变量接收,否则该返回值将无意义 + +##### 带返回值方法的练习求两个数的最大值 + +需求:设计一个方法可以获取两个数的较大值,数据来自于参数 + +思路: + +定义一个方法,声明两个形参接收计算的数值,求出结果并返回 + +使用 if 语句 得出 a 和 b 之间的最大值,根据情况return具体结果 + +在main()方法中调用定义好的方法并使用 【 + +变量保存 】 + +```java + 需求:设计一个方法可以获取两个数的较大值,数据来自于参数 + 1. 定义一个方法,声明两个形参接收计算的数值,求出结果并返回 + 2. 使用 if 语句 得出 a 和 b 之间的最大值,根据情况return具体结果 + 3. 在main()方法中调用定义好的方法并使用 【 变量保存 】 + */ + public static void main(String[] args) { + // 3. 在main()方法中调用定义好的方法并使用 【 变量保存 】 + System.out.println(getMax(10,20)); // 输出调用 + int result = getMax(10,20); + System.out.println(result); + for(int i = 1; i <= result; i++){ + System.out.println("HelloWorld"); + } + } + // 方法可以获取两个数的较大值 + public static int getMax(int a, int b){ + if(a > b){ + return a; + }else{ + return b; + } + } +} +``` + +## 作业 + +1. 第一题 + + ```java + package 作业; + + import java.util.Scanner; + + public class A6 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个函数"); + int a = sc.nextInt(); + System.out.println("请输入另一个函数"); + int b = sc.nextInt(); + System.out.println("最大值是"+bdzdz(a,b)); + } + public static int bdzdz(int a,int b){ + return a>b?a:b; + } + + } + + ``` + + + +2. 第二题 + + ```java + package 作业; + + import java.util.Scanner; + + public class A7 { + 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(); + System.out.println(lf(x)+" "+lf(y)+" "+lf(z)+"="+jsjg(x,y,z)); + } + public static int lf(int a){ + return a*a*a; + } + public static int jsjg(int a,int b,int c){ + return a*a*a+b*b*b+c*c*c; + } + } + + ``` + + \ No newline at end of file -- Gitee