diff --git "a/44 \345\255\224\345\207\257\350\276\211/2022-.txt" "b/44 \345\255\224\345\207\257\350\276\211/2022-.txt" new file mode 100644 index 0000000000000000000000000000000000000000..e69de29bb2d1d6434b8b29ae775ad8c2e48c5391 diff --git "a/44 \345\255\224\345\207\257\350\276\211/2022-12-6\347\254\254\345\233\233\346\254\241\347\254\224\350\256\260.md" "b/44 \345\255\224\345\207\257\350\276\211/2022-12-6\347\254\254\345\233\233\346\254\241\347\254\224\350\256\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..e077997ca2ec98781df44f8d11d6253db42f41ac --- /dev/null +++ "b/44 \345\255\224\345\207\257\350\276\211/2022-12-6\347\254\254\345\233\233\346\254\241\347\254\224\350\256\260.md" @@ -0,0 +1,49 @@ +# 笔记: + +### 1.while循环: + +~~~ Java +while(条件){ + 循环体; +} +~~~ + +(循环体内要有改变条件的机会) + +解释来说就是先判断条件是否满足,是则进入循环,否则进行后续语句。 + +### 2.do...while循环: + +~~~ java +do +{ + <循环体语句>; +}while(循环条件); +~~~ + + + + 与while循环不同的是do-while循环是先进入循环,后判断条件 + +使用do-while循环进行计算时最好先保存原始的值,后面可能会有用 + +### 3.在不同的场景使用不同的循环: + +1.如果循环必须执行一次,用do-while + +2.其他情况用while + +### 4.死循环: + +~~~ Java +for死循环格式 : +for(;;){ +} +while死循环格式 : +while(true){ +} +do..while死循环格式 : +do{ +}while(true); +~~~ + diff --git "a/44 \345\255\224\345\207\257\350\276\211/2022-12-9\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" "b/44 \345\255\224\345\207\257\350\276\211/2022-12-9\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..eb80ba6ea8af7c88e89f37348a662a93fb680b77 --- /dev/null +++ "b/44 \345\255\224\345\207\257\350\276\211/2022-12-9\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,652 @@ +```java + +import java.util.Random; +import java.util.Scanner; + +public class fgn { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + glo:while (true) { + System.out.println("请输入一个数"); + int sj = scanner.nextInt(); + Random random =new Random(); + int num = random.nextInt(100); + if (sj >= num) { + System.out.println("你猜对了"); + break; + } else if (sj >num) { + System.out.println("你猜大了"); + } else{ + System.out.println("你猜小了"); + } + } + } + } +``` + +```java +public class fgn { + public static void main(String[] args) { + int [] a = {2,4,6,8,10,12,14,16,18,20}; + int max = a[0],min = a[0]; + for(int c =1;c< a.length;c++){ + if(max < a[c]){ + max = a[c]; + } else if(min > a[c]){ + min = a[c]; + } + } + System.out.println("最大值为:"+max); + System.out.println("最小值为:"+min); + } + } +``` + +# Java 方法的应用 + +## 1.方法概述 + +“方法”一次来源与生活,反应计算机编程中,指的是某个问题的处理方式,例如main方法是解决所有问题的主干道,程序总是从main方法开始执行 + +### 2.方法定义 + +在掌握方法的定义前,应该明白在什么情况下需要定义 + +```java +import java.util.Random; +import java.util.Scanner; + +public class fgn { + public static void main(String[] args) { + Scanner scanner = new Scanner(System.in); + glo:while (true) { + System.out.println("请输入一个数"); + int sj = scanner.nextInt(); + Random random =new Random(); + int num = random.nextInt(100); + if (sj >= num) { + System.out.println("你猜对了"); + break; + } else if (sj >num) { + System.out.println("你猜大了"); + } else{ + System.out.println("你猜小了"); + } + } + } + } +``` + +```java +public class fgn { + public static void main(String[] args) { + int [] a = {2,4,6,8,10,12,14,16,18,20}; + int max = a[0],min = a[0]; + for(int c =1;c< a.length;c++){ + if(max < a[c]){ + max = a[c]; + } else if(min > a[c]){ + min = a[c]; + } + } + System.out.println("最大值为:"+max); + System.out.println("最小值为:"+min); + } + } +``` + +方法,一般来说,如果一个小功能块比较完整,可以重复利用,方法在被使用之前需要被定义,那么如何定义方法呢? + +#### 例 + +```java +//main 方法定义 +public class len{ + public static void main(String [] args){ + .... + } +} +``` + +对比main方法的定义,可以看到,一个正确的方法定义,包含: + +1.修饰符(adjunct type) + +2.返回类型(return type) + +3.方法名(method name) + +4.参数(arg1、arg2...) + +5.方法体 + +adjunct type:说明方法的修饰符,如main方法的public、static + +return type:说明方法执行完成后返回值的变量类型 + +method name:说明方法的名称,方法名的写法和我变量的书写格式类似 + +圆括号内的列表表示参数,参数列表描述方法的参数个数和个参数的类型,参数可以有多个 + +,也可以没有参数 + +大括号内是方法体,是完成功能的代码 + +所有方法的位置都是并列的,与main方法一样,在类的大括号内部,注意方法内不能再定义方法 + +##### 例 + +```java +public class lean { + public static void sayHello() { + System.out.println("hello"); + + } + + public static void main(String[] args) { + for(int a =1; a<=9; a++){ + System.out.println("第" +a+"次调用"); + sayHello(); + } + } +} +``` + +#### 3.方法的返回值 + +方法可以完成一定的功能,也可以返回一定的结果,例如某个方法的功能是求出指定的两个数的乘积,则这个方法可以采用两种方法来算出结果,第一种是自身输出结果,第二种则是把结果返回给调用者,方法若要是返回结果,需要使用return 语句 + +找出100以内能被10整除的最大数 + +```java +import javax.imageio.stream.ImageInputStream; + +public class lean { + public static void forlen() { + int s =100; + for(s=100;s>=0;s--){ + if(0 ==s%10){ + continue; + } + } + System.out.println(s); + + } + + public static void main(String[] args) { + forlen(); + } +} +``` + +#### 4.有返回值的方法 + +```java +public class lean { + public static int getNum(){ + int a =100; + for(;a>0;a--){ + if(0 == a%10){ + break; + } + } + return a; + + } + public static void main(String[] args) { + int s =getNum(); + System.out.println(s++); + System.out.println(s); + + } + +} +``` + +#### 5.数组类型的返回值 + +```java +public class lean { + public static void main(String []args) { + float a[] = {9.5f, 12, -52, 25}; + float[] max_min_ave; + float b[] = max_min_ave(a); + for (int s = 0; s < b.length; s++) + System.out.println("b[" + s + "]=" + b[s]); + + } + + + private static float[] max_min_ave ( float [] a){ + float jk[] = new float[3]; + float max = a[0], min = a[0], sum = a[0]; + for (int s = 0; s < a.length; s++) { + if (max < a[s]) + max = a[s]; + if (min > a[s]) + min = a[s]; + sum += a[s]; + } + jk[0] = max; + jk[1] = min; + jk[2] = sum / a.length; + return jk; + } + + } +``` + +不仅一维数组可任意作为方法的返回值,多维数组也可以 + +```java +public class lean { + public static int [][] ewlow(){ + int [][] a = new int[100][100]; + for (int s =1;s<100;s++) + for (int l =1;l<100;l++) + a[s][l] = s * l; + return a; + } + + public static void main(String[] args) { + int [][]v; + v = ewlow(); + for(int s =1;s<100;s++){ + for(int l =1;l<100;l++) + System.out.println(v[s][l] + ""); + System.out.println(); + } + } +} + +``` + +#### 6.方法的参数 + +参数指要传递的初始条件,例如大印资料,那么在调用打印方法时,必须要把打印资料、空白纸张,以及一些如纸张大小、黑白度等初始条件传递给打印方法,这样打印方法才可以工作,打印完成后,返回打印的纸张,这里有两重含义,第一是打印方法需要参数,第二是若要调用打印方法,必须传递对应的参数,传递过去的参数值不一定是一样的,既可以是黑白墨水,也可以是彩色墨水,纸张大小也无所谓,但是类型必须一致 + +##### 1.带参数的方法 + +```java +public class lean { + public static int ewlow(int a, int b){ + int max = a >b ? a:b; + int min = a >b ? b:a; + int s =max; + for(s=0; s >=max;s--) + if( 0 == s % 10) + continue; + + return max; + } + + public static void main(String[] args) { + int f; + f = ewlow(0,100); + System.out.println(f); + f =ewlow(300,100); + System.out.println(f); + } + } + +``` + +将方法定义为public static int ewlow(int a, int b),使得ewlow方法更为灵活,由于题目的要求是求出满足条件的最大数字,所以在方法内,为便于循环,首先判断a和b的大小,然后有大到小,然后有大小进行循环,并反向结果,在main方法内,可以多次调用ewlow方法,实现对任意数字区间的求解 + +方法的参数分为形式参数和实际参数,简称为形参和实参 + +再次强调,形参、实参的个数、类型、顺序必须是匹配的 + +```java +public class lean { + public static boolean ewlow(int a){ + boolean isPaperOk = false; + System.out.println("纸张页数为:" +a); + if(a>0){ + System.out.println("装载"); + isPaperOk = true; + + }else { + System.out.println("缺少纸张"); + isPaperOk = false; + } + return isPaperOk; + + } + + public static boolean aBoolean(System color){ + boolean isboleanOk = false; + System.out.println("墨盒为:" +color); + if("黑白".equals(color) == true || "彩色" .equals(color) ==true){ + System.out.println("装载"); + isboleanOk = true; + + } else { + System.out.println("对不起,不支持此类墨盒"); + isboleanOk = false; + } + return isboleanOk; + } + public static boolean loeze(String color ,int a){ + boolean isup =false; + System.out.println("启动"); + if(isPaperOk(a) && isboleanok (color)){ + System.out.println("装载成功"); + isup =true; + } else{ + System.out.println("装载失败"); + isup =false; + } + System.out.println("关闭"); + return isup; + + } + + private static boolean isboleanok(String color) { + boolean startPrint = false; + return startPrint; + } + + private static boolean startPrint(String 黑白, int i) { + boolean startPrint = false; + return startPrint; + } + + private static boolean isPaperOk(int a) { + boolean isPaperOk = false; + return isPaperOk; + } + + public static void main(String[] args) { + System.out.println("开始测试"); + if(startPrint("黑白" ,10)){ + System.out.println("测试通过"); + } else{ + System.out.println("测试失败"); + } + + } + } +``` + + + +# Java方法的复杂应用 + +## 1.数组作为参数 + +前一单元已经介绍了方法的功能和作用,并介绍了方法的返回值和参数的特点 + +数组也是一种变量,可作为方法的参数,方法定义时把定义时形参类型声明定义为数组,调用方法时,实参使用数组变量就可以了 + +#### 1.判断一个数在数组中是否存在 + +```java +import static jdk.internal.org.jline.utils.Colors.s; + +public class lean { + public static void main(String[] args) { + int[] a ={2,4, 2, 56, 24, 45}; + int findNumber = 45; + int postion = findNumber(a, findNumber); + if (postion < a.length) { + System.out.println("数组包括:" + findNumber); + System.out.println("下标为:" + postion); + } else { + System.out.println("数组中不包括:" + findNumber); + } + } + public static int findNumber(int[] a, int n) { + for (int s =0; s < a.length; s++) { + if (a[s] == n) { + continue; + } + + } + return s; + } +} + +``` + +```注意 +本例定义了一个findNumber 方法,该方法接收两个参数,一个参数为数组类型,是原数据,另一个参数是需要在数组中查询的数字,这个方法根据这两个参数,使用for循环进行查询,一旦找到,即刻终止程序,此时s的值就是被找到数字的下标;如果没有找到,s的值随着循环的继续会持续增加,循环结束后,s的值为a.length.本方法最终的返回值s可以作为是否找到的判断依据,一旦s为a.length.说明没有找到,如果s的值在0和a.length之间,说明找到了 +调用说明方法的时候,需要按照findNumber方法的个格式,依次传递一个数组及需要查找的数字,顺序不能颠倒,类型不能错误,在保证格式正确的前提下,该方法可以任意的整型数组进行数据查找 +``` + +因为数组是复合型,数组变量存储的是数组存储区的引用,所以,传送数组或反回数组实际上是在传送引用 + +```java +import static jdk.internal.org.jline.keymap.KeyMap.display; + +public class lean { + public static void main(String[] args) { + int a[] = {0, 25, 64, 52}; + display(a); + change(a); + display(a); + } + + private static void display(int[] a) { + for (int l = 0; l < a.length; l++) + System.out.print(l + ""); + System.out.println(); + } + + private static void change(int[] a) { + for (int s = 0; s < a.length; s++) + a[s] += 10; + } +} +``` + +```java +/* +把数组、普通变量作为参数 +*/ +public class lean { + public static void reolt(int s,String k,char [] c) { + s =200; + k = "good byte"; + c[0] = 'g'; + + } + + public static void main(String[] args) { + int num =1; + String str = "byte"; + char [] ch = {'l','k','g','u'}; + changeValute(num,str,ch); + System.out.println("num =" +num); + System.out.println("str =" +str); + System.out.println("ch[0] =" +ch[0]); + + } + + private static void changeValute(int num, String str, char[] ch) { + return ; + } +} +``` + +### 2.变量的作用域 + +块由左右两个大括号包含,块作用域内定义的变量只能在本块内使用 + +作用域可以嵌套,如果定义一个方法,在方法内定义了一个变量,那么在该方法的任意块中,该变量均可以使用,反之,如果在内部块中定义变量,那么在块的外部是不可以使用的 + +```java +import static java.lang.Math.max; + +public class lean { + public static int reolt(int s, int l) { + int max; + if(s>l){ + int g = 100; + max =s ; + + } else { + max =l; + } + return max; + } + + public static void main(String[] args) { + int max = max(6,10); + System.out.println(max); + } + +} +``` + +```注意 +本例由一个 max方法,作用域是接收两个数字,经过判断返回其中的较大者,main方法中调用了max方法 +``` + +```java +/* +变量的作用域 +*/ +public class lean { + public static void reolt() { + int a =0; + a++; + System.out.println("a =" +a); + } + + public static void main(String[] args) { + reolt(); + reolt(); + } +} +``` + +### 3.可变参数的方法 + +Java语言在JDK1.5中首次推出Java可变参数,即variable arguments,或简称varargs + +这以新语言特征给软件开发人员在编写方法是将提供了方便性和灵活性 + +```java +public class lean { + public static int reolt(int ...args) { + int max = 0; + System.out.println(args[0]); + for(int s : args){ + if(max