diff --git "a/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" "b/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index b55de69a8654727dd3fc772e60a229321205cc59..0000000000000000000000000000000000000000 --- "a/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\344\270\211\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,239 +0,0 @@ -# 认识运算符和表达式 - -### 1.表达式 - -最常见的运算符有: - -+ 赋值运算符 -+ 算术运算符 -+ 关系运算符 -+ 逻辑运算符 -+ 条件运算符(三元运算符) - -### 2.赋值运算符与赋值表达式 - -赋值运算符: - -##### 例 - -```java -variable=expression; -``` - -```注意 -variable是任何有效的标识符,expression是常量、变量、表达式 -float a; -a =62.5f; -``` - - - -#### 3.算术运算符与算术表达式 - -​ 算术运算符 - -算术运算符 描述 表达式 - -​ + 执行加法运算 a+b - -​ - 执行减法运算 a-b - -​ * 执行乘法运算 a*b - -​ / 执行除法运算得到商 a/b - -​ % 执行除法运算得到余数 a%b - -​ ++ 将操作数自加1 a++或++a - -​ -- 将操作数自减1 a--或--a - -```注意 -运算符分为一元运算符和二元运算符 -``` - -###### 1.一元运算符 - -只处理一个操作数的运算符 - -++递增运算符 --递减运算符 - -##### 例 - -```java -a =1; -b =a++; -System.out.println("a ="+a); -System.out.println("b ="+b); -``` - - - -###### 2.二元运算符 - -处理两个操作数的运算符 - -“+”、“*”、“/”、“%” - -##### 例 - -```java -a =1; -b =2; -f =a-b; -s =a*b; -t =a%b; -System.out.println("差为:"+f); -System.out.println("积为:"+f); -System.out.println("商为:"+f); -System.out.println("余数为:"+f); -``` - -##### 例 - -```java -int a =1;b=2; -int f =a/b; -float s =a/(float)b; -float c =(float)(a/b); -float v =a/b*2.5f; -System.out.println(f); -System.out.println(s); -System.out.println(c); -System.out.println(v); -``` - -###### 3.关系运算符与关系表达式 - -​ 关系运算符 - -关系运算符 描述 表达式 - -**>** 检查一个操作数是否大于另一个操作数 a>b - -**<** 检查一个操作数是否小于另一个操作数 a=** 检查一个操作数是否大于等于另一个操作数 a>=b - -**<=** 检查一个操作数是否小于等于另一个操作数 a<=b - -**==** 检查两个操作数是否相等 a==b - -**!=** 检查两个操作数是否不相等 a!=b - -```注意 -"=="和"="的区别,"="是赋值运算符,代表。要把右侧表达式的值赋给左边的变量,而"=="是检查左侧和右侧的值是否相等,如果相等为true;否则为flase -``` - -##### 例 - -```java -/** -*关系运算符 -*@version:2022-111-25 -*@author:flkh -*/ -public class flksh{ - public static void main(String []args) - { - int a =1,b =2; - System.out.println("a ="+a,"b ="+b); - System.out.println("a>b 表达式的结果为:+(a>b)"); - System.out.println("a=b 表达式的结果为:+(a>=b)"); - System.out.println("a<=b 表达式的结果为:+(a<=b)"); - System.out.println("a==b 表达式的结果为:+(a==b)"); - System.out.println("a!=b 表达式的结果为:+(a!=b)"); - System.out.println("a+=b 表达式的结果为:+(a+=b)"); - } -} -``` - -###### 7.逻辑运算符与逻辑表达式 - -​ 逻辑表达式 - -逻辑运算符 描述 表达 - -!(逻辑非) 将操作数的值改变,真假反转 !a - -&&(短路与) 只有两个条件都为真返回真,否则返回假 a&&b - -||(短路或) 两个条件任意一个为真就返回真,否则返回假 a||b - -##### 例 - -```java -int a =1;b =2, c =3; -boolean a1,a2,a3; -a1 =(a>b)&&(c>=b); -System.out.println("\na1 =" +a1); -a2 =(a=b); -System.out.println("\na2 =" +a2); -a3 =!a2; -System.out.println("\na3 =" +a3); -``` - -##### 例 - -```java -int a =1;b =2,c =3; -boolean a1,a2; -a1 =(a>b)&&(c++>=b); -System.out.println("a1 =" +a1); -System.out.println("c =" +c); -c =3; -a2 =(a>b)||(c++>=b); -System.out.println("\na2 =" +a2); -System.out.println("c =" +c); -``` - -###### 8.条件运算符 - -条件运算符又称三元运算符,是"?"和 ":"符合组合 - -test:任何一个boolean表达式 - -##### 例 - -```java -int a = 1; -Strint str; -str =(a%2==0)?"a是偶数!":"a是奇数!"; -``` - -###### 9.运算符的优先级 - -​ 运算符优先级 - -次序 运算符 结合性 - -1 括号,如()和[] 从左到右 - -2 一元运算符,如+(正)、-(负)、++、--和! 从右到左 - -3 乘法算术运算符,如*、/和% 从左到右 - -4 加减法算术运算符,如+(加)和-(减) 从左到右 - -5 大小关系运算符,如>、<、>=和<= 从左到右 - -6 相等关系运算符,如==和!= 从左到右 - -7 与运算符,如&和&& 从左到右 - -8 异或运算符,如^ 从左到右 - -9 或运算符,如 | 和 || 从左到右 - -10 条件运算符,如?: 从左到右 - -11 赋值运算符,如=、+=、-=、/=和%= 从右到左 - - - - - - - - diff --git "a/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" "b/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index 4eb8000b353f20e2138df1f40190dea4a1dadd85..0000000000000000000000000000000000000000 --- "a/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\344\272\214\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,296 +0,0 @@ -# 认识变量和数据类型 - -### 标识符 - -1.由字母、下划线、美元符号($)和数字组成,并且第一个字符不能是数字。 - -### 关键字 - -adstract else long switch - -boolean extends native synchronized - -break final new this - -byte finally null throw - -case float package throws - -catch for private transient - -char goto* protected try - -class if public void - -const* implements return volatile - -continue import short while - -default instanceof static widefp - -do int stricitp - -double interface super -```注意 -* 是目前未使用的保留关键字 -``` - -### 数据类型 - -数据类型:简单数据类型、符合数据类型 - -简单数据类型:数据类型:整数类型(byte、short、int、long)、浮点类型(float、double) - -​ 字符类型(char) - -​ 布尔类型(boolean) - -复合型数据类型:类(class) 数组类型 - -​ 接口类型(interface) - -#### 1.整数 - -短整数(short,占2个字节) - -整数(int,占4个字节) - -长整数(long,占8个字节) - -```注意 -长整数所占的内存比整数的多,可表示的数值范围也就越大,同样,整数可表示是整数数值范围也比短整数的大 -``` - -#### 2.字节型 - -byte数据类型:一个字节型数据类型占一个字节,有必要的话,byte数据类型也可以存储一定的整数数值 - -#### 3.浮点数 - -用来存储小数数值,也可以用来存储范围更大的整数,可分为单精度浮点数(float,占4个字节)与双精度浮点函数(double,占8个字节) - -```注意 -双精度浮点数所使用的内存空间比单精度浮点数的多,可表示的数值范围与精确度也比较大。对于1.0、0.999这样的数据,Java语言里默认都是双精度浮点类型的,即double类型,它们的float类型为1.0f、0.999f -``` - -#### 4.字符 - -Java的字符采用Unicode编码,其中前128个字符编码与ASCII编码兼容,每个字符数据类型占2个字节,可存储的字符范围从\u000到\uFFFF - -​ 常见的转义字符 - -​ 功能 字符形式 - -​ 回车 \r - -​ 换行 \n - -​ 水平制表 \t - -​ 退格 \b - -​ 单引号 \ ‘ - -​ 双引号 \ "" - -​ 反斜线 \ \ - -​ 换页 \f - - - -#### 5.布尔数 - -布尔数占总内存1个字节,可存储true与flase两个数值,分别表示逻辑的真与假 - -​ 数据类型空间区域 - -数据类型 占用空间\字节 数值范围(e表示科学及算法) - -byte 1 -128~127 - -short 2 -32 768~32 767 - -int 4 -2 147 483 648~2 147 483 647 - -long 8 -9 223 372 036 854 775 808~9 - -​ 223 372 036 854 775 807 - -float 4 1.401 298e-45~3.402 - -​ 823e+38 - -double 8 4.900 000e-324~1.797 - -​ 693e+308 - - - -```注意 -范围小 byte-short-char-int-long-float-double 范围大 比较常用的数据类型:int、float、boolean -``` - - - -## 变量的声明及使用 - -#### 1.声明变量 - -##### 例: - -数据类型变量名=值; - -```java -int a =50; -``` - -等号代表把右侧的20放入左侧age所指向的内存空间内,当然,也可以先声明,然后赋值,分两步走: - -```java -int a; - -int b; - -a =50; //a 赋值给50 - -b =60; - -a =80; //把a的值改为80 -``` - -定义也可以这么写: - -```java -int a,b,c; -``` - -每个变量用逗号隔开,结束用分号 - -```java -int a =50,b,c =156; -``` - -可以写成一连串的变量定义: - -```java -char sex='M'; - -boolean isMan =true; -double Mymoney=-5000,hisMonmey=9999999.9999; -``` - - - -字符串专用方法: - -```java -String name ="yangming"; -name="yangming"; //赋值给yangming -``` - -#### 2.使用变量 - -```java -/* -@file:Student.java -@version:2022-11-25 12:17 -@author:hopeful -*/ -public class Student{ - /* - @pargam args - */ - public static void main(String []args){ - //定义一个整数型变量,用来存储学员年龄 - int a; - //定义一个字符串变量,用来存储学员姓名 - String Name; - //给年龄赋值 - a =21; - //给姓名赋值 - Name = "Yangming"; - //输入学员信息 - System.out.println(a); //打印出a的值 - System.ou.println(Name); //打印出Name的值 - } -} -``` - -可以把多个字符串连接起来一起输出: - -```java -System.out.println("姓名:"+Name+\n "+"年龄:" "+a);//换行输出 -``` - - - -也可以个它一个值 - -```java -sj ="姓名:" "+Name+ "\n" +"年龄: "+a; - System.ou.println(sj); -``` - -#### 3.数据类型转换 - -在变成语言里,不同的数据类型混合运算时,会自动把”较小“类型的数据提升为"较大"类型的数据 -```java -public class int{ - public static void main(String []args){ - //初始价格 - int csjg =1; - //涨价 - double zj =0.5; - //最新价格 - double newja=csjg+zj; - System.out.println(newja); - } -} -``` - -#### 4.自动类型转换 - -##### 例 - -```java -byte a =1; -short b =a; -int c =b; -long j =c; -float l =j; -double k =l; -char i ='H'; -int in i; -``` - -#### 5.强制类型转换 - -##### 例 - -```java -(target-type) vale //指定的vale转换成的类型 - double a 256; -int b; -b=(int)a; -System.out.println(b); -``` - -#### 6.使用Scanner录入数据 - -##### 例 - -```java -//告诉编译器,Scanner在那个位置 -import java.util Scanner; -public class Scannertext{ - public static void main(String []args){ - //创建Scanner - Scanner scanner =new Scanner(System.in); - //接收一个整型数字 - int a =scanner.netlnt(); - //输出数字 - System.out.println("my a is" +a); - } -} -``` \ No newline at end of file diff --git "a/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232.md" "b/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index 967b435828694b298cf23339617c93e3194178de..0000000000000000000000000000000000000000 --- "a/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\345\205\253\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,285 +0,0 @@ -# Java 方法的应用 - -## 1.方法概述 - -“方法”一次来源与生活,反应计算机编程中,指的是某个问题的处理方式,例如main方法是解决所有问题的主干道,程序总是从main方法开始执行 - -### 2.方法定义 - -在掌握方法的定义前,应该明白在什么情况下需要定义方法,一般来说,如果一个小功能块比较完整,可以重复利用,方法在被使用之前需要被定义,那么如何定义方法呢? - -#### 例 - -```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("测试失败"); - } - - } - } -``` - - - diff --git "a/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.md" "b/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index 0543de3398f49092cc222dbbd5efc90df2fa9549..0000000000000000000000000000000000000000 --- "a/9\346\241\202\346\263\275\347\205\234/2022-11-25\347\254\254\345\233\233\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,173 +0,0 @@ -# 分支结构的应用 - -## 块作用域 - -#### 1.代码块 - -代码块:一块代码 - -##### 例 - -```java -public class gl{ -int a = 54; -System.println("a =" +a); -{ - a--; - System.out.println("a =" +a); - -} - System.out.println("a =" +a); -} -``` - -#### 2.分支结构之if-else - -### if语句 - -##### 例 - -```java -boolean a = false; -if (a ==false) - System.out.println("去睡觉"); - System.out.println("去玩"); -``` - -### if else语句 - -##### 例 - -```java -import java.util.Scanner;//声明Scanner -public class lean { - public static void main(String []args){ - int a; - Scanner scanner =new Scanner(System.in); - System.out.print("请输入一个数:"); - a = scanner.nextInt(); - if(a>0){ - System.out.print("这个数是正数"); - - } - else{ - System.out.print("这个数是负数"); - } - } -} - -``` - -### 嵌套if函数 - -##### 例 - -```java -public class lean { - public static void main(String[] args) { - int a = 1; - int b = 4; - int c = 3; - if (a > 0 && b > 0 && c > 0) { - if (a + b > c && a + c > b && b + c > a) { - System.out.println("这三个数能够组成三角形!"); - } else { - System.out.println("这三个数不能组成三角形!"); - } - } else { - System.out.println("这个三角形是正数!"); - } - } -} -``` - - - -#### 多重if语句 - -某些情况下,需要对一系列对等的条件进行判断,从而决定采用什么样的解决办法 - -对于学员成绩来说,不同的分数对应不同的等级 - -#### 例 - -```java -import com.sun.source.tree.Scope; - -import java.util.Scanner; -public class lean{ - public static void main(String []args){ - int fs; - char dj; - Scanner scanner = new Scanner(System.in); - System.out.println("请输入分数:"); - fs =scanner.nextInt(); - if(fs >=90){ - dj = 'A'; - } else if( fs >= 80) { - dj = 'B'; - } else if(fs >= 70){ - dj = 'B'; - } else if(fs >= 60){ - dj = 'D'; - } else{ - dj = 'E'; - } - System.out.println("分数为:"+fs); - System.out.println("等级为:"+dj); - } - } -``` - -#### 分支结构之switch - -switch结构,它可以替换某些多重if语句,使得程序代码阅读性大大提高,性能也得到提升 - -#### 例 - -```java -import java.util.Scanner; -public class lean { - public static void main(String []args){ - int a =20,b=2,ysf; - String line; - char zf; - Scanner scanner =new Scanner(System.in); - System.out.println("请输入运算符:"); - line=scanner.nextLine(); - zf =line.charAt(0); - switch (zf){ - case'+': - ysf =a+b; - break; - case '-': - ysf =a -b; - break; - case '*': - ysf =a*b; - break; - case '/': - ysf =a/b; - break; - case '%': - ysf =a%b; - break; - default: - System.out.println("运算符有错误"); - ysf =-1; - } - System.out.println("a"+zf+"b=" +ysf); - } -} -``` - - - - - - - - - - - diff --git "a/9\346\241\202\346\263\275\347\205\234/2022-11-26\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" "b/9\346\241\202\346\263\275\347\205\234/2022-11-26\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index c668abbd8a99e390f83059da79773ce9280e07d5..0000000000000000000000000000000000000000 --- "a/9\346\241\202\346\263\275\347\205\234/2022-11-26\347\254\254\344\272\224\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,139 +0,0 @@ -# 循环结构的应用 - -## 1.使用循环的原因 - -#### 例 - -```java -public class lean { - public static void main(String []args){ - int a =5; - System.out.println(--a); - System.out.println(--a); - System.out.println(--a); - System.out.println(--a); - System.out.println(--a); - System.out.println(--a); - } -} -``` - - - -### 2. while循环 - - - -if只能执行一次,while可以反复执行 - -条件可以是任何布尔表达式,只要条件表达式为真,循环就被执行,当条件为假时, - -程序控制就传递到循环后面紧跟的语句行 - - - -#### 例 - -```java -public class lean { - public static void main(String []args){ - int a =5, b =0; - while(b<10) { - System.out.println(--a); - b--; - } - } -} -``` - -#### 例2 - -```java -public class lean { - public static void main(String []args){ - int a =1,b =0; - while(a<=100){ - b =b+a++; - System.out.println( "b ="+b);} - } -} -``` - -#### 例3 - -```java -public class lean { - public static void main(String []args){ - int a =5; - while(a<+10) { - System.out.println(a); - } - } -} -``` - -## do-while循环 - -先执行循环体,然后计算条件表达式,为真执行,循环,否则,结束循环 - -#### 例 - -```java -public class lean { - public static void main(String []args){ - int a =1,b =0; - do{ - b =b+a++; - - - } while(a<=100); - System.out.println( "b ="+b); - } -} -``` - -## for循环 - -第一步:执行初始化 - -第二步:计算condition的值 - -第三步:条件成立开启循环 - -#### 例 - -```java -public class lean { - public static void main(String []args){ - int a =1,b =0; - for( a=1;a<101;a++){ - b =b+a; - System.out.println( "b ="+b);} - } -} -``` - -#### for循环的变化 - -#### 例 - -```java -public class lean { - public static void main(String []args){ - int a =0,b =0; - for( a=100;a<=200;a++){ - int c =0; - for(c=2;c=a/2){ - b++; - System.out.println(+a); - } - System.out.println( "b ="+b); - } - } -} -``` - diff --git "a/9\346\241\202\346\263\275\347\205\234/2022-11-27\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.md" "b/9\346\241\202\346\263\275\347\205\234/2022-11-27\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index af2085d9db7ffccf2ce63986a0576d1c30a2345a..0000000000000000000000000000000000000000 --- "a/9\346\241\202\346\263\275\347\205\234/2022-11-27\347\254\254\344\270\203\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,492 +0,0 @@ -# 数组的应用 - -## 1.数组概述 - -某校教师每次考试都需要对所带班级的学生成绩进行统计分析,用人工方法进行计算费时又费力,还容易出错,现在用java就可以统计 - -```java -public class lean { - public static void main(String []args){ - float pjcj; - float student1 = 99f;float student2 = 85f; - float student3 = 60f;float student4 = 60f; - float student5 = 66f;float student6 = 76f; - float student7 = 88f;float student8 = 69f; - float student9 = 78f; - pjcj =(student1+student2+student3+student4+student5 - +student6+student7+student8+student9)/9; - System.out.println(pjcj); - } -} -``` - -## 2.数组的声明 - -```java -public class lean { - public static void main(String []args){ - float []a; - String [] b; - long [] c; - - } -} -``` - -## 3.数组的初始化 - -#### 1.静态初始化 - -在声明数组变量的同时进行的 - -```java -float [] a = {80f,60,70,80}; -int [] b ={80,60,70,80}; -``` - - - -### 2.动态初始化 - -静态初始化的方式在声明数组时就必须定义数组的大小,以及每个元素的初始值,如果要定义的数组长度或数组数据只有在运行时才能决定,就要使用动态初始化 - -```java -public class lean { - public static void main(String []args){ - int [] a; - a = new int [15]; - char [] b = new char[20]; - a = new int [] {2,4,6,8}; - - } -} -``` - -### 3.数组的使用 - -数组完成声明与初始化后,就可以使用了,通过数组名与下标来引用数组中的每一个元素 - -```java -public class lean { - public static void main(String []args){ - int [] a; - a = new int[12]; - a[3]=15; - a[3+8]=60; - System.out.println(a[1]+a[0]); - } -} -``` - -##### 例 - -```java -public class lean { - public static void main(String[] args) { - int[] a; - int b = 0; - a = new int[4]; - a[0] = 90; - a[1] = 100; - a[2] = 80; - a[3] = 79; - for (b = 0; b <= 4; b++) { - System.out.println("student"+(b+1)+" "+ a[b] ); - } - } -} -``` - -### 4.使用 Length 属性测定数组长度 - - ##### 例 - -```java -public class lean{ - public static void main(String[] args) { - int a; - double [] a1; - char [] a2; - a1 = new double[1]; - a2 = new char[1]; - int [] a3 =new int[1]; - byte [] a4 =new byte[1]; - char a5[] = {'a'}; - System.out.println("a1.length =" + a1.length); - System.out.println("a2.length =" + a2.length); - System.out.println("a3.length =" + a3.length); - System.out.println("a4.length =" + a4.length); - System.out.println("a5.length =" + a5.length); - for( a=0;amax){ - max = ages[s]; - } - } - System.out.println("最大年龄为:" +max); - } -} -``` - -```注意 -本次用来两个循环,一个是给数组赋值,一个是为数组求最大值 -``` - - - -#### 3.求平均值 - -##### 例 - -```java -import java.util.Scanner; -public class lean { - public static void main(String[] args) { - int max,s =0;int [] ages = new int[6]; - int a, sum =0; - Scanner scanner = new Scanner(System.in); - System.out.println("请输入6名学员的年龄"); - for(s=0;smax){ - max = ages[s]; - } - } - for(int b =0;b -2.5;y -=0.1) - { - for(float x= (float) -1.5;x<1.5;x+= 0.02) - { - float a = x*x+y*y-1; - if((a*a*a-x*x*y*y*y)<=0) - { - System.out.print("+"); - } - else - System.out.print(" "); - } - System.out.println(); - } -} -} -``` - -## 3. break语句 - -在某些时候,我们需要在某些条件出现的时候,强制终止结束循环,而不是等到循环条件为false时 - -一旦在循环中遇到 **brdak**,系统将完全结束该程序,开始执行循环之后的代码 - -#### 例 - -```java -public class lean { - public static void main(String []args){ - int a =0; - while(true){ - System.out.println(a); - a++; - if(a>=10){ - break; - } - } - } -} -``` - -#### 例2 - -```java -public class lean { - public static void main(String []args){ - int a =0,b =0; - for(a=0;a <=15;a++){ - for(b=0;b<=13;b++){ - if(b>a){ - break; - } - System.out.print("*"); - } - System.out.println(); - } - } -} -``` - -## 4. continue语句 - -#### 例 - -```java -public class lean { - public static void main(String []args){ - int a =0,b =0; - for(a=0;a <=15;a++){ - for(b=0;b<=13;b++){ - if(b>a){ - continue; - } - System.out.print("*"); - } - System.out.println(); - } - } -} -``` - diff --git "a/9\346\241\202\346\263\275\347\205\234/\344\275\234\344\270\2323.md" "b/9\346\241\202\346\263\275\347\205\234/\344\275\234\344\270\2323.md" deleted file mode 100644 index b3ab46505a4ab74df258b3caf0cbda12d934004a..0000000000000000000000000000000000000000 --- "a/9\346\241\202\346\263\275\347\205\234/\344\275\234\344\270\2323.md" +++ /dev/null @@ -1,55 +0,0 @@ -作业 : - -3. 定义一函数,用于求2个数中的较大数,并将其返回,这2个数字在主函数中由用户输入 - - ```java - import java.util.Scanner; - - public class jhg { - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - System.out.println("请输入a的值"); - int a = scanner.nextInt(); - System.out.println("请输入b的值"); - int b = scanner.nextInt(); - int s = getNum(a,b); - System.out.println("最大值为:"+s); - } - - private static int getNum(int a,int b) { - int max = a>b?a:b; - return max; - } - - } - ``` - - - -4. 在主函数中从键盘接收X, Y , Z3个数,编写函数计算这3个数的立方和并返回计算结果:S=X3+Y3+Z3 - -```java -import java.util.Scanner; - -public class jhg { - private static int X = 0; - private static int Z = 0; - private static int S = 0; - private static int Y = 0; - - public static void main(String[] args) { - Scanner scanner = new Scanner(System.in); - System.out.println("请输入X的值"); - X = scanner.nextInt(); - System.out.println("请输入Y的值"); - Y = scanner.nextInt(); - System.out.println("请输入Z的值"); - Z = scanner.nextInt(); - S = X*X*X+Y*Y*Y+Z*Z*Z; - System.out.println("这三个数的立方和为:"+S); - } - - -} -``` - diff --git "a/9\346\241\202\346\263\275\347\205\234/\345\233\276\344\271\246\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/9\346\241\202\346\263\275\347\205\234/\345\233\276\344\271\246\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000000000000000000000000000000000000..cb304067432fca129388ff6f5bc6cebc6a356534 --- /dev/null +++ "b/9\346\241\202\346\263\275\347\205\234/\345\233\276\344\271\246\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,171 @@ +【本周大作业】: +============================== +- 欢迎使用3班学生管理系统 - +- 1.浏览所有学生信息 - +- 2.添加学生信息 - +- 3.修改学生信息 - +- 4.删除学生信息 - +- 5.查询学生信息 - +- 6.退出管理系统 - + + 请输入对应的数字选择你需要的功能: + +```java + + +import java.util.ArrayList; +import java.util.Scanner; + + +public class Student{ +// 学号 + private String sid; +// 姓名 + private String name; + + public Student() { + } + + public Student(String sid, String name) { + this.sid = sid; + this.name = name; + } + + public String getSid() { + return sid; + } + + public void setSid(String sid) { + this.sid = sid; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public static void main(String[] args) { + ArrayList array = new ArrayList(); + while(true){ + System.out.println("\n==========================="); + System.out.println("-\t欢迎使用3班学生管理系统\t-"); + System.out.println("-\t1.浏览所有学生信息\t-"); + System.out.println("-\t2.添加学生信息\t-"); + System.out.println("-\t3.修改学生信息\t-"); + System.out.println("-\t4.删除学生信息\t-"); + System.out.println("-\t5.查询学生信息\t-"); + System.out.println("-\t6.退出管理系统\t-"); + System.out.println("==========================="); + System.out.println("请输入对应数字选择你需要的功能:"); + Scanner scanner = new Scanner(System.in); + String number = scanner.next(); + switch(number){ + case "1": + System.out.println("所有学生信息"); + everyone(array); + break; + case "2": + System.out.println("添加学生信息"); + addstudent(array); + break; + case "3": + System.out.println("修改学生信息"); + updatestudent(array); + break; + case "4": + System.out.println("删除学生信息"); + deletestudent(array); + break; + case "5": + System.out.println("查询学生信息"); + findstudent(array); + break; + case "6": + System.out.println("感谢使用!"); + System.exit(0); + } + } + } + public static void everyone(ArrayList array){ + if(array.size() ==0){ + System.out.println("没有数据!"); + return ; + } + + System.out.println("学号\t姓名"); + for(int i = 0; i array){ + Scanner scanner = new Scanner(System.in); + System.out.println("请输入学号:"); + String sid = scanner.next(); + System.out.println("请输入学生姓名"); + String name = scanner.next(); + Student st = new Student(); + st.setSid(sid); + st.setName(name); + array.add(st); + System.out.println("添加成功!"); + } +// 修改学生信息 + public static void updatestudent(ArrayList array) { + Scanner scanner = new Scanner(System.in); + System.out.println("请输入修改的学号"); + String sid = scanner.next(); + System.out.println("请输入修改的姓名"); + String name = scanner.next(); + Student student = new Student(); + student.setSid(sid); + student.setName(name); + for(int i = 0;i < array.size();i++){ + array.get(i); + if(student.getSid().equals(sid)){ + array.set(i,student); + break; + } + } + System.out.println("修改成功!"); + } +// 删除学生信息 + public static void deletestudent(ArrayList array){ + Scanner scanner = new Scanner(System.in); + System.out.println("请输入要删除的学生的学号:"); + String sid = scanner.next(); + for(int i =0; i< array.size();i++){ + Student student = array.get(i); + if(student.getSid().equals(sid)){ + array.remove(i); + break; + } + } + System.out.println("删除成功!"); + } +// 查询学生信息 + public static void findstudent(ArrayList array) { + Scanner scanner = new Scanner(System.in); + System.out.println("请输入需要查询的学生学号"); + String sid = scanner.next(); + System.out.println("请输入需要查询的姓名"); + String name = scanner.next(); + Student student = new Student(); + student.setSid(sid); + student.setName(name); + for(int i = 0; i < array.size();i++){ + array.get(i); + if(student.getSid().equals(sid)){ + everyone(array); + break; + } + System.out.println("未找到该数据!"); + } + } +} +``` + diff --git "a/9\346\241\202\346\263\275\347\205\234/\345\234\206\347\232\204\351\235\242\347\247\257\345\222\214\345\221\250\351\225\277\357\274\214\346\261\202\345\222\214\357\274\214\351\230\266\344\271\230.md" "b/9\346\241\202\346\263\275\347\205\234/\345\234\206\347\232\204\351\235\242\347\247\257\345\222\214\345\221\250\351\225\277\357\274\214\346\261\202\345\222\214\357\274\214\351\230\266\344\271\230.md" index 3051554af934e40a046004eac8cc7d3bae47fb29..3971c8cc94d88477c972dd9a3d16bb7d8ef00bf3 100644 --- "a/9\346\241\202\346\263\275\347\205\234/\345\234\206\347\232\204\351\235\242\347\247\257\345\222\214\345\221\250\351\225\277\357\274\214\346\261\202\345\222\214\357\274\214\351\230\266\344\271\230.md" +++ "b/9\346\241\202\346\263\275\347\205\234/\345\234\206\347\232\204\351\235\242\347\247\257\345\222\214\345\221\250\351\225\277\357\274\214\346\261\202\345\222\214\357\274\214\351\230\266\344\271\230.md" @@ -69,30 +69,47 @@ public class lkfk { 7. 在主函数中接收10个数存入数组,在自定义函数中,将该数组中的最大值与第一个元素交换,最小值与最后一个元素交换,然后在主函数中输出交换后的数组 ```java - public class kd { + import java.util.Arrays; + + public class h { public static void main(String[] args) { int[] a = new int[10]; System.out.println("这10个数为:"); for (int i = 0; i < a.length; i++) { a[i] = (int) Math.round(Math.random() * 100); + System.out.print("\t" + a[i]); } g(a); + System.out.println(); + szzh(a); + System.out.print("转换后的数组为:\n\t"+Arrays.toString(a)); } - public static int g(int []a) { + public static int g(int[] a) { int max = a[0]; + int min = a[0]; for (int i = 0; i < a.length; i++) { if (max < a[i]) { max = a[i]; - + } else if (min > a[i]) { + min = a[i]; } } - System.out.print("\t最大值为:" + max); + + System.out.print("\t\n最大值为:" + max); + System.out.print("\t最小值为:" + min); return max; } - } + public static void szzh(int[] a) { + for (int i = 0; i < a.length / 2 ; i++) { + int temp = a[a.length - 1- i]; + a[a.length - 1- i] = a[i]; + a[i] = temp; + } + } + } ```