From 0e4e35d43a98f0503cd6d87225b003e66bfc3116 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E5=AD=94=E5=87=AF=E8=BE=89=E2=80=9D?= <“2814283821@qq.com”> Date: Wed, 7 Dec 2022 23:33:04 +0800 Subject: [PATCH 1/4] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...00\346\254\241\344\275\234\344\270\232.md" | 0 ...11\346\254\241\344\275\234\344\270\232.md" | 148 ++++++++++++++++++ ...11\346\254\241\347\254\224\350\256\260.md" | 135 ++++++++++++++++ ...33\346\254\241\347\254\224\350\256\260.md" | 49 ++++++ 4 files changed, 332 insertions(+) rename "\345\255\224\345\207\257\350\276\2112244310344/2022-11-25\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" => "44 \345\255\224\345\207\257\350\276\211/2022-11-25\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" (100%) create mode 100644 "44 \345\255\224\345\207\257\350\276\211/2022-12-1\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" create mode 100644 "44 \345\255\224\345\207\257\350\276\211/2022-12-1\347\254\254\344\270\211\346\254\241\347\254\224\350\256\260.md" create mode 100644 "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" diff --git "a/\345\255\224\345\207\257\350\276\2112244310344/2022-11-25\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/44 \345\255\224\345\207\257\350\276\211/2022-11-25\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" similarity index 100% rename from "\345\255\224\345\207\257\350\276\2112244310344/2022-11-25\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" rename to "44 \345\255\224\345\207\257\350\276\211/2022-11-25\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" diff --git "a/44 \345\255\224\345\207\257\350\276\211/2022-12-1\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" "b/44 \345\255\224\345\207\257\350\276\211/2022-12-1\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..bb3fe06 --- /dev/null +++ "b/44 \345\255\224\345\207\257\350\276\211/2022-12-1\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,148 @@ +# Java基本课堂练习 + +1. 设计一个程序用来登记学生信息,要求用户输入姓名、学号、性别、班级、身高等信息。其中身高单位是米。 【Scanner】 + +2. + +3. 利用三元运算符求任意三个数中最大者 【三元】 + + ```java + import java.util.Scanner;//强制声明Scanner + + public class zuoye2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + //定义变量 + int x,y,z,max,c; + //输入x的值 + System.out.println("请输入x的值"); + x = sc.nextInt(); + //输入y的值 + System.out.println("请输入y的值"); + y = sc.nextInt(); + //输入z的值 + System.out.println("请输入z的值"); + z = sc.nextInt(); + //三元运算符 + c = x>y ? x :y; + max = c >z ? c :z; + System.out.println("最大值为:" +max); + } + } + ``` + + + +4. 若任意两数和是3的倍数或其中一个为3,则输出true,否者输出false 【断路或】 + + ```java + import java.util.Scanner;//强制声明Scanner + + public class zuoye2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + //定义变量 + int x,y,f; + //输出x的值 + System.out.println("请输入x的值"); + x = sc.nextInt(); + //输出y的值 + System.out.println("请输入y的值"); + y = sc.nextInt(); + f = x+y; + if(x + y == 3 || f%3 ==0){ + System.out.println("true"); + } else { + System.out.println("flase"); + } + } + } + ``` + + + +5. 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: + + > 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 + > + > 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 + > + > 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少?【使用变量的定义和算术运算符完成本题】 + + ```java + public class zuoye2 { + public static void main(String[] args) { + //定义变量 + int ez, nr, d = 177, m = 165; + ez = (int) ((d + m) * 1.08 / 2); + nr = (int) (d * 0.923 + m) / 2; + System.out.println("儿子的身高为:" +ez); + System.out.println("女儿的身高为:" +nr); + } + } + ``` + + + +6. 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。绿茶妹妹有24元钱,她攒了几天钱之后自己的 钱正好是原来的两倍。 + + - 那么红茶和绿茶现在的钱一样多,请问对么?【使用赋值运算符和算术运算符计算各自的钱,使用比较运算符对比大小】 + + ```java + public class zuoye2 { + public static void main(String[] args) { + //定义变量 + int hc,lc; + hc = 21*2+3; + lc = 24*2; + System.out.println("红茶的钱为:"+hc); + System.out.println("绿茶的钱为:"+lc); + System.out.println("那么红茶和绿茶现在的钱一样多,请问对吗?"); + System.out.println(hc==lc?"红茶的钱比较多":"绿茶的钱比较多"); + } + } + ``` + + + +7. 小明想在食堂打包一份午饭,商家的优惠方式如下:鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元,但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱?【使用算术运算符、赋值运算符和三元运算符联合完成本题】 + +```java +public class zuoye2 { + public static void main(String[] args) { + //定义变量 + int dz,yh,y=24,h=8,m=3; + dz = (int) ((int)(y+h+m)*0.8); + y=16; + yh = y+h+m ; + System.out.println("打折:"+dz); + System.out.println("优惠:"+yh); + System.out.println(dz y ? ++x : y++; 答案为: 6 + +```java + int a =10; + sout(a+"Hello");// 输入了什么 + 答案为:10Hello +``` + + + + + + + diff --git "a/44 \345\255\224\345\207\257\350\276\211/2022-12-1\347\254\254\344\270\211\346\254\241\347\254\224\350\256\260.md" "b/44 \345\255\224\345\207\257\350\276\211/2022-12-1\347\254\254\344\270\211\346\254\241\347\254\224\350\256\260.md" new file mode 100644 index 0000000..dd0bb16 --- /dev/null +++ "b/44 \345\255\224\345\207\257\350\276\211/2022-12-1\347\254\254\344\270\211\346\254\241\347\254\224\350\256\260.md" @@ -0,0 +1,135 @@ +## 2. 运算符 + +### 2.1 算术运算符 + +#### 2.1.1 运算符和表达式(了解) + +运算符:对常量或者变量进行操作的符号 + +表达式:用运算符把常量或者变量连接起来符合Java语法的式子就可以称为表达式。 + +​ 不同运算符连接的表达式体现的是不同类型的表达式。 + ++:是运算符,并且是算术运算符。 + + a + b:是表达式,由于+是算术运算符,所以这个表达式叫算术表达式。 a和b称为操作数 + +**注意:** + +1. /和%的区别:两者都做除法, + - / 取结果的商, + + - % 取结果的余数。 + +2. 整数操作只能得到整数,要想得到小数,必须有浮点数参与运算。 + +#### 2.1.3 字符的“+”操作(理解) + +char类型参与算术运算,使用的是计算机底层对应的十进制数值。需要我们记住三个字符对应的数值: + +'a' -- 97 a-z是连续的,所以'b'对应的数值是98,'c'是99,依次递加 + +'A' -- 65 A-Z是连续的,所以'B'对应的数值是66,'C'是67,依次递加 + +'0' -- 48 0-9是连续的,所以'1'对应的数值是49,'2'是50,依次递加 + +算术表达式中包含不同的基本数据类型的值的时候,整个算术表达式的类型会自动进行提升。 + +**提升规则:** + +```java +1. byte类型,short类型和char类型将被提升到int类型,不管是否有其他类型参与运算。 +2. 整个表达式的类型自动提升到与表达式中最高等级的操作数相同的类型 +3. 等级顺序:byte,short,char --> int --> long --> float --> double +``` + +### 2.2 自增自减运算符(理解) + +| 符号 | 作用 | 说明 | +| ---- | ---- | ----------- | +| ++ | 自增 | 变量的值加1 | +| -- | 自减 | 变量的值减1 | + +注意事项: + +​ ++和-- 既可以放在变量的后边,也可以放在变量的前边。 + +​ 单独使用的时候, ++和-- 无论是放在变量的前边还是后边,结果是一样的。 + +​ 参与操作的时候,如果放在变量的后边,先拿变量参与操作,后拿变量做++或者--。 + +​ 参与操作的时候,如果放在变量的前边,先拿变量做++或者--,后拿变量参与操作。 + +### 2.3 赋值运算符(应用) + +赋值运算符的作用是将一个表达式的值赋给左边,左边必须是可修改的,不能是常量。 + +| 符号 | 作用 | 说明 | +| ---- | ---------- | --------------------- | +| = | 赋值 | a=10,将10赋值给变量a | +| += | 加后赋值 | a+=b,将a+b的值给a | +| -= | 减后赋值 | a-=b,将a-b的值给a | +| *= | 乘后赋值 | a*=b,将a×b的值给a | +| /= | 除后赋值 | a/=b,将a÷b的商给a | +| %= | 取余后赋值 | a%=b,将a÷b的余数给a | + +注意: + +扩展的赋值运算符隐含了强制类型转换。 + +### 2.4 关系运算符(应用) + +关系运算符有6种关系,分别为小于、小于等于、大于、等于、大于等于、不等于。 + +| 符号 | 说明 | +| ---- | ------------------------------------------------------- | +| == | a==b,判断a和b的值是否相等,成立为true,不成立为false | +| != | a!=b,判断a和b的值是否不相等,成立为true,不成立为false | +| > | a>b,判断a是否大于b,成立为true,不成立为false | +| >= | a>=b,判断a是否大于等于b,成立为true,不成立为false | +| < | a; +}while(循环条件); +~~~ + + + + 与while循环不同的是do-while循环是先进入循环,后判断条件 + +使用do-while循环进行计算时最好先保存原始的值,后面可能会有用 + +### 3.在不同的场景使用不同的循环: + +1.如果循环必须执行一次,用do-while + +2.其他情况用while + +### 4.死循环: + +~~~ Java +for死循环格式 : +for(;;){ +} +while死循环格式 : +while(true){ +} +do..while死循环格式 : +do{ +}while(true); +~~~ + -- Gitee From d6b7dd60043ab174941b3ffd7d8d4662e9a42019 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E5=AD=94=E5=87=AF=E8=BE=89=E2=80=9D?= <“2814283821@qq.com”> Date: Wed, 7 Dec 2022 23:45:06 +0800 Subject: [PATCH 2/4] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...33\346\254\241\347\254\224\350\256\260.md" | 49 +++++++++++++++++++ 1 file changed, 49 insertions(+) create mode 100644 "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" 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 0000000..e077997 --- /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); +~~~ + -- Gitee From 8a2490b640cb546cd5ab2bd3863f6243333c6fb2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E5=AD=94=E5=87=AF=E8=BE=89=E2=80=9D?= <“2814283821@qq.com”> Date: Fri, 9 Dec 2022 10:59:04 +0800 Subject: [PATCH 3/4] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...24\346\254\241\344\275\234\344\270\232.md" | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 "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" 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 0000000..fd0377e --- /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,43 @@ +```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); + } + } +``` + -- Gitee From 30be7acccabdfbe14ae6eddd67cf4a6a5773651d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E2=80=9C=E5=AD=94=E5=87=AF=E8=BE=89=E2=80=9D?= <“2814283821@qq.com”> Date: Fri, 9 Dec 2022 11:03:24 +0800 Subject: [PATCH 4/4] =?UTF-8?q?=E7=AC=AC=E4=BA=94=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...24\346\254\241\344\275\234\344\270\232.md" | 609 ++++++++++++++++++ 1 file changed, 609 insertions(+) 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" index fd0377e..eb80ba6 100644 --- "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" @@ -1,3 +1,57 @@ +```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; @@ -41,3 +95,558 @@ public class fgn { } ``` +方法,一般来说,如果一个小功能块比较完整,可以重复利用,方法在被使用之前需要被定义,那么如何定义方法呢? + +#### 例 + +```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