diff --git "a/01 \350\213\217\346\270\205\345\215\216/20221213 API\345\222\214String.md" "b/01 \350\213\217\346\270\205\345\215\216/20221213 API\345\222\214String.md" new file mode 100644 index 0000000000000000000000000000000000000000..43049480b3bc19bd401d0a01120d4ef95e550515 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20221213 API\345\222\214String.md" @@ -0,0 +1,102 @@ +# API和String + +#### 2022.12.13 + +### 第三种遍历方法 + +```java +public class a1 { + public static void main(String[] args) { + int []score={15,13,46,85,24}; + System.out.println(Arrays.toString((score))); + } +} +``` + +### API + +API (Application Programming Interface) :应用程序编程接口 + +#### 键盘录入字符串 + +Scanner类 : + +next() : 遇到了空格, 就不再录入数据了 , 结束标记: 空格, tab键 + +nextLine() : 可以将数据完整的接收过来 , 结束标记: 回车换行符 + +String类: + +```java +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); + } +} + +``` + +字符串的比较 + +比较基本数据类型:比较的是具体的值 + +比较引用数据类型:比较的是对象(内存)地址值 + +String类 : public boolean equals(String s) 比较两个字符串内容是否相同、区分大小写 + +```java +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 +public class a1 { + public static void main(String[] args) { + for (int i=1;i<=100;i++){ + if (i*4.05+(100-i)*5.06==445.4){ + System.out.println("第二档:"+i+"立方,第三档:"+(100-i)+"立方"); + } + } + } +} +``` + +2.倒序乘法表 + +```java +public class a1 { + public static void main(String[] args) { + for (int i=9;i>=1;i--){ + for (int j=9;j>=i;j--){ + System.out.print(i+"*"+j+"="+i*j+"\t"); + } + System.out.println(); + } + } +} +``` + diff --git "a/01 \350\213\217\346\270\205\345\215\216/20221215 \346\226\271\346\263\225.md" "b/01 \350\213\217\346\270\205\345\215\216/20221215 \346\226\271\346\263\225.md" new file mode 100644 index 0000000000000000000000000000000000000000..7f30e151cb3fef236d6e937bf7025b3115368de3 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20221215 \346\226\271\346\263\225.md" @@ -0,0 +1,164 @@ +# 方法(函数) + +#### 2022.12.15 + +求和: + +```java +public class a1 { + public static void main(String[] args) { //main方法就是一个方法 + qiuHe(50,100);//调用求和的方法 + } + static void qiuHe(int a,int b){ + System.out.println(a+"+"+b+"="+(a+b)); + } +} +// 输出为: 50+100=150 +``` + +游戏设计: + +```java +public class a1 { + public static void main(String[] args) { + //设计一个游戏 有四个方向键 + up(); + } + //定义四个新方法 + public static void up(){ + System.out.println("你按了一下上!"); + } + public static void down(){ + System.out.println("你按了一下下!"); + } + public static void left(){ + System.out.println("你按了一下左!"); + } + public static void right(){ + System.out.println("你按了一下右!"); + } +} +// 输出为: 你按了一下上! +``` + + + +### 方法 + +具有独立功能的代码块。 + +### 格式 + +1. 无返回值无参数 + +```java +public class a1 { + public static void main(String[] args) { + say(); + } + public static void say(){ + System.out.println("今天做核酸"); + } +} + +// 输出为:今天做核酸 +``` + +2.有返回值无参数 + +```java +public class a1 { + public static void main(String[] args) { + int a=getOne(); + System.out.println(a); + } + public static int getOne(){ + return 1; + } +} +// 输出为:1 +``` + +3.无返回值无参数 + +```java +public class a1 { + public static void main(String[] args) { + play("哈哈"); + } + public static void play(String name){ + System.out.println("今天"+name+"做核酸"); + } +} +// 输出为: 今天哈哈做核酸 +``` + +4.有返回值有参数 + +```java +public class a1 { + public static void main(String[] args) { + int sum=getSum(3,6); + System.out.println(sum); + } + public static int getSum(int a,int b){ + return a+b; + } +} +// 输出为: 9 +``` + +### 形参和实参 + +1. 形参:方法定义中的参数 等同于变量定义格式,例如:int number +2. 实参:方法调用中的参数 等同于使用变量或常量,例如: 10 number + +### 作业 + +1.定义一函数,用于求2个数中的较大数,并将其返回,这2个数字在主函数中由用户输入 + +```java +public class c { + 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("大的数为:"+max(a,b)); + } + public static int max(int a,int b) { + return a>b?a:b; + } +} +``` + +2.在主函数中从键盘接收X, Y , Z3个数,编写函数计算这3个数的立方和并返回计算结果:S=X3+Y3+Z3 + +```java +public class c { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入x的值"); + int x = sc.nextInt(); + System.out.println("请输入y的值"); + int y = sc.nextInt(); + System.out.println("请输入z的值"); + int z = sc.nextInt(); + System.out.println("三个数的立方和为" + sum(x) + "+" + sum1(y) + "+" + sum2(z)+ "="+s(x,y,z)); + } + public static int sum(int a) { + return a*a*a; + } + public static int sum1(int b){ + return b*b*b; + } + public static int sum2(int c){ + return c*c*c; + } + public static int s(int a,int b,int c ){ + return a*a*a+b*b*b+c*c*c; + } +} +``` +