From 985761c07363273039ff19620daddf224273fb84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0?= <3023096670@qq.com> Date: Fri, 31 Mar 2023 22:21:44 +0800 Subject: [PATCH 01/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0java=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" | 260 ++++++++++++++++++ ...55\346\263\225\350\256\255\347\273\203.md" | 120 ++++++++ 2 files changed, 380 insertions(+) create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..f88c2c8 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,260 @@ +```java +import java.util.Scanner; + +public class Class { +// **1、判断一个字符数据是否是数字字符 ** +// +// **分析:** +// +// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 +// +// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, +// 并且还要下于或等于字符9即可。 +// +// 3、判断完成之后,打印判断的结果。 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int i = sc.nextInt(); + if (i>=0 && i<10){ + System.out.println("是数字字符"); + }else { + System.out.println("不是数字字符"); + } + } +} + +import java.util.Scanner; + +public class java2 { + /* **2、判断一个字符数据是否是字母字符** + + **分析:** + + 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 + + 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, + 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char st = sc.next().charAt(0); + + if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) + + System.out.println( "是字母"); + + else + + System.out.println("不是字母"); + + } +} + + +import java.util.Scanner; + +public class java3 { +/* + **3、判断指定的年份是否为闰年,请使用键盘录入** + + **分析:** + + 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 + + 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 + +*/ + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int i = sc.nextInt(); + if ((i / 4 == 0 && i / 400 ==0) || i / 100 != 0 ){ + System.out.println("是闰年"); + }else{ + System.out.println("不是闰年"); + } + } +} + + +import java.util.Scanner; + +public class java4 { + // **4、判断一个数字是否为水仙花数,请使用键盘录入** +// +// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, +// +// **分析:** +// +// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 +// +// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 +// +// 2、判断的过程 +// +// a) 将3位数字的每一位上的数字拆分下来 +// +// b) 计算每位数字的3次幂之和 +// +// C) 用和值 和 原来的数字进行比较 +// +// D) 打印判断的比较结果即可 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个数字判断是不是水仙花数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int bai = i / 100 % 10; + if (i > 99 && i < 1000) { + if (ge * ge * ge + si * si * si + bai * bai * bai == i) { + System.out.println("是水仙花数"); + } else { + System.out.println("不是水仙花数"); + } + }else { + System.out.println("您好,水仙花数是三位数"); + } + } +} + + +import java.util.Scanner; + +public class java5 { + /* **5、判断一个5位数字是否为回文数,使用键盘录入** + + 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 + + **分析:** + + 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 + + 2、判断的过程 + + a) 将5位数字的万、千、十、个位数拆分出来 + + b) 判断比较万位和个位 、 千位和十位是否相等 + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入五位数判断是不是回文数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int qian = i / 1000 % 10; + int wan = i / 10000 % 10; + if (i > 9999 && i < 100000) { + if (ge == wan && si == qian) { + System.out.println("是回文数"); + } else { + System.out.println("不是回文数"); + } + }else { + System.out.println("您输入的不是五位数"); + } + } +} + + +import java.util.Scanner; + +public class java6 { +// ## 题目1(训练) +// +// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: +// +// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 +// +// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 +// +// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? +// +// ### 训练提示 +// +//1. 已知的父母身高如何用代码体现? +// +// 2. 题目中的公式如何转化为代码? +// +// ### 解题方案 +// +//1. 使用变量的定义和算术运算符完成本题 +// +//### 操作步骤 +// +//1. 定义小数变量代表父亲身高 +// +//2. 定义小数变量代表母亲身高 +// +//3. 通过儿子身高计算方式计算儿子身高 +// +//4. 通过女儿身高计算方式计算女人身高 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入父亲的身高"); + double baba = sc.nextInt(); + System.out.println("请输入母亲的身高"); + double mama = sc.nextInt(); + double erzi = (baba + mama) * 1.08 / 2; + double nver = (baba * 0.923 + mama) / 2; + System.out.println("儿子的身高为:" + erzi); + System.out.println("女儿的身高为:" + nver); + } +} + +public class java7 { +// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 +// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 +// 那么红茶和绿茶现在的钱一样多,请问对么? + public static void main(String[] args) { + int hocha = 21; + int lvcha = 24; + hocha = hocha * 2 + 3; + lvcha = lvcha * 2; + if (hocha == lvcha){ + System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); + }if (hocha > lvcha){ + System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + }if (hocha < lvcha){ + System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + } + } +} + + +public class java8 { +// 某小伙想定一份外卖,商家的优惠方式如下: +// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, +// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? + /*训练提示 + 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 + 解题方案 + 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 + 操作步骤 + 1. 使用算术运算符求出不使用优惠时的总价 + 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 + 3. 使用算术运算符求出使用优惠价时的总价 + 4. 使用三元运算符判断最终更合算的购买方式和花费*/ + public static void main(String[] args) { + int yuxian = 24; + int youza = 8; + int mifan = 3; + int zojia = yuxian + youza + mifan; + double youhuijia = zojia * 0.8; + System.out.println("使用折扣的总价:" + youhuijia); + int yuxian2 = 16; + int youza2 = 8; + int mifan2 = 3; + int zojia2 = yuxian2 + youza2 + mifan2; + System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); + if (youhuijia < zojia2){ + System.out.println("使用这个:" + youhuijia ); + }else if (youhuijia > zojia2){ + System.out.println("使用这个:" + zojia2); + } + } +} +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" new file mode 100644 index 0000000..7b6c657 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" @@ -0,0 +1,120 @@ +```java +package Ly; + +import javax.lang.model.SourceVersion; + +public class Class { + public static void main(String[] args) { + // 第一题 + //(1)先声明两个byte类型的变量b1,b2, + // 并分别赋值为10和20,求b1和b2变量的和, + // 并将结果保存在byte类型的变量b3中, + // 最后输出b3变量的值 + byte b1,b2; + b1=10; + b2=20; + byte b3=(byte)(b1+b2); + System.out.println("byte类型的b1和b2的和为:"+(b3)); + +// 第二题 +// (2)先声明两个short类型的变量s1,s2, +// 并分别赋值为1000和2000,求s1和s2变量的和, +// 并将结果保存在short类型的变量s3中, +// 最后输出s3变量的值 + short s1=1000,s2=2000; + short s3=(short)(s1+s2); + System.out.println("short类型的是s1和s2的和为:"+s3); + +// 第三题 +// (3)先声明1个char类型的变量c1赋值为'a', +// 再声明一个int类型的变量num赋值为5,求c1和num变量的和, +// 并将结果将结果保存在char类型的变量letter中, +// 最后输出letter变量的值。 + char c1='a'; + int num=5; + char letter=(char)(c1+(char)num); + System.out.println("char类型的c1和int类型的num的和:"+(letter)); + +// 第四题 +// (4)先声明两个int类型的变量i1,i2, +// 并分别赋值5和2,求i1和i2的商, +// 并将结果保存在double类型的变量result中, +// 最后输出result变量的值。如何得到结果2.5呢? + int i1=5,i2=2; + double result=(double) i1/i2; + System.out.println("i1和i2的商为:"+(result)); + } +} + + +package Ly; + +public class Class1 { + public static void main(String[] args) { + int a1=10,a2=11; + System.out.println("10是偶数?"+(a1%2==0)); + System.out.println("11是偶数?"+(a2%2==0)); + int a3=12,a4=13; + System.out.println("12是奇数?"+(a3%2!=0)); + System.out.println("13是奇数?"+(a4%2!=0)); + } +} + +package Ly; + +public class Class3 { + public static void main(String[] args) { +// 题目:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时? + int hours=89; + int day=hours/24; + int hour=hours%24; + System.out.println("为抵抗洪水,战士连续作战89小时:"+"89小时是"+(day)+"天"+(hour)+"小时"); + + } +} + +package Ly; + +public class Class4 { + public static void main(String[] args) { +// 题目:今天是周2,100天以后是周几? + int week=2; + week+=100; + week%=7; + System.out.println("今天是周二,100天以后是周"+(week)); + } +} + +package Ly; + +public class Class5 { + public static void main(String[] args) { +// 题目:判断今年是否是闰年 + int year=2023; + boolean a=(year%4==0 && year%100!=0); + boolean b=(year%4==0); + if(a && b){ + System.out.println(year+"是闰年"); + }else { + System.out.println(year + "不是闰年"); + } + } +} + + +package Ly; + +public class Class6 { + public static void main(String[] args) { +// 题目:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。 +// 它需要一个程序将华氏温度(80度)转换为摄氏度, +// 并以华氏度和摄氏度为单位分别显示该温度。 + double hua=80; + double she; + she=(hua-32)/1.8; + System.out.println("华氏度80.0度转化为摄氏度是"+(she)+"度"); + } +} + +``` + -- Gitee From 0f8a570618fcb31b2958b73cd1d782292d8cb03e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0?= <3023096670@qq.com> Date: Tue, 11 Apr 2023 10:29:31 +0800 Subject: [PATCH 02/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0java=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" | 4 +- ...55\346\263\225\350\256\255\347\273\203.md" | 45 +++++- ...60\347\273\204\344\275\234\344\270\232.md" | 148 ++++++++++++++++++ ...71\350\261\241\344\275\234\344\270\232.md" | 47 ++++++ 4 files changed, 234 insertions(+), 10 deletions(-) create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230408 java\347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" index f88c2c8..63c6bc5 100644 --- "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" +++ "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -14,7 +14,7 @@ public class Class { // 3、判断完成之后,打印判断的结果。 public static void main(String[] args) { Scanner sc = new Scanner(System.in); - int i = sc.nextInt(); + char i = sc.next.charAt(0); if (i>=0 && i<10){ System.out.println("是数字字符"); }else { @@ -69,7 +69,7 @@ public class java3 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); int i = sc.nextInt(); - if ((i / 4 == 0 && i / 400 ==0) || i / 100 != 0 ){ + if ((i % 4 == 0 && i% 100 ==0) || i % 400 != 0 ){ System.out.println("是闰年"); }else{ System.out.println("不是闰年"); diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" index 7b6c657..d8f1fa4 100644 --- "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" +++ "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" @@ -1,5 +1,5 @@ ```java -package Ly; +package My; import javax.lang.model.SourceVersion; @@ -47,7 +47,7 @@ public class Class { } -package Ly; +package My; public class Class1 { public static void main(String[] args) { @@ -60,7 +60,7 @@ public class Class1 { } } -package Ly; +package My; public class Class3 { public static void main(String[] args) { @@ -73,7 +73,7 @@ public class Class3 { } } -package Ly; +package My; public class Class4 { public static void main(String[] args) { @@ -85,15 +85,44 @@ public class Class4 { } } -package Ly; +package My; + +import java.util.Scanner; + +public class Class2 { + public static void main(String[] args) { +// ## 5、案例:求三个整数x,y,z中的最大值 +// +// +// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 +// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) +// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) +// 4. 输出结果 + Scanner sc=new Scanner(System.in); + System.out.print("请输入x的值:"); + int x=sc.nextInt(); + System.out.print("请输入y的值:"); + int y=sc.nextInt(); + System.out.print("请输入z的值:"); + int z=sc.nextInt(); + int max; + max=(x>y)?x:y; + max=(max>z)?max:z; + System.out.println(max); + } +} + + + +package My; public class Class5 { public static void main(String[] args) { // 题目:判断今年是否是闰年 int year=2023; boolean a=(year%4==0 && year%100!=0); - boolean b=(year%4==0); - if(a && b){ + boolean b=(year%400==0); + if(a || b){ System.out.println(year+"是闰年"); }else { System.out.println(year + "不是闰年"); @@ -102,7 +131,7 @@ public class Class5 { } -package Ly; +package My; public class Class6 { public static void main(String[] args) { diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000..dc4fd7e --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" @@ -0,0 +1,148 @@ +```java +public class T1 { + public static void main(String[] args) { +// .定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** +// +//**操作步骤:** +// +// 1.定义5个元素数组 +// 2.可以使用初始化数组的两种方式之一为数组元素赋值 +// 3.遍历数组求数组中的最小值 + + + int[] sum={12,32,52,2,5};//创立数组并赋值 + + int min=sum[0]; + for (int i=1;isum[i]){ + min=sum[i]; + } + } + System.out.println("数组sum的最小值为:"+min); + } +} +``` + + + +```java +package Glass; + +import java.util.Arrays; + +public class T2 { + public static void main(String[] args) { +// 需求:求出数组中索引与索引对应的元素都是奇数的元素** +// +//**分析:** +// +// 1、遍历数组 +// 2、判断索引是否是奇数(索引 % 2 != 0) +// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) +// 4、满足条件输出结果 + + int[] sum={12,4,32,6,32,11}; + System.out.println(Arrays.toString(sum)); + + for (int i=0;i Date: Thu, 13 Apr 2023 20:00:58 +0800 Subject: [PATCH 03/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0java=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\345\222\214\347\254\224\350\256\260.md" | 136 ++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 \347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230411 \347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" "b/25 \346\235\216\346\226\207\346\235\260/20230411 \347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" new file mode 100644 index 0000000..d87554f --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230411 \347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" @@ -0,0 +1,136 @@ +```java +package Glass; + +public class Employee { + //变量 + private int id; //编号 + String name; //姓名 + private int age; //年龄 + double salary; //薪资 + + + + + //方法 + + //编号 + public void setId(int id){ + if(id<=0){ + System.out.println("输入编号有误"); + return; + } + this.id=id; + } + //年龄 + public void setAge(int age){ + if(age<0){ + System.out.println("输入年龄有误"); + return; + } + this.age=age; + } + //薪资 + public void setSalary(double salary){ + if (salary<0){ + System.out.println("输入的薪资有误"); + return; + } + this.salary=salary; + } + + + + + + //打印值 + public String toString(){ + System.out.println("第"+id+"个员工的编号:"+id+".姓名:"+name+",年龄:"+age+",薪资:"+salary); + return null; + } + + + + //结构方法 + //无参 + public Employee(){ + id=1; + name="小李"; + age=18; + salary=1011.1127; + System.out.println("第"+id+"个员工的编号:"+id+".姓名:"+name+",年龄:"+age+",薪资:"+salary); + } + + //有参 + public Employee(int id,String name,int age,double salary){ + this.id=id; + this.name=name; + this.age=age; + this.salary=salary; + System.out.println("第"+id+"个员工的编号:"+id+".姓名:"+name+",年龄:"+age+",薪资:"+salary); + } + +} +``` + +```java +package Glass; + +public class Tesx { + public static void main(String[] args) { + Employee emp=new Employee();//无参结构 + + + emp.setId(2); + emp.setAge(18); + emp.name="小王"; + emp.salary=1011; + emp.toString();//打印值 + + + new Employee(3,"小张",18,1127);//有参结构 + } +} +``` + +## 1、 + +成员变量:类中的方法 + +局部变量:方法内或者方法声明上(形参) + +## 2、 + +private是一个权限修饰符,可以修饰成员,被private修饰的成员只能在本类中使用 + +针对private修饰的成员变量,如果需要被其他类使用,提供相应的操作: + +1.提供“get变量名()”方法,用于获取成员变量的值,方法用public修饰 + +2.提供“set变量名(参数)”方法,用于设置成员变量的值,方法用public修饰 + +## 3、 + +this:代表所在类的对象引用 + +this关键字的作用:可以调用本类的成员(变量、方法)解决局部变量和成员变量的重名问题 + +## 4、 + +构造方法 + +构建、创造对象的时候,所调用的方法 + +格式: + +1.方法名与类相同,大小写也要一致 + +2.没有返回值类型,连void都没有 + +3.没有具体的返回值(不能由retrun带回结果数据) + +执行时机: + +1.创建对象的时候调用,没创建一次对象,就会执行一次构造方法 + +2.不能手动调用构造方法 + -- Gitee From a1c3823a0e2705326b3a894f02b6a59953f1a8b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0?= <3023096670@qq.com> Date: Sat, 22 Apr 2023 17:59:08 +0800 Subject: [PATCH 04/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...32\345\222\214\347\254\224\350\256\260.md" | 130 ++++ ...32\345\222\214\347\254\224\350\256\260.md" | 136 ---- .../20230414 java\344\275\234\344\270\232.md" | 682 ++++++++++++++++++ ...32\345\222\214\347\254\224\350\256\260.md" | 290 ++++++++ ...06\345\220\210\344\275\234\344\270\232.md" | 205 ++++++ ...41\347\220\206\347\263\273\347\273\237.md" | 254 +++++++ 6 files changed, 1561 insertions(+), 136 deletions(-) create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 \347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230415 JavaAPI\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" "b/25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" new file mode 100644 index 0000000..9e3dc27 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" @@ -0,0 +1,130 @@ +# 笔记 + +成员变量:类中方法外的变量 + +局部变量:方法中的变量 + +**区别:1、位置不一样** + +**成员变量在堆内存** + +**局部变量在栈内存** + +**2、内存位置不同** + +**3、生命用期不同** + +**4、初始化不同** + +## 封装:private 关键字 + +权限修饰符,可以修饰成员(变量和方法) + +被private修饰的成员只能在本类中使用 + +**提供get变量名()方法获取值 方法用private修饰** + +**提供et变量名()方法设置值 方法用private修饰** + +**toString** 方法将对象转化字符串输出 + +**this关键字** + +可以调用本类的成员(变量和方法) + +可以解决重名的问题 + +构造方法:只能创建对象不能动用手动调用 + +构造器:构建创造对象时调用的方法 + +**标准类:** + +**使用private** + +**提供无参** + +**提供有参** + +**提供每一个成员变量对应的steXxx/getXxx** + +**提供每一个显示对象信息show()** + +**无参构造方法创建对象后使用steXxx/getXxx赋值对象** + +**使用有参构造方法直接创建有属性值** + +# 作业 + +```java +import com.sun.org.apache.xerces.internal.impl.xpath.XPath; + +public class Emp { + private int id; + private String name; + private int age; + private double salary; + + public Emp(){ + + } + + public void setId(int id){ + this.id = id; + } + + public void setName(String name){ + this.name = name; + } + + public void setAge(int age){ + this.age= age; + } + + public void setSalary(double salary){ + this.salary = salary; + } + + public int getId(){ + return id; + } + + public String getName(){ + return name; + } + + public int getAge(){ + return age; + } + + public double getSalary(){ + return salary; + } + public Emp(int id,String name,int age,double salary){ + this.id = id; + this.name = name; + this.age = age; + this.salary = salary; + } + + public String toString(){ + return "第" + id + "名员工的编号为:" + id + " 姓名:" + name + " 年龄:" + age + " 工资:" + salary; + } +} + + +public class TestEmp { + public static void main(String[] args) { + Emp em = new Emp(1,"张三",23,10000); + System.out.println(em.toString()); + + Emp em2 = new Emp(); + em.setId(2); + em.setName("李四"); + em.setAge(24); + em.setSalary(11000); + System.out.println(em.toString()); + } +} +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230411 \347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" "b/25 \346\235\216\346\226\207\346\235\260/20230411 \347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" deleted file mode 100644 index d87554f..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230411 \347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" +++ /dev/null @@ -1,136 +0,0 @@ -```java -package Glass; - -public class Employee { - //变量 - private int id; //编号 - String name; //姓名 - private int age; //年龄 - double salary; //薪资 - - - - - //方法 - - //编号 - public void setId(int id){ - if(id<=0){ - System.out.println("输入编号有误"); - return; - } - this.id=id; - } - //年龄 - public void setAge(int age){ - if(age<0){ - System.out.println("输入年龄有误"); - return; - } - this.age=age; - } - //薪资 - public void setSalary(double salary){ - if (salary<0){ - System.out.println("输入的薪资有误"); - return; - } - this.salary=salary; - } - - - - - - //打印值 - public String toString(){ - System.out.println("第"+id+"个员工的编号:"+id+".姓名:"+name+",年龄:"+age+",薪资:"+salary); - return null; - } - - - - //结构方法 - //无参 - public Employee(){ - id=1; - name="小李"; - age=18; - salary=1011.1127; - System.out.println("第"+id+"个员工的编号:"+id+".姓名:"+name+",年龄:"+age+",薪资:"+salary); - } - - //有参 - public Employee(int id,String name,int age,double salary){ - this.id=id; - this.name=name; - this.age=age; - this.salary=salary; - System.out.println("第"+id+"个员工的编号:"+id+".姓名:"+name+",年龄:"+age+",薪资:"+salary); - } - -} -``` - -```java -package Glass; - -public class Tesx { - public static void main(String[] args) { - Employee emp=new Employee();//无参结构 - - - emp.setId(2); - emp.setAge(18); - emp.name="小王"; - emp.salary=1011; - emp.toString();//打印值 - - - new Employee(3,"小张",18,1127);//有参结构 - } -} -``` - -## 1、 - -成员变量:类中的方法 - -局部变量:方法内或者方法声明上(形参) - -## 2、 - -private是一个权限修饰符,可以修饰成员,被private修饰的成员只能在本类中使用 - -针对private修饰的成员变量,如果需要被其他类使用,提供相应的操作: - -1.提供“get变量名()”方法,用于获取成员变量的值,方法用public修饰 - -2.提供“set变量名(参数)”方法,用于设置成员变量的值,方法用public修饰 - -## 3、 - -this:代表所在类的对象引用 - -this关键字的作用:可以调用本类的成员(变量、方法)解决局部变量和成员变量的重名问题 - -## 4、 - -构造方法 - -构建、创造对象的时候,所调用的方法 - -格式: - -1.方法名与类相同,大小写也要一致 - -2.没有返回值类型,连void都没有 - -3.没有具体的返回值(不能由retrun带回结果数据) - -执行时机: - -1.创建对象的时候调用,没创建一次对象,就会执行一次构造方法 - -2.不能手动调用构造方法 - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" new file mode 100644 index 0000000..3678a47 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" @@ -0,0 +1,682 @@ +```java +第一题 +package T1; + +import java.text.DecimalFormat; + +public class Round { + private double radius; + + public Round(){ + + } + + public void setRadius(double radius){ + this.radius = radius; + } + + public Round(double radius){ + this.radius = radius; + } + + public double area(double radius){ + return Math.PI * (radius * radius); + } + + public double girth(double radius){ + return 2 * Math.PI * radius; + } + + DecimalFormat df = new DecimalFormat("0.00"); + public String toString(){ + return "圆的周长为:" + df.format(area(radius)) + " 面积为:" + df.format(girth(radius)); + } +} + +package T1; + +import java.math.RoundingMode; +import java.text.DecimalFormat; + +public class TestRound { + public static void main(String[] args) { + Round r = new Round(); + r.setRadius(2); + System.out.println(r); + + Round r1 = new Round(3); + System.out.println(r1); + } +} + +``` + +```java +package T2; + +public class Student { + private String name; + private double score; + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + + public void setScore(double score){ + this.score = score; + } + + public Student(String name,double score){ + this.name = name; + this.score = score; + } + + public String toString(){ + return "学生的名字为:" + name + " 成绩为:" + score; + } +} + +package T2; + +public class TestStudent { + public static void main(String[] args) { + Student st = new Student(); + st.setName("张三"); + st.setScore(86); + System.out.println(st); + + Student st1 = new Student("李四",85); + System.out.println(st1); + } +} + +``` + +# 第三题 + +```java +package T3; + +import sun.dc.pr.PRError; + +public class Employee { + private String name; + private MyDate birthday; + + public Employee(String name,MyDate birthday){ + this.name = name; + this.birthday = birthday; + } + + public Employee(){ + + } + + public void setName(String name){ + this.name = name; + } + + public void setBirthday(MyDate birthday){ + this.birthday = birthday; + } + + public String toString(){ + return "员工姓名:" + name + " " + birthday ; + } +} + +package T3; + +public class MyDate { + private int year; + private int month; + private int day; + + public MyDate(int year,int month,int day){ + this.year = year; + this.month = month; + this.day = day; + } + + public MyDate(){ + + } + + public void setYear(int year){ + this.year = year; + } + + public void setMonth(int month){ + this.month = month; + } + + public void setDay(int day){ + this.day = day; + } + + public int getYear(){ + return this.year = year; + } + + public int getMonth(){ + return this.month = month; + } + + public int getDay(){ + return this.day = day; + } + + public String toString(){ + return "生日:" + year + "年" + month + "月" + day + "日"; + } +} + +package T3; + +public class TestEmployee { + public static void main(String[] args) { + Employee emp = new Employee(); + emp.setName("张三"); + emp.setBirthday(new MyDate(1997,12,1)); + System.out.println(emp); + + Employee emp2 = new Employee("李四",new MyDate(1998,10,9)); + System.out.println(emp2); + } +} + +``` + +# 第四题 + +```java +package T4; + +import java.text.DecimalFormat; + +public class Round { + private double radius; + + public Round(){ + + } + + public void setRadius(double radius){ + this.radius = radius; + } + + public Round(double radius){ + this.radius = radius; + } + + public double area(double radius){ + return Math.PI * (radius * radius); + } + + public double girth(double radius){ + return 2 * Math.PI * radius; + } + + DecimalFormat df = new DecimalFormat("0.00"); + public String toString(){ + return "圆的周长为:" + df.format(area(radius)) + " 面积为:" + df.format(girth(radius)); + } +} + +package T4; + +public class TestRound { + public static void main(String[] args) { + Round r = new Round(); + r.setRadius(2); + System.out.println(r); + + Round r1 = new Round(3); + System.out.println(r1); + } +} + +``` + +# **第五题** + +```java +package T5; + +public class Student { + private String name; + private double score; + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + + public void setScore(double score){ + this.score = score; + } + + public Student(String name,double score){ + this.name = name; + this.score = score; + } + + public String toString(){ + return "学生的名字为:" + name + " 成绩为:" + score; + } +} + + +package T5; + +public class TestStudent { + public static void main(String[] args) { + Student st = new Student(); + st.setName("张三"); + st.setScore(86); + System.out.println(st); + + Student st1 = new Student("李四",85); + System.out.println(st1); + } +} + +``` + +# 第六题 + +```java +package T6; + +import sun.net.util.IPAddressUtil; + +import java.util.Arrays; + +public class MyLnt { + private int value; + + public MyLnt(){ + + } + + public void setValue(int value){ + this.value = value; + } + + public int getValue(int value){ + return this.value = value; + } + + public MyLnt(int value){ + this.value = value; + } + +// 包含一个方法boolean isNatural()方法, +// 用于判断value属性值是否是自然数。自然数是大于等于0的整数。 + public boolean isNatural(){ + return this.value >= 0; + } + +// 包含一个方法int approximateNumberCount()方法, +// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 + public int approximateNumberCount(){ + int count = 0; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + count++; + } + } + return count; + } + +// 包含一个方法boolean isPrimeNumber()方法, +// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, +// 并且value是大于1的自然数,那么value就是素数。 + public boolean isPrimeNumber(){ + int count = 0; + boolean flag = false; + for (int i = 1; i < value ; i++) { + count++; + if (value % i == 0 && value % 1 == 0 && value > 1){ + if (count >= 3){ + flag = false; + }else { + flag = true; + } + } + } + return flag; + } +// 包含一个方法int[] getAllPrimeNumber()方法, +// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 + public int[] getAllPrimeNumber(){ + int[] arr = new int[approximateNumberCount()]; + int index = arr[0]; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + arr[index++]=i; + } + } + return arr; + } + + @Override + public String toString() { + return "MyInt{" + + "\nvalue=" + value + + "\n是不是自然数:" + isNatural() + + "\n约数个数:" + approximateNumberCount() + + "\n是不是素数:" + isPrimeNumber() + + "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + + "\n}"; + } +} + +package T6; + +public class TestMyLnt { + public static void main(String[] args) { + MyLnt lnt = new MyLnt(20); + System.out.println(lnt); + } +} + +``` + +# 第七题 + +```java +package T7; + +public class TestYue { + public static void main(String[] args) { + Yue y = new Yue(); + int min = y.min(44,33,88,342,454,234,5,2); + System.out.println("最小值为:" + min); + int max = y.maxApproximate(4,8,16,20,24); + System.out.println("最大公约数为:" + max); + } +} + + +package T7; + +public class Yue { +// 返回n个整数中的最小值 + int min(int... nums){ + int min = nums[0]; + for (int i = 0; i < nums.length; i++) { + if (min > nums[i]){ + min = nums[i]; + } + } + return min; + } + + public int maxApproximate(int... nums){ + int max=min(nums); + for (int i = max; i >= 1; i--) { + boolean flag=true; + for (int j = 0; j < nums.length; j++) { + if (nums[j]%i!=0){ + flag=false; + break; + } + } + if (flag==true){ + return i; + } + } + return 1; + } +} + +``` + +# 第八题 + +```java +package T8; + +public class ArraysTools { + public String toString(int[]arr) { + String str = "["; + for (int i = 0; i < arr.length; i++) { + int i1 = arr[i]; + str += i1; + if (i < arr.length - 1) { + str += ","; + } + } + str += "]"; + return str; + } + int[] grow(int[] arr){ + int shu = arr.length; + int[] newArr = new int[2 * shu]; + for (int i = 0; i < arr.length; i++) { + newArr[i] = arr[i]; + } + return newArr; + } +} + +package T8; + +public class Test { + public static void main(String[] args) { + int[]old={11,22,33,44}; + ArraysTools arraysTools = new ArraysTools(); + System.out.println(arraysTools.toString(old)); + int[]newArr= arraysTools.grow(old); + System.out.println(arraysTools.toString(newArr)); + } +} + +``` + +# 第九题 + +```java +package T9; + +public class MathTools { + int compare(int a, int b){ + if (a > b){ + return a; + }else if (a < b){ + return -a; + }else { + return 0; + } + } +/* +* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, +* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; +* */ + int compare(double a, double b){ + if (a > b){ + return (int)a; + }else if (a < b){ + return (int)-a; + }else { + return 0; + } + } + +// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, +// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; + int compare(char a, char b){ + if (a > b){ + return (char)a; + }else if (a < b){ + return (char)-a; + }else { + return 0; + } + } +} + + +package T9; + +public class Test { + public static void main(String[] args) { + MathTools ma = new MathTools(); + int a = ma.compare(3,5); + int b = ma.compare('2','3'); + int c = ma.compare(33.5,22.4); + System.out.println(a); + System.out.println(b); + System.out.println(c); + } +} + +``` + +# 第十题 + +```java +package T10; + +public class ArraysTools { + public void sort(int[] arr){ + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]){ + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public void sort(double[] arr){ + for (int i= 1; i arr[j+1]){ + double temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } + } + } + + public void sort(char[] arr) { + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]) { + char temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public String toString(int[] arr){ + String re="["; + for (int i = 0; i < arr.length; i++) { + if (i我***你 + +##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 + +String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] + +## String方法小结 + +| **方法名** | **说明** | +| ----------------------------------------------------- | -------------------------------- | +| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | +| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | +| public int length() | 返回此字符串的长度 | + +| public char charAt(int index) | 返回指定索引处的 char 值 | +| ----------------------------- | ---------------------------- | +| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | + +| **方法名** | **说明** | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | +| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | +| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | + +| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | +| ----------------------------------- | ---------------------------------------- | +| | | + +## StringBuilder + +StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 + +作用:提高字符串的操作效率 + +### StringBuilder 构造方法 + +| **方法名** | **说明** | +| -------------------------------- | ------------------------------------------ | +| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | +| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | + +### StringBuilder 的常用方法 + +| **方法名** | **说明** | +| -------------------------------------- | ------------------------ | +| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | +| public StringBuilder reverse() | 返回相反的字符序列 | + +| public int length() | 返回长度 ( 字符出现的个数) | +| ------------------- | -------------------------- | +| | | + +| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | +| ------------------------ | --------------------------------------------------- | +| | | + +## StringBuilder和String的区别 + +**String** :内容是不可变的 + +**StringBuilder**:内容是可变的 + +## StringBuilder 和 String 相互转化 + +### StringBuilder 转换为 String + +public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String + +### String 转换为 StringBuilder + +public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder + +# 作业 + +```java +import java.util.Scanner; + +public class Test1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String password = "12345"; + System.out.println("请输入密码登陆"); + int count = 0; + for (int i = 0; i < 3; i++) { + String st = sc.next(); + count++; + if (st.equals(password)){ + System.out.println("密码正确,登陆成功"); + break; + }else { + System.out.println("密码错误"); + System.out.println("您还有" + (3 - count) + "次"); + } + } + } +} + + +import java.util.Scanner; + +public class Test2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入字符串遍历数组"); + String st = sc.next(); + for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ + da++; + } + if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ + xiao++; + } + if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ + shu++; + } + } + System.out.println("大写出现的次数:" + da); + System.out.println("小写出现的次数:" + xiao); + System.out.println("数字出现的次数:" + shu); + } +} + + +import javax.swing.*; +import java.util.Scanner; + +public class Test4 { + public static void main(String[] args) { + System.out.println("请输入电话号码"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.substring(0,3); + String s2 = st.substring(7); + System.out.println("转换后的电话号码为:" + s1 + "****" + s2); + } +} + + +import java.util.Scanner; + +public class Test5 { + public static void main(String[] args) { + System.out.println("请输入脏话"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.replace("TMD","不可以说脏话"); + System.out.println(s1); + } +} + + +public class Student { + private String name; + private int age; + + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } +} + + +import java.util.Arrays; +import java.util.Scanner; + +public class TestStudent6 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Student st = new Student(); + System.out.println("请输入名字"); + st.setName(sc.next()); + System.out.println("请输入年龄"); + st.setAge(sc.nextInt()); + String st2 = st.getName() +","+ st.getAge(); + String[] arr = st2.split(","); + System.out.println(Arrays.toString(arr)); + + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" new file mode 100644 index 0000000..db5bfa5 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" @@ -0,0 +1,205 @@ +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三",18)); + list.add(new Student("李四",19)); + list.add(new Student("王五",20)); + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +//学生的姓名和年龄来自于键盘录入 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} +import java.security.AllPermission; +import java.util.ArrayList; +import java.util.Scanner; + +public class TestStudent2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int count = 0; + ArrayList list = new ArrayList<>(); + while (true) { + count++; + System.out.println("请输入第" + count + "名学生的姓名和年龄"); + list.add(new Student(sc.next(), sc.nextInt())); + if (count == 3){ + break; + } + } + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java + +import java.util.ArrayList; +import java.util.Iterator; + +public class T2 { + public static void main(String[] args) { +// 需求:创建一个存储String的集合,内部存储 +// (test,张三,李四,test,test)字符串 +// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 + ArrayList list = new ArrayList<>(); + list.add("test"); + list.add("张三"); + list.add("李四"); + list.add("test"); + list.add("test"); +// Iterator it = list.iterator(); +// while(it.hasNext()){ +// String str = it.next(); +// if (str.contains("test")){ +// it.remove(); +// } +// } + list.removeIf(s -> s.contains("test")); + System.out.println(list); + } +} + + +``` + +```java +//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 +//并存入新集合对象,方法返回新集合。 +public class Student { + private String name; + private int age; + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } + + + public String toString(){ + return "姓名:" + getName() + " " + "年龄:" + getAge(); + } +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三", 19)); + list.add(new Student("李四", 20)); + list.add(new Student("王五", 10)); + list.add(new Student("赵六", 15)); + chaXun(new ArrayList<>(list)); + } + public static ArrayList chaXun(ArrayList list + ){ + ArrayList list1 = new ArrayList<>(); + for (int i = 0; i < list.size(); i++) { + Student st = list.get(i); + if (st.getAge()<18){ + list1.add(list.get(i)); + } + } + System.out.println(list1); + return list1; + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000..bc280be --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,254 @@ +```java +package Light; + +public class Student { + private String StuID; + private String name; + private int age; + private String sex; + + + + public String getStuID() { + return StuID; + } + + public void setStuID(String stuID) { + StuID = stuID; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Student(){} + public Student(String StuID,String name,int age,String sex){ + this.StuID=StuID; + this.name=name; + this.age=age; + this.sex=sex; + } + + @Override + public String toString() { + return "Student{" + + "StuID='" + StuID + '\'' + + ", name='" + name + '\'' + + ", age=" + age + + ", sex='" + sex + '\'' + + '}'; + } +} +``` + +```java +package Light; + +import java.util.ArrayList; +import java.util.Scanner; + +public class T { + public static void main(String[] args) { + ArrayList list=new ArrayList<>(); + Scanner sc=new Scanner(System.in); + + sum: + for (int i=0;;){ + System.out.println("============学生系统============"); + System.out.println("请输入编号:"+ + "\n\t1 添加学生信息"+ + "\n\t2 修改学生信息"+ + "\n\t3 删除学生信息"+ + "\n\t4 查看学生信息"+ + "\n\t5 退出学生系统"); + System.out.println(); + System.out.print("请输入编号:"); + String sum=sc.next();; + switch (sum){ + case "1": +// System.out.println("添加"); + insertStudent(list,sc); + break; + case"2": + updateStudent(list,sc); + break; + case"3": + deleteStudent(list,sc); + break; + case"4": + lookStudent(list,sc); + break; + case"5": + System.out.println("成功退出学生系统"); + break sum; + default: + System.out.println("输入编号错误,请重新输入!!!"); + System.out.println(); + } + } + } + + + /** + * 添加 + * @param list + * @param sc + */ + public static void insertStudent(ArrayList list, Scanner sc){ + System.out.println(); + System.out.println("===*请输入需要添加学生信息*==="); + System.out.println("请输入学号"); + String stuId=sc.next(); + + if (!list.isEmpty()){ + for (int i = 0; i< list.size(); i++){ + Student stu= list.get(i); + if (stuId.equals(stu.getStuID())){ + System.out.println("学号已经存在"); + break; + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + } + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + System.out.println(); + } + + + /** + * 修改 + * @param list + * @param sc + */ + + public static void updateStudent(ArrayList list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); + }else { + System.out.println("===修改学生信息==="); + System.out.println("请输入需要修改的学生学号"); + String stuId=sc.next(); + for (int i=0;i list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); + }else { + System.out.println("==*删除学生信息*=="); + System.out.println("请输入需要删除的学生学号"); + String stuId=sc.next(); + + for (int i=0;i list,Scanner sc){ + System.out.println(); + + if (list.isEmpty()) { + System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); + }else { + System.out.println("==*查看学生信息*=="); + System.out.println("请输入需要查看学生的学生学号"); + String stuId=sc.next(); + + for (int i=0;i Date: Sat, 22 Apr 2023 18:54:12 +0800 Subject: [PATCH 05/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.en.md | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) create mode 100644 README.en.md diff --git a/README.en.md b/README.en.md new file mode 100644 index 0000000..c05e4d3 --- /dev/null +++ b/README.en.md @@ -0,0 +1,36 @@ +# Java面向对象笔记 + +#### Description +Java面向对象笔记 + +#### Software Architecture +Software architecture description + +#### Installation + +1. xxxx +2. xxxx +3. xxxx + +#### Instructions + +1. xxxx +2. xxxx +3. xxxx + +#### Contribution + +1. Fork the repository +2. Create Feat_xxx branch +3. Commit your code +4. Create Pull Request + + +#### Gitee Feature + +1. You can use Readme\_XXX.md to support different languages, such as Readme\_en.md, Readme\_zh.md +2. Gitee blog [blog.gitee.com](https://blog.gitee.com) +3. Explore open source project [https://gitee.com/explore](https://gitee.com/explore) +4. The most valuable open source project [GVP](https://gitee.com/gvp) +5. The manual of Gitee [https://gitee.com/help](https://gitee.com/help) +6. The most popular members [https://gitee.com/gitee-stars/](https://gitee.com/gitee-stars/) -- Gitee From 52d6a569e1fb69d8fb196f11a65f5f72094022f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0?= <3023096670@qq.com> Date: Sat, 22 Apr 2023 18:58:10 +0800 Subject: [PATCH 06/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0=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" | 520 +++---- ...55\346\263\225\350\256\255\347\273\203.md" | 298 ++-- ...60\347\273\204\344\275\234\344\270\232.md" | 296 ++-- ...71\350\261\241\344\275\234\344\270\232.md" | 94 +- ...32\345\222\214\347\254\224\350\256\260.md" | 260 ++-- .../20230414 java\344\275\234\344\270\232.md" | 1364 ++++++++--------- ...32\345\222\214\347\254\224\350\256\260.md" | 580 +++---- ...06\345\220\210\344\275\234\344\270\232.md" | 410 ++--- ...41\347\220\206\347\263\273\347\273\237.md" | 506 +++--- README.en.md | 36 - 10 files changed, 2164 insertions(+), 2200 deletions(-) delete mode 100644 README.en.md diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" index 63c6bc5..5000ca1 100644 --- "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" +++ "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -1,260 +1,260 @@ -```java -import java.util.Scanner; - -public class Class { -// **1、判断一个字符数据是否是数字字符 ** -// -// **分析:** -// -// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 -// -// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, -// 并且还要下于或等于字符9即可。 -// -// 3、判断完成之后,打印判断的结果。 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - char i = sc.next.charAt(0); - if (i>=0 && i<10){ - System.out.println("是数字字符"); - }else { - System.out.println("不是数字字符"); - } - } -} - -import java.util.Scanner; - -public class java2 { - /* **2、判断一个字符数据是否是字母字符** - - **分析:** - - 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 - - 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, - 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z - - 3、判断完成之后,打印判断的结果。*/ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - char st = sc.next().charAt(0); - - if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) - - System.out.println( "是字母"); - - else - - System.out.println("不是字母"); - - } -} - - -import java.util.Scanner; - -public class java3 { -/* - **3、判断指定的年份是否为闰年,请使用键盘录入** - - **分析:** - - 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 - - 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 - -*/ - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int i = sc.nextInt(); - if ((i % 4 == 0 && i% 100 ==0) || i % 400 != 0 ){ - System.out.println("是闰年"); - }else{ - System.out.println("不是闰年"); - } - } -} - - -import java.util.Scanner; - -public class java4 { - // **4、判断一个数字是否为水仙花数,请使用键盘录入** -// -// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, -// -// **分析:** -// -// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 -// -// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 -// -// 2、判断的过程 -// -// a) 将3位数字的每一位上的数字拆分下来 -// -// b) 计算每位数字的3次幂之和 -// -// C) 用和值 和 原来的数字进行比较 -// -// D) 打印判断的比较结果即可 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入一个数字判断是不是水仙花数"); - int i = sc.nextInt(); - int ge = i % 10; - int si = i / 10 % 10; - int bai = i / 100 % 10; - if (i > 99 && i < 1000) { - if (ge * ge * ge + si * si * si + bai * bai * bai == i) { - System.out.println("是水仙花数"); - } else { - System.out.println("不是水仙花数"); - } - }else { - System.out.println("您好,水仙花数是三位数"); - } - } -} - - -import java.util.Scanner; - -public class java5 { - /* **5、判断一个5位数字是否为回文数,使用键盘录入** - - 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 - - **分析:** - - 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 - - 2、判断的过程 - - a) 将5位数字的万、千、十、个位数拆分出来 - - b) 判断比较万位和个位 、 千位和十位是否相等 - - 3、判断完成之后,打印判断的结果。*/ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入五位数判断是不是回文数"); - int i = sc.nextInt(); - int ge = i % 10; - int si = i / 10 % 10; - int qian = i / 1000 % 10; - int wan = i / 10000 % 10; - if (i > 9999 && i < 100000) { - if (ge == wan && si == qian) { - System.out.println("是回文数"); - } else { - System.out.println("不是回文数"); - } - }else { - System.out.println("您输入的不是五位数"); - } - } -} - - -import java.util.Scanner; - -public class java6 { -// ## 题目1(训练) -// -// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: -// -// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 -// -// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 -// -// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? -// -// ### 训练提示 -// -//1. 已知的父母身高如何用代码体现? -// -// 2. 题目中的公式如何转化为代码? -// -// ### 解题方案 -// -//1. 使用变量的定义和算术运算符完成本题 -// -//### 操作步骤 -// -//1. 定义小数变量代表父亲身高 -// -//2. 定义小数变量代表母亲身高 -// -//3. 通过儿子身高计算方式计算儿子身高 -// -//4. 通过女儿身高计算方式计算女人身高 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入父亲的身高"); - double baba = sc.nextInt(); - System.out.println("请输入母亲的身高"); - double mama = sc.nextInt(); - double erzi = (baba + mama) * 1.08 / 2; - double nver = (baba * 0.923 + mama) / 2; - System.out.println("儿子的身高为:" + erzi); - System.out.println("女儿的身高为:" + nver); - } -} - -public class java7 { -// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 -// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 -// 那么红茶和绿茶现在的钱一样多,请问对么? - public static void main(String[] args) { - int hocha = 21; - int lvcha = 24; - hocha = hocha * 2 + 3; - lvcha = lvcha * 2; - if (hocha == lvcha){ - System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); - }if (hocha > lvcha){ - System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); - }if (hocha < lvcha){ - System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); - } - } -} - - -public class java8 { -// 某小伙想定一份外卖,商家的优惠方式如下: -// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, -// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? - /*训练提示 - 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 - 解题方案 - 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 - 操作步骤 - 1. 使用算术运算符求出不使用优惠时的总价 - 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 - 3. 使用算术运算符求出使用优惠价时的总价 - 4. 使用三元运算符判断最终更合算的购买方式和花费*/ - public static void main(String[] args) { - int yuxian = 24; - int youza = 8; - int mifan = 3; - int zojia = yuxian + youza + mifan; - double youhuijia = zojia * 0.8; - System.out.println("使用折扣的总价:" + youhuijia); - int yuxian2 = 16; - int youza2 = 8; - int mifan2 = 3; - int zojia2 = yuxian2 + youza2 + mifan2; - System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); - if (youhuijia < zojia2){ - System.out.println("使用这个:" + youhuijia ); - }else if (youhuijia > zojia2){ - System.out.println("使用这个:" + zojia2); - } - } -} -``` - +```java +import java.util.Scanner; + +public class Class { +// **1、判断一个字符数据是否是数字字符 ** +// +// **分析:** +// +// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 +// +// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, +// 并且还要下于或等于字符9即可。 +// +// 3、判断完成之后,打印判断的结果。 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char i = sc.next.charAt(0); + if (i>=0 && i<10){ + System.out.println("是数字字符"); + }else { + System.out.println("不是数字字符"); + } + } +} + +import java.util.Scanner; + +public class java2 { + /* **2、判断一个字符数据是否是字母字符** + + **分析:** + + 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 + + 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, + 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char st = sc.next().charAt(0); + + if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) + + System.out.println( "是字母"); + + else + + System.out.println("不是字母"); + + } +} + + +import java.util.Scanner; + +public class java3 { +/* + **3、判断指定的年份是否为闰年,请使用键盘录入** + + **分析:** + + 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 + + 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 + +*/ + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int i = sc.nextInt(); + if ((i % 4 == 0 && i% 100 ==0) || i % 400 != 0 ){ + System.out.println("是闰年"); + }else{ + System.out.println("不是闰年"); + } + } +} + + +import java.util.Scanner; + +public class java4 { + // **4、判断一个数字是否为水仙花数,请使用键盘录入** +// +// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, +// +// **分析:** +// +// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 +// +// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 +// +// 2、判断的过程 +// +// a) 将3位数字的每一位上的数字拆分下来 +// +// b) 计算每位数字的3次幂之和 +// +// C) 用和值 和 原来的数字进行比较 +// +// D) 打印判断的比较结果即可 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个数字判断是不是水仙花数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int bai = i / 100 % 10; + if (i > 99 && i < 1000) { + if (ge * ge * ge + si * si * si + bai * bai * bai == i) { + System.out.println("是水仙花数"); + } else { + System.out.println("不是水仙花数"); + } + }else { + System.out.println("您好,水仙花数是三位数"); + } + } +} + + +import java.util.Scanner; + +public class java5 { + /* **5、判断一个5位数字是否为回文数,使用键盘录入** + + 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 + + **分析:** + + 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 + + 2、判断的过程 + + a) 将5位数字的万、千、十、个位数拆分出来 + + b) 判断比较万位和个位 、 千位和十位是否相等 + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入五位数判断是不是回文数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int qian = i / 1000 % 10; + int wan = i / 10000 % 10; + if (i > 9999 && i < 100000) { + if (ge == wan && si == qian) { + System.out.println("是回文数"); + } else { + System.out.println("不是回文数"); + } + }else { + System.out.println("您输入的不是五位数"); + } + } +} + + +import java.util.Scanner; + +public class java6 { +// ## 题目1(训练) +// +// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: +// +// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 +// +// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 +// +// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? +// +// ### 训练提示 +// +//1. 已知的父母身高如何用代码体现? +// +// 2. 题目中的公式如何转化为代码? +// +// ### 解题方案 +// +//1. 使用变量的定义和算术运算符完成本题 +// +//### 操作步骤 +// +//1. 定义小数变量代表父亲身高 +// +//2. 定义小数变量代表母亲身高 +// +//3. 通过儿子身高计算方式计算儿子身高 +// +//4. 通过女儿身高计算方式计算女人身高 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入父亲的身高"); + double baba = sc.nextInt(); + System.out.println("请输入母亲的身高"); + double mama = sc.nextInt(); + double erzi = (baba + mama) * 1.08 / 2; + double nver = (baba * 0.923 + mama) / 2; + System.out.println("儿子的身高为:" + erzi); + System.out.println("女儿的身高为:" + nver); + } +} + +public class java7 { +// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 +// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 +// 那么红茶和绿茶现在的钱一样多,请问对么? + public static void main(String[] args) { + int hocha = 21; + int lvcha = 24; + hocha = hocha * 2 + 3; + lvcha = lvcha * 2; + if (hocha == lvcha){ + System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); + }if (hocha > lvcha){ + System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + }if (hocha < lvcha){ + System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + } + } +} + + +public class java8 { +// 某小伙想定一份外卖,商家的优惠方式如下: +// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, +// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? + /*训练提示 + 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 + 解题方案 + 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 + 操作步骤 + 1. 使用算术运算符求出不使用优惠时的总价 + 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 + 3. 使用算术运算符求出使用优惠价时的总价 + 4. 使用三元运算符判断最终更合算的购买方式和花费*/ + public static void main(String[] args) { + int yuxian = 24; + int youza = 8; + int mifan = 3; + int zojia = yuxian + youza + mifan; + double youhuijia = zojia * 0.8; + System.out.println("使用折扣的总价:" + youhuijia); + int yuxian2 = 16; + int youza2 = 8; + int mifan2 = 3; + int zojia2 = yuxian2 + youza2 + mifan2; + System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); + if (youhuijia < zojia2){ + System.out.println("使用这个:" + youhuijia ); + }else if (youhuijia > zojia2){ + System.out.println("使用这个:" + zojia2); + } + } +} +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" index d8f1fa4..c7f47ac 100644 --- "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" +++ "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" @@ -1,149 +1,149 @@ -```java -package My; - -import javax.lang.model.SourceVersion; - -public class Class { - public static void main(String[] args) { - // 第一题 - //(1)先声明两个byte类型的变量b1,b2, - // 并分别赋值为10和20,求b1和b2变量的和, - // 并将结果保存在byte类型的变量b3中, - // 最后输出b3变量的值 - byte b1,b2; - b1=10; - b2=20; - byte b3=(byte)(b1+b2); - System.out.println("byte类型的b1和b2的和为:"+(b3)); - -// 第二题 -// (2)先声明两个short类型的变量s1,s2, -// 并分别赋值为1000和2000,求s1和s2变量的和, -// 并将结果保存在short类型的变量s3中, -// 最后输出s3变量的值 - short s1=1000,s2=2000; - short s3=(short)(s1+s2); - System.out.println("short类型的是s1和s2的和为:"+s3); - -// 第三题 -// (3)先声明1个char类型的变量c1赋值为'a', -// 再声明一个int类型的变量num赋值为5,求c1和num变量的和, -// 并将结果将结果保存在char类型的变量letter中, -// 最后输出letter变量的值。 - char c1='a'; - int num=5; - char letter=(char)(c1+(char)num); - System.out.println("char类型的c1和int类型的num的和:"+(letter)); - -// 第四题 -// (4)先声明两个int类型的变量i1,i2, -// 并分别赋值5和2,求i1和i2的商, -// 并将结果保存在double类型的变量result中, -// 最后输出result变量的值。如何得到结果2.5呢? - int i1=5,i2=2; - double result=(double) i1/i2; - System.out.println("i1和i2的商为:"+(result)); - } -} - - -package My; - -public class Class1 { - public static void main(String[] args) { - int a1=10,a2=11; - System.out.println("10是偶数?"+(a1%2==0)); - System.out.println("11是偶数?"+(a2%2==0)); - int a3=12,a4=13; - System.out.println("12是奇数?"+(a3%2!=0)); - System.out.println("13是奇数?"+(a4%2!=0)); - } -} - -package My; - -public class Class3 { - public static void main(String[] args) { -// 题目:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时? - int hours=89; - int day=hours/24; - int hour=hours%24; - System.out.println("为抵抗洪水,战士连续作战89小时:"+"89小时是"+(day)+"天"+(hour)+"小时"); - - } -} - -package My; - -public class Class4 { - public static void main(String[] args) { -// 题目:今天是周2,100天以后是周几? - int week=2; - week+=100; - week%=7; - System.out.println("今天是周二,100天以后是周"+(week)); - } -} - -package My; - -import java.util.Scanner; - -public class Class2 { - public static void main(String[] args) { -// ## 5、案例:求三个整数x,y,z中的最大值 -// -// -// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 -// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) -// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) -// 4. 输出结果 - Scanner sc=new Scanner(System.in); - System.out.print("请输入x的值:"); - int x=sc.nextInt(); - System.out.print("请输入y的值:"); - int y=sc.nextInt(); - System.out.print("请输入z的值:"); - int z=sc.nextInt(); - int max; - max=(x>y)?x:y; - max=(max>z)?max:z; - System.out.println(max); - } -} - - - -package My; - -public class Class5 { - public static void main(String[] args) { -// 题目:判断今年是否是闰年 - int year=2023; - boolean a=(year%4==0 && year%100!=0); - boolean b=(year%400==0); - if(a || b){ - System.out.println(year+"是闰年"); - }else { - System.out.println(year + "不是闰年"); - } - } -} - - -package My; - -public class Class6 { - public static void main(String[] args) { -// 题目:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。 -// 它需要一个程序将华氏温度(80度)转换为摄氏度, -// 并以华氏度和摄氏度为单位分别显示该温度。 - double hua=80; - double she; - she=(hua-32)/1.8; - System.out.println("华氏度80.0度转化为摄氏度是"+(she)+"度"); - } -} - -``` - +```java +package My; + +import javax.lang.model.SourceVersion; + +public class Class { + public static void main(String[] args) { + // 第一题 + //(1)先声明两个byte类型的变量b1,b2, + // 并分别赋值为10和20,求b1和b2变量的和, + // 并将结果保存在byte类型的变量b3中, + // 最后输出b3变量的值 + byte b1,b2; + b1=10; + b2=20; + byte b3=(byte)(b1+b2); + System.out.println("byte类型的b1和b2的和为:"+(b3)); + +// 第二题 +// (2)先声明两个short类型的变量s1,s2, +// 并分别赋值为1000和2000,求s1和s2变量的和, +// 并将结果保存在short类型的变量s3中, +// 最后输出s3变量的值 + short s1=1000,s2=2000; + short s3=(short)(s1+s2); + System.out.println("short类型的是s1和s2的和为:"+s3); + +// 第三题 +// (3)先声明1个char类型的变量c1赋值为'a', +// 再声明一个int类型的变量num赋值为5,求c1和num变量的和, +// 并将结果将结果保存在char类型的变量letter中, +// 最后输出letter变量的值。 + char c1='a'; + int num=5; + char letter=(char)(c1+(char)num); + System.out.println("char类型的c1和int类型的num的和:"+(letter)); + +// 第四题 +// (4)先声明两个int类型的变量i1,i2, +// 并分别赋值5和2,求i1和i2的商, +// 并将结果保存在double类型的变量result中, +// 最后输出result变量的值。如何得到结果2.5呢? + int i1=5,i2=2; + double result=(double) i1/i2; + System.out.println("i1和i2的商为:"+(result)); + } +} + + +package My; + +public class Class1 { + public static void main(String[] args) { + int a1=10,a2=11; + System.out.println("10是偶数?"+(a1%2==0)); + System.out.println("11是偶数?"+(a2%2==0)); + int a3=12,a4=13; + System.out.println("12是奇数?"+(a3%2!=0)); + System.out.println("13是奇数?"+(a4%2!=0)); + } +} + +package My; + +public class Class3 { + public static void main(String[] args) { +// 题目:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时? + int hours=89; + int day=hours/24; + int hour=hours%24; + System.out.println("为抵抗洪水,战士连续作战89小时:"+"89小时是"+(day)+"天"+(hour)+"小时"); + + } +} + +package My; + +public class Class4 { + public static void main(String[] args) { +// 题目:今天是周2,100天以后是周几? + int week=2; + week+=100; + week%=7; + System.out.println("今天是周二,100天以后是周"+(week)); + } +} + +package My; + +import java.util.Scanner; + +public class Class2 { + public static void main(String[] args) { +// ## 5、案例:求三个整数x,y,z中的最大值 +// +// +// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 +// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) +// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) +// 4. 输出结果 + Scanner sc=new Scanner(System.in); + System.out.print("请输入x的值:"); + int x=sc.nextInt(); + System.out.print("请输入y的值:"); + int y=sc.nextInt(); + System.out.print("请输入z的值:"); + int z=sc.nextInt(); + int max; + max=(x>y)?x:y; + max=(max>z)?max:z; + System.out.println(max); + } +} + + + +package My; + +public class Class5 { + public static void main(String[] args) { +// 题目:判断今年是否是闰年 + int year=2023; + boolean a=(year%4==0 && year%100!=0); + boolean b=(year%400==0); + if(a || b){ + System.out.println(year+"是闰年"); + }else { + System.out.println(year + "不是闰年"); + } + } +} + + +package My; + +public class Class6 { + public static void main(String[] args) { +// 题目:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。 +// 它需要一个程序将华氏温度(80度)转换为摄氏度, +// 并以华氏度和摄氏度为单位分别显示该温度。 + double hua=80; + double she; + she=(hua-32)/1.8; + System.out.println("华氏度80.0度转化为摄氏度是"+(she)+"度"); + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" index dc4fd7e..de5fc8d 100644 --- "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" +++ "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" @@ -1,148 +1,148 @@ -```java -public class T1 { - public static void main(String[] args) { -// .定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** -// -//**操作步骤:** -// -// 1.定义5个元素数组 -// 2.可以使用初始化数组的两种方式之一为数组元素赋值 -// 3.遍历数组求数组中的最小值 - - - int[] sum={12,32,52,2,5};//创立数组并赋值 - - int min=sum[0]; - for (int i=1;isum[i]){ - min=sum[i]; - } - } - System.out.println("数组sum的最小值为:"+min); - } -} -``` - - - -```java -package Glass; - -import java.util.Arrays; - -public class T2 { - public static void main(String[] args) { -// 需求:求出数组中索引与索引对应的元素都是奇数的元素** -// -//**分析:** -// -// 1、遍历数组 -// 2、判断索引是否是奇数(索引 % 2 != 0) -// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) -// 4、满足条件输出结果 - - int[] sum={12,4,32,6,32,11}; - System.out.println(Arrays.toString(sum)); - - for (int i=0;isum[i]){ + min=sum[i]; + } + } + System.out.println("数组sum的最小值为:"+min); + } +} +``` + + + +```java +package Glass; + +import java.util.Arrays; + +public class T2 { + public static void main(String[] args) { +// 需求:求出数组中索引与索引对应的元素都是奇数的元素** +// +//**分析:** +// +// 1、遍历数组 +// 2、判断索引是否是奇数(索引 % 2 != 0) +// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) +// 4、满足条件输出结果 + + int[] sum={12,4,32,6,32,11}; + System.out.println(Arrays.toString(sum)); + + for (int i=0;i= 0; - } - -// 包含一个方法int approximateNumberCount()方法, -// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 - public int approximateNumberCount(){ - int count = 0; - for (int i = 1; i <= value; i++) { - if (value % i == 0){ - count++; - } - } - return count; - } - -// 包含一个方法boolean isPrimeNumber()方法, -// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, -// 并且value是大于1的自然数,那么value就是素数。 - public boolean isPrimeNumber(){ - int count = 0; - boolean flag = false; - for (int i = 1; i < value ; i++) { - count++; - if (value % i == 0 && value % 1 == 0 && value > 1){ - if (count >= 3){ - flag = false; - }else { - flag = true; - } - } - } - return flag; - } -// 包含一个方法int[] getAllPrimeNumber()方法, -// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 - public int[] getAllPrimeNumber(){ - int[] arr = new int[approximateNumberCount()]; - int index = arr[0]; - for (int i = 1; i <= value; i++) { - if (value % i == 0){ - arr[index++]=i; - } - } - return arr; - } - - @Override - public String toString() { - return "MyInt{" + - "\nvalue=" + value + - "\n是不是自然数:" + isNatural() + - "\n约数个数:" + approximateNumberCount() + - "\n是不是素数:" + isPrimeNumber() + - "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + - "\n}"; - } -} - -package T6; - -public class TestMyLnt { - public static void main(String[] args) { - MyLnt lnt = new MyLnt(20); - System.out.println(lnt); - } -} - -``` - -# 第七题 - -```java -package T7; - -public class TestYue { - public static void main(String[] args) { - Yue y = new Yue(); - int min = y.min(44,33,88,342,454,234,5,2); - System.out.println("最小值为:" + min); - int max = y.maxApproximate(4,8,16,20,24); - System.out.println("最大公约数为:" + max); - } -} - - -package T7; - -public class Yue { -// 返回n个整数中的最小值 - int min(int... nums){ - int min = nums[0]; - for (int i = 0; i < nums.length; i++) { - if (min > nums[i]){ - min = nums[i]; - } - } - return min; - } - - public int maxApproximate(int... nums){ - int max=min(nums); - for (int i = max; i >= 1; i--) { - boolean flag=true; - for (int j = 0; j < nums.length; j++) { - if (nums[j]%i!=0){ - flag=false; - break; - } - } - if (flag==true){ - return i; - } - } - return 1; - } -} - -``` - -# 第八题 - -```java -package T8; - -public class ArraysTools { - public String toString(int[]arr) { - String str = "["; - for (int i = 0; i < arr.length; i++) { - int i1 = arr[i]; - str += i1; - if (i < arr.length - 1) { - str += ","; - } - } - str += "]"; - return str; - } - int[] grow(int[] arr){ - int shu = arr.length; - int[] newArr = new int[2 * shu]; - for (int i = 0; i < arr.length; i++) { - newArr[i] = arr[i]; - } - return newArr; - } -} - -package T8; - -public class Test { - public static void main(String[] args) { - int[]old={11,22,33,44}; - ArraysTools arraysTools = new ArraysTools(); - System.out.println(arraysTools.toString(old)); - int[]newArr= arraysTools.grow(old); - System.out.println(arraysTools.toString(newArr)); - } -} - -``` - -# 第九题 - -```java -package T9; - -public class MathTools { - int compare(int a, int b){ - if (a > b){ - return a; - }else if (a < b){ - return -a; - }else { - return 0; - } - } -/* -* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, -* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; -* */ - int compare(double a, double b){ - if (a > b){ - return (int)a; - }else if (a < b){ - return (int)-a; - }else { - return 0; - } - } - -// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, -// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; - int compare(char a, char b){ - if (a > b){ - return (char)a; - }else if (a < b){ - return (char)-a; - }else { - return 0; - } - } -} - - -package T9; - -public class Test { - public static void main(String[] args) { - MathTools ma = new MathTools(); - int a = ma.compare(3,5); - int b = ma.compare('2','3'); - int c = ma.compare(33.5,22.4); - System.out.println(a); - System.out.println(b); - System.out.println(c); - } -} - -``` - -# 第十题 - -```java -package T10; - -public class ArraysTools { - public void sort(int[] arr){ - for (int i = 1; i < arr.length; i++) { - for (int j = 0; j < arr.length - i; j++) { - if (arr[j] > arr[j + 1]){ - int temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } - public void sort(double[] arr){ - for (int i= 1; i arr[j+1]){ - double temp=arr[j]; - arr[j]=arr[j+1]; - arr[j+1]=temp; - } - } - } - } - - public void sort(char[] arr) { - for (int i = 1; i < arr.length; i++) { - for (int j = 0; j < arr.length - i; j++) { - if (arr[j] > arr[j + 1]) { - char temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } - public String toString(int[] arr){ - String re="["; - for (int i = 0; i < arr.length; i++) { - if (i= 0; + } + +// 包含一个方法int approximateNumberCount()方法, +// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 + public int approximateNumberCount(){ + int count = 0; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + count++; + } + } + return count; + } + +// 包含一个方法boolean isPrimeNumber()方法, +// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, +// 并且value是大于1的自然数,那么value就是素数。 + public boolean isPrimeNumber(){ + int count = 0; + boolean flag = false; + for (int i = 1; i < value ; i++) { + count++; + if (value % i == 0 && value % 1 == 0 && value > 1){ + if (count >= 3){ + flag = false; + }else { + flag = true; + } + } + } + return flag; + } +// 包含一个方法int[] getAllPrimeNumber()方法, +// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 + public int[] getAllPrimeNumber(){ + int[] arr = new int[approximateNumberCount()]; + int index = arr[0]; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + arr[index++]=i; + } + } + return arr; + } + + @Override + public String toString() { + return "MyInt{" + + "\nvalue=" + value + + "\n是不是自然数:" + isNatural() + + "\n约数个数:" + approximateNumberCount() + + "\n是不是素数:" + isPrimeNumber() + + "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + + "\n}"; + } +} + +package T6; + +public class TestMyLnt { + public static void main(String[] args) { + MyLnt lnt = new MyLnt(20); + System.out.println(lnt); + } +} + +``` + +# 第七题 + +```java +package T7; + +public class TestYue { + public static void main(String[] args) { + Yue y = new Yue(); + int min = y.min(44,33,88,342,454,234,5,2); + System.out.println("最小值为:" + min); + int max = y.maxApproximate(4,8,16,20,24); + System.out.println("最大公约数为:" + max); + } +} + + +package T7; + +public class Yue { +// 返回n个整数中的最小值 + int min(int... nums){ + int min = nums[0]; + for (int i = 0; i < nums.length; i++) { + if (min > nums[i]){ + min = nums[i]; + } + } + return min; + } + + public int maxApproximate(int... nums){ + int max=min(nums); + for (int i = max; i >= 1; i--) { + boolean flag=true; + for (int j = 0; j < nums.length; j++) { + if (nums[j]%i!=0){ + flag=false; + break; + } + } + if (flag==true){ + return i; + } + } + return 1; + } +} + +``` + +# 第八题 + +```java +package T8; + +public class ArraysTools { + public String toString(int[]arr) { + String str = "["; + for (int i = 0; i < arr.length; i++) { + int i1 = arr[i]; + str += i1; + if (i < arr.length - 1) { + str += ","; + } + } + str += "]"; + return str; + } + int[] grow(int[] arr){ + int shu = arr.length; + int[] newArr = new int[2 * shu]; + for (int i = 0; i < arr.length; i++) { + newArr[i] = arr[i]; + } + return newArr; + } +} + +package T8; + +public class Test { + public static void main(String[] args) { + int[]old={11,22,33,44}; + ArraysTools arraysTools = new ArraysTools(); + System.out.println(arraysTools.toString(old)); + int[]newArr= arraysTools.grow(old); + System.out.println(arraysTools.toString(newArr)); + } +} + +``` + +# 第九题 + +```java +package T9; + +public class MathTools { + int compare(int a, int b){ + if (a > b){ + return a; + }else if (a < b){ + return -a; + }else { + return 0; + } + } +/* +* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, +* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; +* */ + int compare(double a, double b){ + if (a > b){ + return (int)a; + }else if (a < b){ + return (int)-a; + }else { + return 0; + } + } + +// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, +// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; + int compare(char a, char b){ + if (a > b){ + return (char)a; + }else if (a < b){ + return (char)-a; + }else { + return 0; + } + } +} + + +package T9; + +public class Test { + public static void main(String[] args) { + MathTools ma = new MathTools(); + int a = ma.compare(3,5); + int b = ma.compare('2','3'); + int c = ma.compare(33.5,22.4); + System.out.println(a); + System.out.println(b); + System.out.println(c); + } +} + +``` + +# 第十题 + +```java +package T10; + +public class ArraysTools { + public void sort(int[] arr){ + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]){ + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public void sort(double[] arr){ + for (int i= 1; i arr[j+1]){ + double temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } + } + } + + public void sort(char[] arr) { + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]) { + char temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public String toString(int[] arr){ + String re="["; + for (int i = 0; i < arr.length; i++) { + if (i我***你 - -##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 - -String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] - -## String方法小结 - -| **方法名** | **说明** | -| ----------------------------------------------------- | -------------------------------- | -| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | -| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | -| public int length() | 返回此字符串的长度 | - -| public char charAt(int index) | 返回指定索引处的 char 值 | -| ----------------------------- | ---------------------------- | -| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | - -| **方法名** | **说明** | -| ------------------------------------------------------------ | ------------------------------------------------------------ | -| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | -| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | -| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | - -| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | -| ----------------------------------- | ---------------------------------------- | -| | | - -## StringBuilder - -StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 - -作用:提高字符串的操作效率 - -### StringBuilder 构造方法 - -| **方法名** | **说明** | -| -------------------------------- | ------------------------------------------ | -| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | -| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | - -### StringBuilder 的常用方法 - -| **方法名** | **说明** | -| -------------------------------------- | ------------------------ | -| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | -| public StringBuilder reverse() | 返回相反的字符序列 | - -| public int length() | 返回长度 ( 字符出现的个数) | -| ------------------- | -------------------------- | -| | | - -| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | -| ------------------------ | --------------------------------------------------- | -| | | - -## StringBuilder和String的区别 - -**String** :内容是不可变的 - -**StringBuilder**:内容是可变的 - -## StringBuilder 和 String 相互转化 - -### StringBuilder 转换为 String - -public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String - -### String 转换为 StringBuilder - -public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder - -# 作业 - -```java -import java.util.Scanner; - -public class Test1 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - String password = "12345"; - System.out.println("请输入密码登陆"); - int count = 0; - for (int i = 0; i < 3; i++) { - String st = sc.next(); - count++; - if (st.equals(password)){ - System.out.println("密码正确,登陆成功"); - break; - }else { - System.out.println("密码错误"); - System.out.println("您还有" + (3 - count) + "次"); - } - } - } -} - - -import java.util.Scanner; - -public class Test2 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入字符串遍历数组"); - String st = sc.next(); - for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ - da++; - } - if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ - xiao++; - } - if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ - shu++; - } - } - System.out.println("大写出现的次数:" + da); - System.out.println("小写出现的次数:" + xiao); - System.out.println("数字出现的次数:" + shu); - } -} - - -import javax.swing.*; -import java.util.Scanner; - -public class Test4 { - public static void main(String[] args) { - System.out.println("请输入电话号码"); - Scanner sc = new Scanner(System.in); - String st = sc.next(); - String s1 = st.substring(0,3); - String s2 = st.substring(7); - System.out.println("转换后的电话号码为:" + s1 + "****" + s2); - } -} - - -import java.util.Scanner; - -public class Test5 { - public static void main(String[] args) { - System.out.println("请输入脏话"); - Scanner sc = new Scanner(System.in); - String st = sc.next(); - String s1 = st.replace("TMD","不可以说脏话"); - System.out.println(s1); - } -} - - -public class Student { - private String name; - private int age; - - public Student(String name,int age){ - this.name = name; - this.age = age; - } - - public Student(){ - - } - - public void setName(String name){ - this.name = name; - } - - public void setAge(int age){ - this.age = age; - } - - public String getName(){ - return this.name = name; - } - - public int getAge(){ - return this.age = age; - } -} - - -import java.util.Arrays; -import java.util.Scanner; - -public class TestStudent6 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - Student st = new Student(); - System.out.println("请输入名字"); - st.setName(sc.next()); - System.out.println("请输入年龄"); - st.setAge(sc.nextInt()); - String st2 = st.getName() +","+ st.getAge(); - String[] arr = st2.split(","); - System.out.println(Arrays.toString(arr)); - - } -} - -``` - +## API + +Java API :指的就是 JDK 中提供的各种功能的 Java类 + +#### 好处:需要用的时候不用自己写方法,可以直接调用 + +调用扫描器也是属于API的使用 + +## String + +String 类代表**字符串**,Java 程序中所有的**双引号**字符串,都是 String 类的对象 + +### 重点:字符串不可变,它们的值在创建后不能被更改 + +### 虽然 String 的值是不可变的,但是它们可以被共享 + +例如: String a="wwb"; a="www"; 以上这个案例,他并不是将字符串的值进行了改变,他只是调用了另一个值,而原本的值还是存在的 + +## String 常见构造方法 + +| **方法名** | **说明** | +| ------------------------------ | ----------------------------------------- | +| public String() | 创建一个空白字符串对象,不含有任何内容 | +| public String(char[] chs) | 根据字符数组的内容,来创建字符串对象 | +| public String(String original) | 根据传入的字符串内容,来创建字符串对象 | +| String s = “abc”; | 直接赋值的方式创建字符串对象,内容就是abc | + +#### 字符串常量池:当使用双引号创建字符串对象的时候,系统会检查该字符串是否在字符串常量池中存在 + +#### 不存在:创建 + +#### 存在:不会重新创建,而是直接复用 + +#### 通过 new 创建的字符串对象,每一次 new 都会申请一个内存空间,虽然内容相同,但是地址值不同 ----- 每一次new的时候都会有一个新的地址 + +#### 当字符串之间使用 + 号串联(拼接)的时候,系统底层会自动创建一个StringBuilder对象然后再调用其append方法完成拼接拼接后,再调用其toString方法转换为String类型 + +#### Java存在常量优化机制,在编译的时候,就会将"a" + "b" + "c" 拼接为 "abc" + +## 字符串的比较 + +使用 == 做比较 + +基本类型:比较的是数据值是否相同 + +引用类型:比较的是地址值是否相同 + +#### 字符串是对象,它比较内容是否相同,是通过一个方法来实现的,这个方法叫:equals() + +例如: String a="wdf"; String b="wdf"; //判断a是不是跟b相等 if(a.equals(b)){ + +} + +##### public char charAt(int index):返回指定索引处的char值,字符串的索引也是从0开始的 + +##### public int length():返回此字符串的长度 + +例如: String char1="wewef"; for (int i = 0; i 我***你 + +##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 + +String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] + +## String方法小结 + +| **方法名** | **说明** | +| ----------------------------------------------------- | -------------------------------- | +| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | +| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | +| public int length() | 返回此字符串的长度 | + +| public char charAt(int index) | 返回指定索引处的 char 值 | +| ----------------------------- | ---------------------------- | +| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | + +| **方法名** | **说明** | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | +| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | +| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | + +| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | +| ----------------------------------- | ---------------------------------------- | +| | | + +## StringBuilder + +StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 + +作用:提高字符串的操作效率 + +### StringBuilder 构造方法 + +| **方法名** | **说明** | +| -------------------------------- | ------------------------------------------ | +| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | +| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | + +### StringBuilder 的常用方法 + +| **方法名** | **说明** | +| -------------------------------------- | ------------------------ | +| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | +| public StringBuilder reverse() | 返回相反的字符序列 | + +| public int length() | 返回长度 ( 字符出现的个数) | +| ------------------- | -------------------------- | +| | | + +| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | +| ------------------------ | --------------------------------------------------- | +| | | + +## StringBuilder和String的区别 + +**String** :内容是不可变的 + +**StringBuilder**:内容是可变的 + +## StringBuilder 和 String 相互转化 + +### StringBuilder 转换为 String + +public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String + +### String 转换为 StringBuilder + +public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder + +# 作业 + +```java +import java.util.Scanner; + +public class Test1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String password = "12345"; + System.out.println("请输入密码登陆"); + int count = 0; + for (int i = 0; i < 3; i++) { + String st = sc.next(); + count++; + if (st.equals(password)){ + System.out.println("密码正确,登陆成功"); + break; + }else { + System.out.println("密码错误"); + System.out.println("您还有" + (3 - count) + "次"); + } + } + } +} + + +import java.util.Scanner; + +public class Test2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入字符串遍历数组"); + String st = sc.next(); + for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ + da++; + } + if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ + xiao++; + } + if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ + shu++; + } + } + System.out.println("大写出现的次数:" + da); + System.out.println("小写出现的次数:" + xiao); + System.out.println("数字出现的次数:" + shu); + } +} + + +import javax.swing.*; +import java.util.Scanner; + +public class Test4 { + public static void main(String[] args) { + System.out.println("请输入电话号码"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.substring(0,3); + String s2 = st.substring(7); + System.out.println("转换后的电话号码为:" + s1 + "****" + s2); + } +} + + +import java.util.Scanner; + +public class Test5 { + public static void main(String[] args) { + System.out.println("请输入脏话"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.replace("TMD","不可以说脏话"); + System.out.println(s1); + } +} + + +public class Student { + private String name; + private int age; + + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } +} + + +import java.util.Arrays; +import java.util.Scanner; + +public class TestStudent6 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Student st = new Student(); + System.out.println("请输入名字"); + st.setName(sc.next()); + System.out.println("请输入年龄"); + st.setAge(sc.nextInt()); + String st2 = st.getName() +","+ st.getAge(); + String[] arr = st2.split(","); + System.out.println(Arrays.toString(arr)); + + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" index db5bfa5..4be6e5e 100644 --- "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" +++ "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" @@ -1,205 +1,205 @@ -```java -//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 -public class Student { - private String name; - private int age; - - public Student(String name, int age) { - this.name = name; - this.age = age; - } - - public Student(String name) { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - -} - -import java.util.ArrayList; - -public class TestStudent { - public static void main(String[] args) { - ArrayList list = new ArrayList<>(); - list.add(new Student("张三",18)); - list.add(new Student("李四",19)); - list.add(new Student("王五",20)); - for (int i = 0; i < list.size(); i++) { - Student stu = list.get(i); - System.out.print(stu.getName() + " "); - System.out.println(stu.getAge()); - } - } -} - -``` - -```java -//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 -//学生的姓名和年龄来自于键盘录入 -public class Student { - private String name; - private int age; - - public Student(String name, int age) { - this.name = name; - this.age = age; - } - - public Student(String name) { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - -} -import java.security.AllPermission; -import java.util.ArrayList; -import java.util.Scanner; - -public class TestStudent2 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int count = 0; - ArrayList list = new ArrayList<>(); - while (true) { - count++; - System.out.println("请输入第" + count + "名学生的姓名和年龄"); - list.add(new Student(sc.next(), sc.nextInt())); - if (count == 3){ - break; - } - } - for (int i = 0; i < list.size(); i++) { - Student stu = list.get(i); - System.out.print(stu.getName() + " "); - System.out.println(stu.getAge()); - } - } -} - -``` - -```java - -import java.util.ArrayList; -import java.util.Iterator; - -public class T2 { - public static void main(String[] args) { -// 需求:创建一个存储String的集合,内部存储 -// (test,张三,李四,test,test)字符串 -// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 - ArrayList list = new ArrayList<>(); - list.add("test"); - list.add("张三"); - list.add("李四"); - list.add("test"); - list.add("test"); -// Iterator it = list.iterator(); -// while(it.hasNext()){ -// String str = it.next(); -// if (str.contains("test")){ -// it.remove(); -// } -// } - list.removeIf(s -> s.contains("test")); - System.out.println(list); - } -} - - -``` - -```java -//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 -//并存入新集合对象,方法返回新集合。 -public class Student { - private String name; - private int age; - public Student(String name,int age){ - this.name = name; - this.age = age; - } - - public Student(){ - - } - - public void setName(String name){ - this.name = name; - } - public void setAge(int age){ - this.age = age; - } - - public String getName(){ - return this.name = name; - } - - public int getAge(){ - return this.age = age; - } - - - public String toString(){ - return "姓名:" + getName() + " " + "年龄:" + getAge(); - } -} - -import java.util.ArrayList; - -public class TestStudent { - public static void main(String[] args) { - ArrayList list = new ArrayList<>(); - list.add(new Student("张三", 19)); - list.add(new Student("李四", 20)); - list.add(new Student("王五", 10)); - list.add(new Student("赵六", 15)); - chaXun(new ArrayList<>(list)); - } - public static ArrayList chaXun(ArrayList list - ){ - ArrayList list1 = new ArrayList<>(); - for (int i = 0; i < list.size(); i++) { - Student st = list.get(i); - if (st.getAge()<18){ - list1.add(list.get(i)); - } - } - System.out.println(list1); - return list1; - } -} - -``` - +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三",18)); + list.add(new Student("李四",19)); + list.add(new Student("王五",20)); + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +//学生的姓名和年龄来自于键盘录入 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} +import java.security.AllPermission; +import java.util.ArrayList; +import java.util.Scanner; + +public class TestStudent2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int count = 0; + ArrayList list = new ArrayList<>(); + while (true) { + count++; + System.out.println("请输入第" + count + "名学生的姓名和年龄"); + list.add(new Student(sc.next(), sc.nextInt())); + if (count == 3){ + break; + } + } + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java + +import java.util.ArrayList; +import java.util.Iterator; + +public class T2 { + public static void main(String[] args) { +// 需求:创建一个存储String的集合,内部存储 +// (test,张三,李四,test,test)字符串 +// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 + ArrayList list = new ArrayList<>(); + list.add("test"); + list.add("张三"); + list.add("李四"); + list.add("test"); + list.add("test"); +// Iterator it = list.iterator(); +// while(it.hasNext()){ +// String str = it.next(); +// if (str.contains("test")){ +// it.remove(); +// } +// } + list.removeIf(s -> s.contains("test")); + System.out.println(list); + } +} + + +``` + +```java +//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 +//并存入新集合对象,方法返回新集合。 +public class Student { + private String name; + private int age; + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } + + + public String toString(){ + return "姓名:" + getName() + " " + "年龄:" + getAge(); + } +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三", 19)); + list.add(new Student("李四", 20)); + list.add(new Student("王五", 10)); + list.add(new Student("赵六", 15)); + chaXun(new ArrayList<>(list)); + } + public static ArrayList chaXun(ArrayList list + ){ + ArrayList list1 = new ArrayList<>(); + for (int i = 0; i < list.size(); i++) { + Student st = list.get(i); + if (st.getAge()<18){ + list1.add(list.get(i)); + } + } + System.out.println(list1); + return list1; + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" index bc280be..ebaa434 100644 --- "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" +++ "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -1,254 +1,254 @@ -```java -package Light; - -public class Student { - private String StuID; - private String name; - private int age; - private String sex; - - - - public String getStuID() { - return StuID; - } - - public void setStuID(String stuID) { - StuID = stuID; - } - - public String getSex() { - return sex; - } - - public void setSex(String sex) { - this.sex = sex; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Student(){} - public Student(String StuID,String name,int age,String sex){ - this.StuID=StuID; - this.name=name; - this.age=age; - this.sex=sex; - } - - @Override - public String toString() { - return "Student{" + - "StuID='" + StuID + '\'' + - ", name='" + name + '\'' + - ", age=" + age + - ", sex='" + sex + '\'' + - '}'; - } -} -``` - -```java -package Light; - -import java.util.ArrayList; -import java.util.Scanner; - -public class T { - public static void main(String[] args) { - ArrayList list=new ArrayList<>(); - Scanner sc=new Scanner(System.in); - - sum: - for (int i=0;;){ - System.out.println("============学生系统============"); - System.out.println("请输入编号:"+ - "\n\t1 添加学生信息"+ - "\n\t2 修改学生信息"+ - "\n\t3 删除学生信息"+ - "\n\t4 查看学生信息"+ - "\n\t5 退出学生系统"); - System.out.println(); - System.out.print("请输入编号:"); - String sum=sc.next();; - switch (sum){ - case "1": -// System.out.println("添加"); - insertStudent(list,sc); - break; - case"2": - updateStudent(list,sc); - break; - case"3": - deleteStudent(list,sc); - break; - case"4": - lookStudent(list,sc); - break; - case"5": - System.out.println("成功退出学生系统"); - break sum; - default: - System.out.println("输入编号错误,请重新输入!!!"); - System.out.println(); - } - } - } - - - /** - * 添加 - * @param list - * @param sc - */ - public static void insertStudent(ArrayList list, Scanner sc){ - System.out.println(); - System.out.println("===*请输入需要添加学生信息*==="); - System.out.println("请输入学号"); - String stuId=sc.next(); - - if (!list.isEmpty()){ - for (int i = 0; i< list.size(); i++){ - Student stu= list.get(i); - if (stuId.equals(stu.getStuID())){ - System.out.println("学号已经存在"); - break; - }else { - System.out.println("请输入姓名"); - String stuName=sc.next(); - System.out.println("请输入年龄"); - int stuAge=sc.nextInt(); - System.out.println("请输入性别"); - String stuSex=sc.next(); - list.add(new Student(stuId,stuName,stuAge,stuSex)); - } - } - }else { - System.out.println("请输入姓名"); - String stuName=sc.next(); - System.out.println("请输入年龄"); - int stuAge=sc.nextInt(); - System.out.println("请输入性别"); - String stuSex=sc.next(); - list.add(new Student(stuId,stuName,stuAge,stuSex)); - } - System.out.println(); - } - - - /** - * 修改 - * @param list - * @param sc - */ - - public static void updateStudent(ArrayList list,Scanner sc){ - System.out.println(); - if (list.isEmpty()){ - System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); - }else { - System.out.println("===修改学生信息==="); - System.out.println("请输入需要修改的学生学号"); - String stuId=sc.next(); - for (int i=0;i list,Scanner sc){ - System.out.println(); - if (list.isEmpty()){ - System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); - }else { - System.out.println("==*删除学生信息*=="); - System.out.println("请输入需要删除的学生学号"); - String stuId=sc.next(); - - for (int i=0;i list,Scanner sc){ - System.out.println(); - - if (list.isEmpty()) { - System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); - }else { - System.out.println("==*查看学生信息*=="); - System.out.println("请输入需要查看学生的学生学号"); - String stuId=sc.next(); - - for (int i=0;i list=new ArrayList<>(); + Scanner sc=new Scanner(System.in); + + sum: + for (int i=0;;){ + System.out.println("============学生系统============"); + System.out.println("请输入编号:"+ + "\n\t1 添加学生信息"+ + "\n\t2 修改学生信息"+ + "\n\t3 删除学生信息"+ + "\n\t4 查看学生信息"+ + "\n\t5 退出学生系统"); + System.out.println(); + System.out.print("请输入编号:"); + String sum=sc.next();; + switch (sum){ + case "1": +// System.out.println("添加"); + insertStudent(list,sc); + break; + case"2": + updateStudent(list,sc); + break; + case"3": + deleteStudent(list,sc); + break; + case"4": + lookStudent(list,sc); + break; + case"5": + System.out.println("成功退出学生系统"); + break sum; + default: + System.out.println("输入编号错误,请重新输入!!!"); + System.out.println(); + } + } + } + + + /** + * 添加 + * @param list + * @param sc + */ + public static void insertStudent(ArrayList list, Scanner sc){ + System.out.println(); + System.out.println("===*请输入需要添加学生信息*==="); + System.out.println("请输入学号"); + String stuId=sc.next(); + + if (!list.isEmpty()){ + for (int i = 0; i< list.size(); i++){ + Student stu= list.get(i); + if (stuId.equals(stu.getStuID())){ + System.out.println("学号已经存在"); + break; + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + } + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + System.out.println(); + } + + + /** + * 修改 + * @param list + * @param sc + */ + + public static void updateStudent(ArrayList list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); + }else { + System.out.println("===修改学生信息==="); + System.out.println("请输入需要修改的学生学号"); + String stuId=sc.next(); + for (int i=0;i list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); + }else { + System.out.println("==*删除学生信息*=="); + System.out.println("请输入需要删除的学生学号"); + String stuId=sc.next(); + + for (int i=0;i list,Scanner sc){ + System.out.println(); + + if (list.isEmpty()) { + System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); + }else { + System.out.println("==*查看学生信息*=="); + System.out.println("请输入需要查看学生的学生学号"); + String stuId=sc.next(); + + for (int i=0;i Date: Mon, 8 May 2023 20:20:37 +0800 Subject: [PATCH 07/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0=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" | 260 ------- ...55\346\263\225\350\256\255\347\273\203.md" | 149 ---- ...60\347\273\204\344\275\234\344\270\232.md" | 148 ---- ...71\350\261\241\344\275\234\344\270\232.md" | 47 -- ...32\345\222\214\347\254\224\350\256\260.md" | 130 ---- .../20230414 java\344\275\234\344\270\232.md" | 682 ------------------ ...32\345\222\214\347\254\224\350\256\260.md" | 290 -------- ...06\345\220\210\344\275\234\344\270\232.md" | 205 ------ ...41\347\220\206\347\263\273\347\273\237.md" | 254 ------- 9 files changed, 2165 deletions(-) delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230408 java\347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230415 JavaAPI\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index 5000ca1..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,260 +0,0 @@ -```java -import java.util.Scanner; - -public class Class { -// **1、判断一个字符数据是否是数字字符 ** -// -// **分析:** -// -// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 -// -// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, -// 并且还要下于或等于字符9即可。 -// -// 3、判断完成之后,打印判断的结果。 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - char i = sc.next.charAt(0); - if (i>=0 && i<10){ - System.out.println("是数字字符"); - }else { - System.out.println("不是数字字符"); - } - } -} - -import java.util.Scanner; - -public class java2 { - /* **2、判断一个字符数据是否是字母字符** - - **分析:** - - 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 - - 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, - 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z - - 3、判断完成之后,打印判断的结果。*/ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - char st = sc.next().charAt(0); - - if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) - - System.out.println( "是字母"); - - else - - System.out.println("不是字母"); - - } -} - - -import java.util.Scanner; - -public class java3 { -/* - **3、判断指定的年份是否为闰年,请使用键盘录入** - - **分析:** - - 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 - - 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 - -*/ - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int i = sc.nextInt(); - if ((i % 4 == 0 && i% 100 ==0) || i % 400 != 0 ){ - System.out.println("是闰年"); - }else{ - System.out.println("不是闰年"); - } - } -} - - -import java.util.Scanner; - -public class java4 { - // **4、判断一个数字是否为水仙花数,请使用键盘录入** -// -// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, -// -// **分析:** -// -// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 -// -// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 -// -// 2、判断的过程 -// -// a) 将3位数字的每一位上的数字拆分下来 -// -// b) 计算每位数字的3次幂之和 -// -// C) 用和值 和 原来的数字进行比较 -// -// D) 打印判断的比较结果即可 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入一个数字判断是不是水仙花数"); - int i = sc.nextInt(); - int ge = i % 10; - int si = i / 10 % 10; - int bai = i / 100 % 10; - if (i > 99 && i < 1000) { - if (ge * ge * ge + si * si * si + bai * bai * bai == i) { - System.out.println("是水仙花数"); - } else { - System.out.println("不是水仙花数"); - } - }else { - System.out.println("您好,水仙花数是三位数"); - } - } -} - - -import java.util.Scanner; - -public class java5 { - /* **5、判断一个5位数字是否为回文数,使用键盘录入** - - 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 - - **分析:** - - 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 - - 2、判断的过程 - - a) 将5位数字的万、千、十、个位数拆分出来 - - b) 判断比较万位和个位 、 千位和十位是否相等 - - 3、判断完成之后,打印判断的结果。*/ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入五位数判断是不是回文数"); - int i = sc.nextInt(); - int ge = i % 10; - int si = i / 10 % 10; - int qian = i / 1000 % 10; - int wan = i / 10000 % 10; - if (i > 9999 && i < 100000) { - if (ge == wan && si == qian) { - System.out.println("是回文数"); - } else { - System.out.println("不是回文数"); - } - }else { - System.out.println("您输入的不是五位数"); - } - } -} - - -import java.util.Scanner; - -public class java6 { -// ## 题目1(训练) -// -// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: -// -// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 -// -// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 -// -// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? -// -// ### 训练提示 -// -//1. 已知的父母身高如何用代码体现? -// -// 2. 题目中的公式如何转化为代码? -// -// ### 解题方案 -// -//1. 使用变量的定义和算术运算符完成本题 -// -//### 操作步骤 -// -//1. 定义小数变量代表父亲身高 -// -//2. 定义小数变量代表母亲身高 -// -//3. 通过儿子身高计算方式计算儿子身高 -// -//4. 通过女儿身高计算方式计算女人身高 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入父亲的身高"); - double baba = sc.nextInt(); - System.out.println("请输入母亲的身高"); - double mama = sc.nextInt(); - double erzi = (baba + mama) * 1.08 / 2; - double nver = (baba * 0.923 + mama) / 2; - System.out.println("儿子的身高为:" + erzi); - System.out.println("女儿的身高为:" + nver); - } -} - -public class java7 { -// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 -// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 -// 那么红茶和绿茶现在的钱一样多,请问对么? - public static void main(String[] args) { - int hocha = 21; - int lvcha = 24; - hocha = hocha * 2 + 3; - lvcha = lvcha * 2; - if (hocha == lvcha){ - System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); - }if (hocha > lvcha){ - System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); - }if (hocha < lvcha){ - System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); - } - } -} - - -public class java8 { -// 某小伙想定一份外卖,商家的优惠方式如下: -// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, -// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? - /*训练提示 - 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 - 解题方案 - 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 - 操作步骤 - 1. 使用算术运算符求出不使用优惠时的总价 - 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 - 3. 使用算术运算符求出使用优惠价时的总价 - 4. 使用三元运算符判断最终更合算的购买方式和花费*/ - public static void main(String[] args) { - int yuxian = 24; - int youza = 8; - int mifan = 3; - int zojia = yuxian + youza + mifan; - double youhuijia = zojia * 0.8; - System.out.println("使用折扣的总价:" + youhuijia); - int yuxian2 = 16; - int youza2 = 8; - int mifan2 = 3; - int zojia2 = yuxian2 + youza2 + mifan2; - System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); - if (youhuijia < zojia2){ - System.out.println("使用这个:" + youhuijia ); - }else if (youhuijia > zojia2){ - System.out.println("使用这个:" + zojia2); - } - } -} -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" deleted file mode 100644 index c7f47ac..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" +++ /dev/null @@ -1,149 +0,0 @@ -```java -package My; - -import javax.lang.model.SourceVersion; - -public class Class { - public static void main(String[] args) { - // 第一题 - //(1)先声明两个byte类型的变量b1,b2, - // 并分别赋值为10和20,求b1和b2变量的和, - // 并将结果保存在byte类型的变量b3中, - // 最后输出b3变量的值 - byte b1,b2; - b1=10; - b2=20; - byte b3=(byte)(b1+b2); - System.out.println("byte类型的b1和b2的和为:"+(b3)); - -// 第二题 -// (2)先声明两个short类型的变量s1,s2, -// 并分别赋值为1000和2000,求s1和s2变量的和, -// 并将结果保存在short类型的变量s3中, -// 最后输出s3变量的值 - short s1=1000,s2=2000; - short s3=(short)(s1+s2); - System.out.println("short类型的是s1和s2的和为:"+s3); - -// 第三题 -// (3)先声明1个char类型的变量c1赋值为'a', -// 再声明一个int类型的变量num赋值为5,求c1和num变量的和, -// 并将结果将结果保存在char类型的变量letter中, -// 最后输出letter变量的值。 - char c1='a'; - int num=5; - char letter=(char)(c1+(char)num); - System.out.println("char类型的c1和int类型的num的和:"+(letter)); - -// 第四题 -// (4)先声明两个int类型的变量i1,i2, -// 并分别赋值5和2,求i1和i2的商, -// 并将结果保存在double类型的变量result中, -// 最后输出result变量的值。如何得到结果2.5呢? - int i1=5,i2=2; - double result=(double) i1/i2; - System.out.println("i1和i2的商为:"+(result)); - } -} - - -package My; - -public class Class1 { - public static void main(String[] args) { - int a1=10,a2=11; - System.out.println("10是偶数?"+(a1%2==0)); - System.out.println("11是偶数?"+(a2%2==0)); - int a3=12,a4=13; - System.out.println("12是奇数?"+(a3%2!=0)); - System.out.println("13是奇数?"+(a4%2!=0)); - } -} - -package My; - -public class Class3 { - public static void main(String[] args) { -// 题目:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时? - int hours=89; - int day=hours/24; - int hour=hours%24; - System.out.println("为抵抗洪水,战士连续作战89小时:"+"89小时是"+(day)+"天"+(hour)+"小时"); - - } -} - -package My; - -public class Class4 { - public static void main(String[] args) { -// 题目:今天是周2,100天以后是周几? - int week=2; - week+=100; - week%=7; - System.out.println("今天是周二,100天以后是周"+(week)); - } -} - -package My; - -import java.util.Scanner; - -public class Class2 { - public static void main(String[] args) { -// ## 5、案例:求三个整数x,y,z中的最大值 -// -// -// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 -// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) -// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) -// 4. 输出结果 - Scanner sc=new Scanner(System.in); - System.out.print("请输入x的值:"); - int x=sc.nextInt(); - System.out.print("请输入y的值:"); - int y=sc.nextInt(); - System.out.print("请输入z的值:"); - int z=sc.nextInt(); - int max; - max=(x>y)?x:y; - max=(max>z)?max:z; - System.out.println(max); - } -} - - - -package My; - -public class Class5 { - public static void main(String[] args) { -// 题目:判断今年是否是闰年 - int year=2023; - boolean a=(year%4==0 && year%100!=0); - boolean b=(year%400==0); - if(a || b){ - System.out.println(year+"是闰年"); - }else { - System.out.println(year + "不是闰年"); - } - } -} - - -package My; - -public class Class6 { - public static void main(String[] args) { -// 题目:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。 -// 它需要一个程序将华氏温度(80度)转换为摄氏度, -// 并以华氏度和摄氏度为单位分别显示该温度。 - double hua=80; - double she; - she=(hua-32)/1.8; - System.out.println("华氏度80.0度转化为摄氏度是"+(she)+"度"); - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" deleted file mode 100644 index de5fc8d..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" +++ /dev/null @@ -1,148 +0,0 @@ -```java -public class T1 { - public static void main(String[] args) { -// .定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** -// -//**操作步骤:** -// -// 1.定义5个元素数组 -// 2.可以使用初始化数组的两种方式之一为数组元素赋值 -// 3.遍历数组求数组中的最小值 - - - int[] sum={12,32,52,2,5};//创立数组并赋值 - - int min=sum[0]; - for (int i=1;isum[i]){ - min=sum[i]; - } - } - System.out.println("数组sum的最小值为:"+min); - } -} -``` - - - -```java -package Glass; - -import java.util.Arrays; - -public class T2 { - public static void main(String[] args) { -// 需求:求出数组中索引与索引对应的元素都是奇数的元素** -// -//**分析:** -// -// 1、遍历数组 -// 2、判断索引是否是奇数(索引 % 2 != 0) -// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) -// 4、满足条件输出结果 - - int[] sum={12,4,32,6,32,11}; - System.out.println(Arrays.toString(sum)); - - for (int i=0;i= 0; - } - -// 包含一个方法int approximateNumberCount()方法, -// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 - public int approximateNumberCount(){ - int count = 0; - for (int i = 1; i <= value; i++) { - if (value % i == 0){ - count++; - } - } - return count; - } - -// 包含一个方法boolean isPrimeNumber()方法, -// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, -// 并且value是大于1的自然数,那么value就是素数。 - public boolean isPrimeNumber(){ - int count = 0; - boolean flag = false; - for (int i = 1; i < value ; i++) { - count++; - if (value % i == 0 && value % 1 == 0 && value > 1){ - if (count >= 3){ - flag = false; - }else { - flag = true; - } - } - } - return flag; - } -// 包含一个方法int[] getAllPrimeNumber()方法, -// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 - public int[] getAllPrimeNumber(){ - int[] arr = new int[approximateNumberCount()]; - int index = arr[0]; - for (int i = 1; i <= value; i++) { - if (value % i == 0){ - arr[index++]=i; - } - } - return arr; - } - - @Override - public String toString() { - return "MyInt{" + - "\nvalue=" + value + - "\n是不是自然数:" + isNatural() + - "\n约数个数:" + approximateNumberCount() + - "\n是不是素数:" + isPrimeNumber() + - "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + - "\n}"; - } -} - -package T6; - -public class TestMyLnt { - public static void main(String[] args) { - MyLnt lnt = new MyLnt(20); - System.out.println(lnt); - } -} - -``` - -# 第七题 - -```java -package T7; - -public class TestYue { - public static void main(String[] args) { - Yue y = new Yue(); - int min = y.min(44,33,88,342,454,234,5,2); - System.out.println("最小值为:" + min); - int max = y.maxApproximate(4,8,16,20,24); - System.out.println("最大公约数为:" + max); - } -} - - -package T7; - -public class Yue { -// 返回n个整数中的最小值 - int min(int... nums){ - int min = nums[0]; - for (int i = 0; i < nums.length; i++) { - if (min > nums[i]){ - min = nums[i]; - } - } - return min; - } - - public int maxApproximate(int... nums){ - int max=min(nums); - for (int i = max; i >= 1; i--) { - boolean flag=true; - for (int j = 0; j < nums.length; j++) { - if (nums[j]%i!=0){ - flag=false; - break; - } - } - if (flag==true){ - return i; - } - } - return 1; - } -} - -``` - -# 第八题 - -```java -package T8; - -public class ArraysTools { - public String toString(int[]arr) { - String str = "["; - for (int i = 0; i < arr.length; i++) { - int i1 = arr[i]; - str += i1; - if (i < arr.length - 1) { - str += ","; - } - } - str += "]"; - return str; - } - int[] grow(int[] arr){ - int shu = arr.length; - int[] newArr = new int[2 * shu]; - for (int i = 0; i < arr.length; i++) { - newArr[i] = arr[i]; - } - return newArr; - } -} - -package T8; - -public class Test { - public static void main(String[] args) { - int[]old={11,22,33,44}; - ArraysTools arraysTools = new ArraysTools(); - System.out.println(arraysTools.toString(old)); - int[]newArr= arraysTools.grow(old); - System.out.println(arraysTools.toString(newArr)); - } -} - -``` - -# 第九题 - -```java -package T9; - -public class MathTools { - int compare(int a, int b){ - if (a > b){ - return a; - }else if (a < b){ - return -a; - }else { - return 0; - } - } -/* -* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, -* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; -* */ - int compare(double a, double b){ - if (a > b){ - return (int)a; - }else if (a < b){ - return (int)-a; - }else { - return 0; - } - } - -// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, -// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; - int compare(char a, char b){ - if (a > b){ - return (char)a; - }else if (a < b){ - return (char)-a; - }else { - return 0; - } - } -} - - -package T9; - -public class Test { - public static void main(String[] args) { - MathTools ma = new MathTools(); - int a = ma.compare(3,5); - int b = ma.compare('2','3'); - int c = ma.compare(33.5,22.4); - System.out.println(a); - System.out.println(b); - System.out.println(c); - } -} - -``` - -# 第十题 - -```java -package T10; - -public class ArraysTools { - public void sort(int[] arr){ - for (int i = 1; i < arr.length; i++) { - for (int j = 0; j < arr.length - i; j++) { - if (arr[j] > arr[j + 1]){ - int temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } - public void sort(double[] arr){ - for (int i= 1; i arr[j+1]){ - double temp=arr[j]; - arr[j]=arr[j+1]; - arr[j+1]=temp; - } - } - } - } - - public void sort(char[] arr) { - for (int i = 1; i < arr.length; i++) { - for (int j = 0; j < arr.length - i; j++) { - if (arr[j] > arr[j + 1]) { - char temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } - public String toString(int[] arr){ - String re="["; - for (int i = 0; i < arr.length; i++) { - if (i我***你 - -##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 - -String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] - -## String方法小结 - -| **方法名** | **说明** | -| ----------------------------------------------------- | -------------------------------- | -| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | -| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | -| public int length() | 返回此字符串的长度 | - -| public char charAt(int index) | 返回指定索引处的 char 值 | -| ----------------------------- | ---------------------------- | -| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | - -| **方法名** | **说明** | -| ------------------------------------------------------------ | ------------------------------------------------------------ | -| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | -| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | -| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | - -| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | -| ----------------------------------- | ---------------------------------------- | -| | | - -## StringBuilder - -StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 - -作用:提高字符串的操作效率 - -### StringBuilder 构造方法 - -| **方法名** | **说明** | -| -------------------------------- | ------------------------------------------ | -| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | -| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | - -### StringBuilder 的常用方法 - -| **方法名** | **说明** | -| -------------------------------------- | ------------------------ | -| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | -| public StringBuilder reverse() | 返回相反的字符序列 | - -| public int length() | 返回长度 ( 字符出现的个数) | -| ------------------- | -------------------------- | -| | | - -| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | -| ------------------------ | --------------------------------------------------- | -| | | - -## StringBuilder和String的区别 - -**String** :内容是不可变的 - -**StringBuilder**:内容是可变的 - -## StringBuilder 和 String 相互转化 - -### StringBuilder 转换为 String - -public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String - -### String 转换为 StringBuilder - -public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder - -# 作业 - -```java -import java.util.Scanner; - -public class Test1 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - String password = "12345"; - System.out.println("请输入密码登陆"); - int count = 0; - for (int i = 0; i < 3; i++) { - String st = sc.next(); - count++; - if (st.equals(password)){ - System.out.println("密码正确,登陆成功"); - break; - }else { - System.out.println("密码错误"); - System.out.println("您还有" + (3 - count) + "次"); - } - } - } -} - - -import java.util.Scanner; - -public class Test2 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入字符串遍历数组"); - String st = sc.next(); - for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ - da++; - } - if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ - xiao++; - } - if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ - shu++; - } - } - System.out.println("大写出现的次数:" + da); - System.out.println("小写出现的次数:" + xiao); - System.out.println("数字出现的次数:" + shu); - } -} - - -import javax.swing.*; -import java.util.Scanner; - -public class Test4 { - public static void main(String[] args) { - System.out.println("请输入电话号码"); - Scanner sc = new Scanner(System.in); - String st = sc.next(); - String s1 = st.substring(0,3); - String s2 = st.substring(7); - System.out.println("转换后的电话号码为:" + s1 + "****" + s2); - } -} - - -import java.util.Scanner; - -public class Test5 { - public static void main(String[] args) { - System.out.println("请输入脏话"); - Scanner sc = new Scanner(System.in); - String st = sc.next(); - String s1 = st.replace("TMD","不可以说脏话"); - System.out.println(s1); - } -} - - -public class Student { - private String name; - private int age; - - public Student(String name,int age){ - this.name = name; - this.age = age; - } - - public Student(){ - - } - - public void setName(String name){ - this.name = name; - } - - public void setAge(int age){ - this.age = age; - } - - public String getName(){ - return this.name = name; - } - - public int getAge(){ - return this.age = age; - } -} - - -import java.util.Arrays; -import java.util.Scanner; - -public class TestStudent6 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - Student st = new Student(); - System.out.println("请输入名字"); - st.setName(sc.next()); - System.out.println("请输入年龄"); - st.setAge(sc.nextInt()); - String st2 = st.getName() +","+ st.getAge(); - String[] arr = st2.split(","); - System.out.println(Arrays.toString(arr)); - - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" deleted file mode 100644 index 4be6e5e..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" +++ /dev/null @@ -1,205 +0,0 @@ -```java -//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 -public class Student { - private String name; - private int age; - - public Student(String name, int age) { - this.name = name; - this.age = age; - } - - public Student(String name) { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - -} - -import java.util.ArrayList; - -public class TestStudent { - public static void main(String[] args) { - ArrayList list = new ArrayList<>(); - list.add(new Student("张三",18)); - list.add(new Student("李四",19)); - list.add(new Student("王五",20)); - for (int i = 0; i < list.size(); i++) { - Student stu = list.get(i); - System.out.print(stu.getName() + " "); - System.out.println(stu.getAge()); - } - } -} - -``` - -```java -//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 -//学生的姓名和年龄来自于键盘录入 -public class Student { - private String name; - private int age; - - public Student(String name, int age) { - this.name = name; - this.age = age; - } - - public Student(String name) { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - -} -import java.security.AllPermission; -import java.util.ArrayList; -import java.util.Scanner; - -public class TestStudent2 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int count = 0; - ArrayList list = new ArrayList<>(); - while (true) { - count++; - System.out.println("请输入第" + count + "名学生的姓名和年龄"); - list.add(new Student(sc.next(), sc.nextInt())); - if (count == 3){ - break; - } - } - for (int i = 0; i < list.size(); i++) { - Student stu = list.get(i); - System.out.print(stu.getName() + " "); - System.out.println(stu.getAge()); - } - } -} - -``` - -```java - -import java.util.ArrayList; -import java.util.Iterator; - -public class T2 { - public static void main(String[] args) { -// 需求:创建一个存储String的集合,内部存储 -// (test,张三,李四,test,test)字符串 -// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 - ArrayList list = new ArrayList<>(); - list.add("test"); - list.add("张三"); - list.add("李四"); - list.add("test"); - list.add("test"); -// Iterator it = list.iterator(); -// while(it.hasNext()){ -// String str = it.next(); -// if (str.contains("test")){ -// it.remove(); -// } -// } - list.removeIf(s -> s.contains("test")); - System.out.println(list); - } -} - - -``` - -```java -//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 -//并存入新集合对象,方法返回新集合。 -public class Student { - private String name; - private int age; - public Student(String name,int age){ - this.name = name; - this.age = age; - } - - public Student(){ - - } - - public void setName(String name){ - this.name = name; - } - public void setAge(int age){ - this.age = age; - } - - public String getName(){ - return this.name = name; - } - - public int getAge(){ - return this.age = age; - } - - - public String toString(){ - return "姓名:" + getName() + " " + "年龄:" + getAge(); - } -} - -import java.util.ArrayList; - -public class TestStudent { - public static void main(String[] args) { - ArrayList list = new ArrayList<>(); - list.add(new Student("张三", 19)); - list.add(new Student("李四", 20)); - list.add(new Student("王五", 10)); - list.add(new Student("赵六", 15)); - chaXun(new ArrayList<>(list)); - } - public static ArrayList chaXun(ArrayList list - ){ - ArrayList list1 = new ArrayList<>(); - for (int i = 0; i < list.size(); i++) { - Student st = list.get(i); - if (st.getAge()<18){ - list1.add(list.get(i)); - } - } - System.out.println(list1); - return list1; - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" deleted file mode 100644 index ebaa434..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" +++ /dev/null @@ -1,254 +0,0 @@ -```java -package Light; - -public class Student { - private String StuID; - private String name; - private int age; - private String sex; - - - - public String getStuID() { - return StuID; - } - - public void setStuID(String stuID) { - StuID = stuID; - } - - public String getSex() { - return sex; - } - - public void setSex(String sex) { - this.sex = sex; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Student(){} - public Student(String StuID,String name,int age,String sex){ - this.StuID=StuID; - this.name=name; - this.age=age; - this.sex=sex; - } - - @Override - public String toString() { - return "Student{" + - "StuID='" + StuID + '\'' + - ", name='" + name + '\'' + - ", age=" + age + - ", sex='" + sex + '\'' + - '}'; - } -} -``` - -```java -package Light; - -import java.util.ArrayList; -import java.util.Scanner; - -public class T { - public static void main(String[] args) { - ArrayList list=new ArrayList<>(); - Scanner sc=new Scanner(System.in); - - sum: - for (int i=0;;){ - System.out.println("============学生系统============"); - System.out.println("请输入编号:"+ - "\n\t1 添加学生信息"+ - "\n\t2 修改学生信息"+ - "\n\t3 删除学生信息"+ - "\n\t4 查看学生信息"+ - "\n\t5 退出学生系统"); - System.out.println(); - System.out.print("请输入编号:"); - String sum=sc.next();; - switch (sum){ - case "1": -// System.out.println("添加"); - insertStudent(list,sc); - break; - case"2": - updateStudent(list,sc); - break; - case"3": - deleteStudent(list,sc); - break; - case"4": - lookStudent(list,sc); - break; - case"5": - System.out.println("成功退出学生系统"); - break sum; - default: - System.out.println("输入编号错误,请重新输入!!!"); - System.out.println(); - } - } - } - - - /** - * 添加 - * @param list - * @param sc - */ - public static void insertStudent(ArrayList list, Scanner sc){ - System.out.println(); - System.out.println("===*请输入需要添加学生信息*==="); - System.out.println("请输入学号"); - String stuId=sc.next(); - - if (!list.isEmpty()){ - for (int i = 0; i< list.size(); i++){ - Student stu= list.get(i); - if (stuId.equals(stu.getStuID())){ - System.out.println("学号已经存在"); - break; - }else { - System.out.println("请输入姓名"); - String stuName=sc.next(); - System.out.println("请输入年龄"); - int stuAge=sc.nextInt(); - System.out.println("请输入性别"); - String stuSex=sc.next(); - list.add(new Student(stuId,stuName,stuAge,stuSex)); - } - } - }else { - System.out.println("请输入姓名"); - String stuName=sc.next(); - System.out.println("请输入年龄"); - int stuAge=sc.nextInt(); - System.out.println("请输入性别"); - String stuSex=sc.next(); - list.add(new Student(stuId,stuName,stuAge,stuSex)); - } - System.out.println(); - } - - - /** - * 修改 - * @param list - * @param sc - */ - - public static void updateStudent(ArrayList list,Scanner sc){ - System.out.println(); - if (list.isEmpty()){ - System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); - }else { - System.out.println("===修改学生信息==="); - System.out.println("请输入需要修改的学生学号"); - String stuId=sc.next(); - for (int i=0;i list,Scanner sc){ - System.out.println(); - if (list.isEmpty()){ - System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); - }else { - System.out.println("==*删除学生信息*=="); - System.out.println("请输入需要删除的学生学号"); - String stuId=sc.next(); - - for (int i=0;i list,Scanner sc){ - System.out.println(); - - if (list.isEmpty()) { - System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); - }else { - System.out.println("==*查看学生信息*=="); - System.out.println("请输入需要查看学生的学生学号"); - String stuId=sc.next(); - - for (int i=0;i Date: Mon, 8 May 2023 20:24:00 +0800 Subject: [PATCH 08/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0=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" | 260 +++++++ ...55\346\263\225\350\256\255\347\273\203.md" | 149 ++++ ...60\347\273\204\344\275\234\344\270\232.md" | 148 ++++ ...71\350\261\241\344\275\234\344\270\232.md" | 47 ++ ...32\345\222\214\347\254\224\350\256\260.md" | 130 ++++ .../20230414 java\344\275\234\344\270\232.md" | 682 ++++++++++++++++++ ...32\345\222\214\347\254\224\350\256\260.md" | 290 ++++++++ ...06\345\220\210\344\275\234\344\270\232.md" | 205 ++++++ ...41\347\220\206\347\263\273\347\273\237.md" | 254 +++++++ 9 files changed, 2165 insertions(+) create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230408 java\347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230415 JavaAPI\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..5000ca1 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,260 @@ +```java +import java.util.Scanner; + +public class Class { +// **1、判断一个字符数据是否是数字字符 ** +// +// **分析:** +// +// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 +// +// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, +// 并且还要下于或等于字符9即可。 +// +// 3、判断完成之后,打印判断的结果。 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char i = sc.next.charAt(0); + if (i>=0 && i<10){ + System.out.println("是数字字符"); + }else { + System.out.println("不是数字字符"); + } + } +} + +import java.util.Scanner; + +public class java2 { + /* **2、判断一个字符数据是否是字母字符** + + **分析:** + + 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 + + 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, + 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char st = sc.next().charAt(0); + + if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) + + System.out.println( "是字母"); + + else + + System.out.println("不是字母"); + + } +} + + +import java.util.Scanner; + +public class java3 { +/* + **3、判断指定的年份是否为闰年,请使用键盘录入** + + **分析:** + + 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 + + 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 + +*/ + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int i = sc.nextInt(); + if ((i % 4 == 0 && i% 100 ==0) || i % 400 != 0 ){ + System.out.println("是闰年"); + }else{ + System.out.println("不是闰年"); + } + } +} + + +import java.util.Scanner; + +public class java4 { + // **4、判断一个数字是否为水仙花数,请使用键盘录入** +// +// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, +// +// **分析:** +// +// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 +// +// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 +// +// 2、判断的过程 +// +// a) 将3位数字的每一位上的数字拆分下来 +// +// b) 计算每位数字的3次幂之和 +// +// C) 用和值 和 原来的数字进行比较 +// +// D) 打印判断的比较结果即可 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个数字判断是不是水仙花数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int bai = i / 100 % 10; + if (i > 99 && i < 1000) { + if (ge * ge * ge + si * si * si + bai * bai * bai == i) { + System.out.println("是水仙花数"); + } else { + System.out.println("不是水仙花数"); + } + }else { + System.out.println("您好,水仙花数是三位数"); + } + } +} + + +import java.util.Scanner; + +public class java5 { + /* **5、判断一个5位数字是否为回文数,使用键盘录入** + + 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 + + **分析:** + + 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 + + 2、判断的过程 + + a) 将5位数字的万、千、十、个位数拆分出来 + + b) 判断比较万位和个位 、 千位和十位是否相等 + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入五位数判断是不是回文数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int qian = i / 1000 % 10; + int wan = i / 10000 % 10; + if (i > 9999 && i < 100000) { + if (ge == wan && si == qian) { + System.out.println("是回文数"); + } else { + System.out.println("不是回文数"); + } + }else { + System.out.println("您输入的不是五位数"); + } + } +} + + +import java.util.Scanner; + +public class java6 { +// ## 题目1(训练) +// +// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: +// +// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 +// +// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 +// +// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? +// +// ### 训练提示 +// +//1. 已知的父母身高如何用代码体现? +// +// 2. 题目中的公式如何转化为代码? +// +// ### 解题方案 +// +//1. 使用变量的定义和算术运算符完成本题 +// +//### 操作步骤 +// +//1. 定义小数变量代表父亲身高 +// +//2. 定义小数变量代表母亲身高 +// +//3. 通过儿子身高计算方式计算儿子身高 +// +//4. 通过女儿身高计算方式计算女人身高 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入父亲的身高"); + double baba = sc.nextInt(); + System.out.println("请输入母亲的身高"); + double mama = sc.nextInt(); + double erzi = (baba + mama) * 1.08 / 2; + double nver = (baba * 0.923 + mama) / 2; + System.out.println("儿子的身高为:" + erzi); + System.out.println("女儿的身高为:" + nver); + } +} + +public class java7 { +// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 +// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 +// 那么红茶和绿茶现在的钱一样多,请问对么? + public static void main(String[] args) { + int hocha = 21; + int lvcha = 24; + hocha = hocha * 2 + 3; + lvcha = lvcha * 2; + if (hocha == lvcha){ + System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); + }if (hocha > lvcha){ + System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + }if (hocha < lvcha){ + System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + } + } +} + + +public class java8 { +// 某小伙想定一份外卖,商家的优惠方式如下: +// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, +// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? + /*训练提示 + 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 + 解题方案 + 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 + 操作步骤 + 1. 使用算术运算符求出不使用优惠时的总价 + 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 + 3. 使用算术运算符求出使用优惠价时的总价 + 4. 使用三元运算符判断最终更合算的购买方式和花费*/ + public static void main(String[] args) { + int yuxian = 24; + int youza = 8; + int mifan = 3; + int zojia = yuxian + youza + mifan; + double youhuijia = zojia * 0.8; + System.out.println("使用折扣的总价:" + youhuijia); + int yuxian2 = 16; + int youza2 = 8; + int mifan2 = 3; + int zojia2 = yuxian2 + youza2 + mifan2; + System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); + if (youhuijia < zojia2){ + System.out.println("使用这个:" + youhuijia ); + }else if (youhuijia > zojia2){ + System.out.println("使用这个:" + zojia2); + } + } +} +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" new file mode 100644 index 0000000..c7f47ac --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" @@ -0,0 +1,149 @@ +```java +package My; + +import javax.lang.model.SourceVersion; + +public class Class { + public static void main(String[] args) { + // 第一题 + //(1)先声明两个byte类型的变量b1,b2, + // 并分别赋值为10和20,求b1和b2变量的和, + // 并将结果保存在byte类型的变量b3中, + // 最后输出b3变量的值 + byte b1,b2; + b1=10; + b2=20; + byte b3=(byte)(b1+b2); + System.out.println("byte类型的b1和b2的和为:"+(b3)); + +// 第二题 +// (2)先声明两个short类型的变量s1,s2, +// 并分别赋值为1000和2000,求s1和s2变量的和, +// 并将结果保存在short类型的变量s3中, +// 最后输出s3变量的值 + short s1=1000,s2=2000; + short s3=(short)(s1+s2); + System.out.println("short类型的是s1和s2的和为:"+s3); + +// 第三题 +// (3)先声明1个char类型的变量c1赋值为'a', +// 再声明一个int类型的变量num赋值为5,求c1和num变量的和, +// 并将结果将结果保存在char类型的变量letter中, +// 最后输出letter变量的值。 + char c1='a'; + int num=5; + char letter=(char)(c1+(char)num); + System.out.println("char类型的c1和int类型的num的和:"+(letter)); + +// 第四题 +// (4)先声明两个int类型的变量i1,i2, +// 并分别赋值5和2,求i1和i2的商, +// 并将结果保存在double类型的变量result中, +// 最后输出result变量的值。如何得到结果2.5呢? + int i1=5,i2=2; + double result=(double) i1/i2; + System.out.println("i1和i2的商为:"+(result)); + } +} + + +package My; + +public class Class1 { + public static void main(String[] args) { + int a1=10,a2=11; + System.out.println("10是偶数?"+(a1%2==0)); + System.out.println("11是偶数?"+(a2%2==0)); + int a3=12,a4=13; + System.out.println("12是奇数?"+(a3%2!=0)); + System.out.println("13是奇数?"+(a4%2!=0)); + } +} + +package My; + +public class Class3 { + public static void main(String[] args) { +// 题目:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时? + int hours=89; + int day=hours/24; + int hour=hours%24; + System.out.println("为抵抗洪水,战士连续作战89小时:"+"89小时是"+(day)+"天"+(hour)+"小时"); + + } +} + +package My; + +public class Class4 { + public static void main(String[] args) { +// 题目:今天是周2,100天以后是周几? + int week=2; + week+=100; + week%=7; + System.out.println("今天是周二,100天以后是周"+(week)); + } +} + +package My; + +import java.util.Scanner; + +public class Class2 { + public static void main(String[] args) { +// ## 5、案例:求三个整数x,y,z中的最大值 +// +// +// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 +// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) +// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) +// 4. 输出结果 + Scanner sc=new Scanner(System.in); + System.out.print("请输入x的值:"); + int x=sc.nextInt(); + System.out.print("请输入y的值:"); + int y=sc.nextInt(); + System.out.print("请输入z的值:"); + int z=sc.nextInt(); + int max; + max=(x>y)?x:y; + max=(max>z)?max:z; + System.out.println(max); + } +} + + + +package My; + +public class Class5 { + public static void main(String[] args) { +// 题目:判断今年是否是闰年 + int year=2023; + boolean a=(year%4==0 && year%100!=0); + boolean b=(year%400==0); + if(a || b){ + System.out.println(year+"是闰年"); + }else { + System.out.println(year + "不是闰年"); + } + } +} + + +package My; + +public class Class6 { + public static void main(String[] args) { +// 题目:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。 +// 它需要一个程序将华氏温度(80度)转换为摄氏度, +// 并以华氏度和摄氏度为单位分别显示该温度。 + double hua=80; + double she; + she=(hua-32)/1.8; + System.out.println("华氏度80.0度转化为摄氏度是"+(she)+"度"); + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000..de5fc8d --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" @@ -0,0 +1,148 @@ +```java +public class T1 { + public static void main(String[] args) { +// .定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** +// +//**操作步骤:** +// +// 1.定义5个元素数组 +// 2.可以使用初始化数组的两种方式之一为数组元素赋值 +// 3.遍历数组求数组中的最小值 + + + int[] sum={12,32,52,2,5};//创立数组并赋值 + + int min=sum[0]; + for (int i=1;isum[i]){ + min=sum[i]; + } + } + System.out.println("数组sum的最小值为:"+min); + } +} +``` + + + +```java +package Glass; + +import java.util.Arrays; + +public class T2 { + public static void main(String[] args) { +// 需求:求出数组中索引与索引对应的元素都是奇数的元素** +// +//**分析:** +// +// 1、遍历数组 +// 2、判断索引是否是奇数(索引 % 2 != 0) +// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) +// 4、满足条件输出结果 + + int[] sum={12,4,32,6,32,11}; + System.out.println(Arrays.toString(sum)); + + for (int i=0;i= 0; + } + +// 包含一个方法int approximateNumberCount()方法, +// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 + public int approximateNumberCount(){ + int count = 0; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + count++; + } + } + return count; + } + +// 包含一个方法boolean isPrimeNumber()方法, +// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, +// 并且value是大于1的自然数,那么value就是素数。 + public boolean isPrimeNumber(){ + int count = 0; + boolean flag = false; + for (int i = 1; i < value ; i++) { + count++; + if (value % i == 0 && value % 1 == 0 && value > 1){ + if (count >= 3){ + flag = false; + }else { + flag = true; + } + } + } + return flag; + } +// 包含一个方法int[] getAllPrimeNumber()方法, +// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 + public int[] getAllPrimeNumber(){ + int[] arr = new int[approximateNumberCount()]; + int index = arr[0]; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + arr[index++]=i; + } + } + return arr; + } + + @Override + public String toString() { + return "MyInt{" + + "\nvalue=" + value + + "\n是不是自然数:" + isNatural() + + "\n约数个数:" + approximateNumberCount() + + "\n是不是素数:" + isPrimeNumber() + + "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + + "\n}"; + } +} + +package T6; + +public class TestMyLnt { + public static void main(String[] args) { + MyLnt lnt = new MyLnt(20); + System.out.println(lnt); + } +} + +``` + +# 第七题 + +```java +package T7; + +public class TestYue { + public static void main(String[] args) { + Yue y = new Yue(); + int min = y.min(44,33,88,342,454,234,5,2); + System.out.println("最小值为:" + min); + int max = y.maxApproximate(4,8,16,20,24); + System.out.println("最大公约数为:" + max); + } +} + + +package T7; + +public class Yue { +// 返回n个整数中的最小值 + int min(int... nums){ + int min = nums[0]; + for (int i = 0; i < nums.length; i++) { + if (min > nums[i]){ + min = nums[i]; + } + } + return min; + } + + public int maxApproximate(int... nums){ + int max=min(nums); + for (int i = max; i >= 1; i--) { + boolean flag=true; + for (int j = 0; j < nums.length; j++) { + if (nums[j]%i!=0){ + flag=false; + break; + } + } + if (flag==true){ + return i; + } + } + return 1; + } +} + +``` + +# 第八题 + +```java +package T8; + +public class ArraysTools { + public String toString(int[]arr) { + String str = "["; + for (int i = 0; i < arr.length; i++) { + int i1 = arr[i]; + str += i1; + if (i < arr.length - 1) { + str += ","; + } + } + str += "]"; + return str; + } + int[] grow(int[] arr){ + int shu = arr.length; + int[] newArr = new int[2 * shu]; + for (int i = 0; i < arr.length; i++) { + newArr[i] = arr[i]; + } + return newArr; + } +} + +package T8; + +public class Test { + public static void main(String[] args) { + int[]old={11,22,33,44}; + ArraysTools arraysTools = new ArraysTools(); + System.out.println(arraysTools.toString(old)); + int[]newArr= arraysTools.grow(old); + System.out.println(arraysTools.toString(newArr)); + } +} + +``` + +# 第九题 + +```java +package T9; + +public class MathTools { + int compare(int a, int b){ + if (a > b){ + return a; + }else if (a < b){ + return -a; + }else { + return 0; + } + } +/* +* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, +* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; +* */ + int compare(double a, double b){ + if (a > b){ + return (int)a; + }else if (a < b){ + return (int)-a; + }else { + return 0; + } + } + +// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, +// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; + int compare(char a, char b){ + if (a > b){ + return (char)a; + }else if (a < b){ + return (char)-a; + }else { + return 0; + } + } +} + + +package T9; + +public class Test { + public static void main(String[] args) { + MathTools ma = new MathTools(); + int a = ma.compare(3,5); + int b = ma.compare('2','3'); + int c = ma.compare(33.5,22.4); + System.out.println(a); + System.out.println(b); + System.out.println(c); + } +} + +``` + +# 第十题 + +```java +package T10; + +public class ArraysTools { + public void sort(int[] arr){ + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]){ + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public void sort(double[] arr){ + for (int i= 1; i arr[j+1]){ + double temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } + } + } + + public void sort(char[] arr) { + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]) { + char temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public String toString(int[] arr){ + String re="["; + for (int i = 0; i < arr.length; i++) { + if (i我***你 + +##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 + +String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] + +## String方法小结 + +| **方法名** | **说明** | +| ----------------------------------------------------- | -------------------------------- | +| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | +| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | +| public int length() | 返回此字符串的长度 | + +| public char charAt(int index) | 返回指定索引处的 char 值 | +| ----------------------------- | ---------------------------- | +| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | + +| **方法名** | **说明** | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | +| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | +| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | + +| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | +| ----------------------------------- | ---------------------------------------- | +| | | + +## StringBuilder + +StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 + +作用:提高字符串的操作效率 + +### StringBuilder 构造方法 + +| **方法名** | **说明** | +| -------------------------------- | ------------------------------------------ | +| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | +| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | + +### StringBuilder 的常用方法 + +| **方法名** | **说明** | +| -------------------------------------- | ------------------------ | +| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | +| public StringBuilder reverse() | 返回相反的字符序列 | + +| public int length() | 返回长度 ( 字符出现的个数) | +| ------------------- | -------------------------- | +| | | + +| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | +| ------------------------ | --------------------------------------------------- | +| | | + +## StringBuilder和String的区别 + +**String** :内容是不可变的 + +**StringBuilder**:内容是可变的 + +## StringBuilder 和 String 相互转化 + +### StringBuilder 转换为 String + +public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String + +### String 转换为 StringBuilder + +public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder + +# 作业 + +```java +import java.util.Scanner; + +public class Test1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String password = "12345"; + System.out.println("请输入密码登陆"); + int count = 0; + for (int i = 0; i < 3; i++) { + String st = sc.next(); + count++; + if (st.equals(password)){ + System.out.println("密码正确,登陆成功"); + break; + }else { + System.out.println("密码错误"); + System.out.println("您还有" + (3 - count) + "次"); + } + } + } +} + + +import java.util.Scanner; + +public class Test2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入字符串遍历数组"); + String st = sc.next(); + for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ + da++; + } + if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ + xiao++; + } + if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ + shu++; + } + } + System.out.println("大写出现的次数:" + da); + System.out.println("小写出现的次数:" + xiao); + System.out.println("数字出现的次数:" + shu); + } +} + + +import javax.swing.*; +import java.util.Scanner; + +public class Test4 { + public static void main(String[] args) { + System.out.println("请输入电话号码"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.substring(0,3); + String s2 = st.substring(7); + System.out.println("转换后的电话号码为:" + s1 + "****" + s2); + } +} + + +import java.util.Scanner; + +public class Test5 { + public static void main(String[] args) { + System.out.println("请输入脏话"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.replace("TMD","不可以说脏话"); + System.out.println(s1); + } +} + + +public class Student { + private String name; + private int age; + + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } +} + + +import java.util.Arrays; +import java.util.Scanner; + +public class TestStudent6 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Student st = new Student(); + System.out.println("请输入名字"); + st.setName(sc.next()); + System.out.println("请输入年龄"); + st.setAge(sc.nextInt()); + String st2 = st.getName() +","+ st.getAge(); + String[] arr = st2.split(","); + System.out.println(Arrays.toString(arr)); + + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" new file mode 100644 index 0000000..4be6e5e --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" @@ -0,0 +1,205 @@ +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三",18)); + list.add(new Student("李四",19)); + list.add(new Student("王五",20)); + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +//学生的姓名和年龄来自于键盘录入 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} +import java.security.AllPermission; +import java.util.ArrayList; +import java.util.Scanner; + +public class TestStudent2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int count = 0; + ArrayList list = new ArrayList<>(); + while (true) { + count++; + System.out.println("请输入第" + count + "名学生的姓名和年龄"); + list.add(new Student(sc.next(), sc.nextInt())); + if (count == 3){ + break; + } + } + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java + +import java.util.ArrayList; +import java.util.Iterator; + +public class T2 { + public static void main(String[] args) { +// 需求:创建一个存储String的集合,内部存储 +// (test,张三,李四,test,test)字符串 +// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 + ArrayList list = new ArrayList<>(); + list.add("test"); + list.add("张三"); + list.add("李四"); + list.add("test"); + list.add("test"); +// Iterator it = list.iterator(); +// while(it.hasNext()){ +// String str = it.next(); +// if (str.contains("test")){ +// it.remove(); +// } +// } + list.removeIf(s -> s.contains("test")); + System.out.println(list); + } +} + + +``` + +```java +//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 +//并存入新集合对象,方法返回新集合。 +public class Student { + private String name; + private int age; + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } + + + public String toString(){ + return "姓名:" + getName() + " " + "年龄:" + getAge(); + } +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三", 19)); + list.add(new Student("李四", 20)); + list.add(new Student("王五", 10)); + list.add(new Student("赵六", 15)); + chaXun(new ArrayList<>(list)); + } + public static ArrayList chaXun(ArrayList list + ){ + ArrayList list1 = new ArrayList<>(); + for (int i = 0; i < list.size(); i++) { + Student st = list.get(i); + if (st.getAge()<18){ + list1.add(list.get(i)); + } + } + System.out.println(list1); + return list1; + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000..ebaa434 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,254 @@ +```java +package Light; + +public class Student { + private String StuID; + private String name; + private int age; + private String sex; + + + + public String getStuID() { + return StuID; + } + + public void setStuID(String stuID) { + StuID = stuID; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Student(){} + public Student(String StuID,String name,int age,String sex){ + this.StuID=StuID; + this.name=name; + this.age=age; + this.sex=sex; + } + + @Override + public String toString() { + return "Student{" + + "StuID='" + StuID + '\'' + + ", name='" + name + '\'' + + ", age=" + age + + ", sex='" + sex + '\'' + + '}'; + } +} +``` + +```java +package Light; + +import java.util.ArrayList; +import java.util.Scanner; + +public class T { + public static void main(String[] args) { + ArrayList list=new ArrayList<>(); + Scanner sc=new Scanner(System.in); + + sum: + for (int i=0;;){ + System.out.println("============学生系统============"); + System.out.println("请输入编号:"+ + "\n\t1 添加学生信息"+ + "\n\t2 修改学生信息"+ + "\n\t3 删除学生信息"+ + "\n\t4 查看学生信息"+ + "\n\t5 退出学生系统"); + System.out.println(); + System.out.print("请输入编号:"); + String sum=sc.next();; + switch (sum){ + case "1": +// System.out.println("添加"); + insertStudent(list,sc); + break; + case"2": + updateStudent(list,sc); + break; + case"3": + deleteStudent(list,sc); + break; + case"4": + lookStudent(list,sc); + break; + case"5": + System.out.println("成功退出学生系统"); + break sum; + default: + System.out.println("输入编号错误,请重新输入!!!"); + System.out.println(); + } + } + } + + + /** + * 添加 + * @param list + * @param sc + */ + public static void insertStudent(ArrayList list, Scanner sc){ + System.out.println(); + System.out.println("===*请输入需要添加学生信息*==="); + System.out.println("请输入学号"); + String stuId=sc.next(); + + if (!list.isEmpty()){ + for (int i = 0; i< list.size(); i++){ + Student stu= list.get(i); + if (stuId.equals(stu.getStuID())){ + System.out.println("学号已经存在"); + break; + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + } + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + System.out.println(); + } + + + /** + * 修改 + * @param list + * @param sc + */ + + public static void updateStudent(ArrayList list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); + }else { + System.out.println("===修改学生信息==="); + System.out.println("请输入需要修改的学生学号"); + String stuId=sc.next(); + for (int i=0;i list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); + }else { + System.out.println("==*删除学生信息*=="); + System.out.println("请输入需要删除的学生学号"); + String stuId=sc.next(); + + for (int i=0;i list,Scanner sc){ + System.out.println(); + + if (list.isEmpty()) { + System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); + }else { + System.out.println("==*查看学生信息*=="); + System.out.println("请输入需要查看学生的学生学号"); + String stuId=sc.next(); + + for (int i=0;i Date: Mon, 8 May 2023 12:27:07 +0000 Subject: [PATCH 09/17] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2025?= =?UTF-8?q?=20=E6=9D=8E=E6=96=87=E6=9D=B0/20230330=20Java=E7=AC=AC?= =?UTF-8?q?=E4=B8=80=E6=AC=A1=E4=BD=9C=E4=B8=9A.md?= 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" | 260 ------------------ 1 file changed, 260 deletions(-) delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index 5000ca1..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,260 +0,0 @@ -```java -import java.util.Scanner; - -public class Class { -// **1、判断一个字符数据是否是数字字符 ** -// -// **分析:** -// -// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 -// -// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, -// 并且还要下于或等于字符9即可。 -// -// 3、判断完成之后,打印判断的结果。 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - char i = sc.next.charAt(0); - if (i>=0 && i<10){ - System.out.println("是数字字符"); - }else { - System.out.println("不是数字字符"); - } - } -} - -import java.util.Scanner; - -public class java2 { - /* **2、判断一个字符数据是否是字母字符** - - **分析:** - - 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 - - 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, - 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z - - 3、判断完成之后,打印判断的结果。*/ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - char st = sc.next().charAt(0); - - if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) - - System.out.println( "是字母"); - - else - - System.out.println("不是字母"); - - } -} - - -import java.util.Scanner; - -public class java3 { -/* - **3、判断指定的年份是否为闰年,请使用键盘录入** - - **分析:** - - 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 - - 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 - -*/ - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int i = sc.nextInt(); - if ((i % 4 == 0 && i% 100 ==0) || i % 400 != 0 ){ - System.out.println("是闰年"); - }else{ - System.out.println("不是闰年"); - } - } -} - - -import java.util.Scanner; - -public class java4 { - // **4、判断一个数字是否为水仙花数,请使用键盘录入** -// -// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, -// -// **分析:** -// -// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 -// -// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 -// -// 2、判断的过程 -// -// a) 将3位数字的每一位上的数字拆分下来 -// -// b) 计算每位数字的3次幂之和 -// -// C) 用和值 和 原来的数字进行比较 -// -// D) 打印判断的比较结果即可 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入一个数字判断是不是水仙花数"); - int i = sc.nextInt(); - int ge = i % 10; - int si = i / 10 % 10; - int bai = i / 100 % 10; - if (i > 99 && i < 1000) { - if (ge * ge * ge + si * si * si + bai * bai * bai == i) { - System.out.println("是水仙花数"); - } else { - System.out.println("不是水仙花数"); - } - }else { - System.out.println("您好,水仙花数是三位数"); - } - } -} - - -import java.util.Scanner; - -public class java5 { - /* **5、判断一个5位数字是否为回文数,使用键盘录入** - - 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 - - **分析:** - - 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 - - 2、判断的过程 - - a) 将5位数字的万、千、十、个位数拆分出来 - - b) 判断比较万位和个位 、 千位和十位是否相等 - - 3、判断完成之后,打印判断的结果。*/ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入五位数判断是不是回文数"); - int i = sc.nextInt(); - int ge = i % 10; - int si = i / 10 % 10; - int qian = i / 1000 % 10; - int wan = i / 10000 % 10; - if (i > 9999 && i < 100000) { - if (ge == wan && si == qian) { - System.out.println("是回文数"); - } else { - System.out.println("不是回文数"); - } - }else { - System.out.println("您输入的不是五位数"); - } - } -} - - -import java.util.Scanner; - -public class java6 { -// ## 题目1(训练) -// -// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: -// -// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 -// -// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 -// -// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? -// -// ### 训练提示 -// -//1. 已知的父母身高如何用代码体现? -// -// 2. 题目中的公式如何转化为代码? -// -// ### 解题方案 -// -//1. 使用变量的定义和算术运算符完成本题 -// -//### 操作步骤 -// -//1. 定义小数变量代表父亲身高 -// -//2. 定义小数变量代表母亲身高 -// -//3. 通过儿子身高计算方式计算儿子身高 -// -//4. 通过女儿身高计算方式计算女人身高 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入父亲的身高"); - double baba = sc.nextInt(); - System.out.println("请输入母亲的身高"); - double mama = sc.nextInt(); - double erzi = (baba + mama) * 1.08 / 2; - double nver = (baba * 0.923 + mama) / 2; - System.out.println("儿子的身高为:" + erzi); - System.out.println("女儿的身高为:" + nver); - } -} - -public class java7 { -// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 -// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 -// 那么红茶和绿茶现在的钱一样多,请问对么? - public static void main(String[] args) { - int hocha = 21; - int lvcha = 24; - hocha = hocha * 2 + 3; - lvcha = lvcha * 2; - if (hocha == lvcha){ - System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); - }if (hocha > lvcha){ - System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); - }if (hocha < lvcha){ - System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); - } - } -} - - -public class java8 { -// 某小伙想定一份外卖,商家的优惠方式如下: -// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, -// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? - /*训练提示 - 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 - 解题方案 - 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 - 操作步骤 - 1. 使用算术运算符求出不使用优惠时的总价 - 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 - 3. 使用算术运算符求出使用优惠价时的总价 - 4. 使用三元运算符判断最终更合算的购买方式和花费*/ - public static void main(String[] args) { - int yuxian = 24; - int youza = 8; - int mifan = 3; - int zojia = yuxian + youza + mifan; - double youhuijia = zojia * 0.8; - System.out.println("使用折扣的总价:" + youhuijia); - int yuxian2 = 16; - int youza2 = 8; - int mifan2 = 3; - int zojia2 = yuxian2 + youza2 + mifan2; - System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); - if (youhuijia < zojia2){ - System.out.println("使用这个:" + youhuijia ); - }else if (youhuijia > zojia2){ - System.out.println("使用这个:" + zojia2); - } - } -} -``` - -- Gitee From e20422da7cff3f8f68d7a7dc2e48fe19cec9f1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0?= <11802249+lwjlmy@user.noreply.gitee.com> Date: Mon, 8 May 2023 12:27:27 +0000 Subject: [PATCH 10/17] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2025?= =?UTF-8?q?=20=E6=9D=8E=E6=96=87=E6=9D=B0/20230331=20java=E5=9F=BA?= =?UTF-8?q?=E7=A1=80=E8=AF=AD=E6=B3=95=E8=AE=AD=E7=BB=83.md?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...55\346\263\225\350\256\255\347\273\203.md" | 149 ------------------ 1 file changed, 149 deletions(-) delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" deleted file mode 100644 index c7f47ac..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" +++ /dev/null @@ -1,149 +0,0 @@ -```java -package My; - -import javax.lang.model.SourceVersion; - -public class Class { - public static void main(String[] args) { - // 第一题 - //(1)先声明两个byte类型的变量b1,b2, - // 并分别赋值为10和20,求b1和b2变量的和, - // 并将结果保存在byte类型的变量b3中, - // 最后输出b3变量的值 - byte b1,b2; - b1=10; - b2=20; - byte b3=(byte)(b1+b2); - System.out.println("byte类型的b1和b2的和为:"+(b3)); - -// 第二题 -// (2)先声明两个short类型的变量s1,s2, -// 并分别赋值为1000和2000,求s1和s2变量的和, -// 并将结果保存在short类型的变量s3中, -// 最后输出s3变量的值 - short s1=1000,s2=2000; - short s3=(short)(s1+s2); - System.out.println("short类型的是s1和s2的和为:"+s3); - -// 第三题 -// (3)先声明1个char类型的变量c1赋值为'a', -// 再声明一个int类型的变量num赋值为5,求c1和num变量的和, -// 并将结果将结果保存在char类型的变量letter中, -// 最后输出letter变量的值。 - char c1='a'; - int num=5; - char letter=(char)(c1+(char)num); - System.out.println("char类型的c1和int类型的num的和:"+(letter)); - -// 第四题 -// (4)先声明两个int类型的变量i1,i2, -// 并分别赋值5和2,求i1和i2的商, -// 并将结果保存在double类型的变量result中, -// 最后输出result变量的值。如何得到结果2.5呢? - int i1=5,i2=2; - double result=(double) i1/i2; - System.out.println("i1和i2的商为:"+(result)); - } -} - - -package My; - -public class Class1 { - public static void main(String[] args) { - int a1=10,a2=11; - System.out.println("10是偶数?"+(a1%2==0)); - System.out.println("11是偶数?"+(a2%2==0)); - int a3=12,a4=13; - System.out.println("12是奇数?"+(a3%2!=0)); - System.out.println("13是奇数?"+(a4%2!=0)); - } -} - -package My; - -public class Class3 { - public static void main(String[] args) { -// 题目:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时? - int hours=89; - int day=hours/24; - int hour=hours%24; - System.out.println("为抵抗洪水,战士连续作战89小时:"+"89小时是"+(day)+"天"+(hour)+"小时"); - - } -} - -package My; - -public class Class4 { - public static void main(String[] args) { -// 题目:今天是周2,100天以后是周几? - int week=2; - week+=100; - week%=7; - System.out.println("今天是周二,100天以后是周"+(week)); - } -} - -package My; - -import java.util.Scanner; - -public class Class2 { - public static void main(String[] args) { -// ## 5、案例:求三个整数x,y,z中的最大值 -// -// -// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 -// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) -// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) -// 4. 输出结果 - Scanner sc=new Scanner(System.in); - System.out.print("请输入x的值:"); - int x=sc.nextInt(); - System.out.print("请输入y的值:"); - int y=sc.nextInt(); - System.out.print("请输入z的值:"); - int z=sc.nextInt(); - int max; - max=(x>y)?x:y; - max=(max>z)?max:z; - System.out.println(max); - } -} - - - -package My; - -public class Class5 { - public static void main(String[] args) { -// 题目:判断今年是否是闰年 - int year=2023; - boolean a=(year%4==0 && year%100!=0); - boolean b=(year%400==0); - if(a || b){ - System.out.println(year+"是闰年"); - }else { - System.out.println(year + "不是闰年"); - } - } -} - - -package My; - -public class Class6 { - public static void main(String[] args) { -// 题目:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。 -// 它需要一个程序将华氏温度(80度)转换为摄氏度, -// 并以华氏度和摄氏度为单位分别显示该温度。 - double hua=80; - double she; - she=(hua-32)/1.8; - System.out.println("华氏度80.0度转化为摄氏度是"+(she)+"度"); - } -} - -``` - -- Gitee From a27b4b3cff3d3ac0756009cca2ef9435735ec52f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0?= <11802249+lwjlmy@user.noreply.gitee.com> Date: Mon, 8 May 2023 12:32:14 +0000 Subject: [PATCH 11/17] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2025?= =?UTF-8?q?=20=E6=9D=8E=E6=96=87=E6=9D=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\347\273\204\344\275\234\344\270\232.md" | 148 ---- ...71\350\261\241\344\275\234\344\270\232.md" | 47 -- ...32\345\222\214\347\254\224\350\256\260.md" | 130 ---- .../20230414 java\344\275\234\344\270\232.md" | 682 ------------------ ...32\345\222\214\347\254\224\350\256\260.md" | 290 -------- ...06\345\220\210\344\275\234\344\270\232.md" | 205 ------ ...41\347\220\206\347\263\273\347\273\237.md" | 254 ------- 7 files changed, 1756 deletions(-) delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230408 java\347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230415 JavaAPI\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" deleted file mode 100644 index de5fc8d..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" +++ /dev/null @@ -1,148 +0,0 @@ -```java -public class T1 { - public static void main(String[] args) { -// .定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** -// -//**操作步骤:** -// -// 1.定义5个元素数组 -// 2.可以使用初始化数组的两种方式之一为数组元素赋值 -// 3.遍历数组求数组中的最小值 - - - int[] sum={12,32,52,2,5};//创立数组并赋值 - - int min=sum[0]; - for (int i=1;isum[i]){ - min=sum[i]; - } - } - System.out.println("数组sum的最小值为:"+min); - } -} -``` - - - -```java -package Glass; - -import java.util.Arrays; - -public class T2 { - public static void main(String[] args) { -// 需求:求出数组中索引与索引对应的元素都是奇数的元素** -// -//**分析:** -// -// 1、遍历数组 -// 2、判断索引是否是奇数(索引 % 2 != 0) -// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) -// 4、满足条件输出结果 - - int[] sum={12,4,32,6,32,11}; - System.out.println(Arrays.toString(sum)); - - for (int i=0;i= 0; - } - -// 包含一个方法int approximateNumberCount()方法, -// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 - public int approximateNumberCount(){ - int count = 0; - for (int i = 1; i <= value; i++) { - if (value % i == 0){ - count++; - } - } - return count; - } - -// 包含一个方法boolean isPrimeNumber()方法, -// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, -// 并且value是大于1的自然数,那么value就是素数。 - public boolean isPrimeNumber(){ - int count = 0; - boolean flag = false; - for (int i = 1; i < value ; i++) { - count++; - if (value % i == 0 && value % 1 == 0 && value > 1){ - if (count >= 3){ - flag = false; - }else { - flag = true; - } - } - } - return flag; - } -// 包含一个方法int[] getAllPrimeNumber()方法, -// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 - public int[] getAllPrimeNumber(){ - int[] arr = new int[approximateNumberCount()]; - int index = arr[0]; - for (int i = 1; i <= value; i++) { - if (value % i == 0){ - arr[index++]=i; - } - } - return arr; - } - - @Override - public String toString() { - return "MyInt{" + - "\nvalue=" + value + - "\n是不是自然数:" + isNatural() + - "\n约数个数:" + approximateNumberCount() + - "\n是不是素数:" + isPrimeNumber() + - "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + - "\n}"; - } -} - -package T6; - -public class TestMyLnt { - public static void main(String[] args) { - MyLnt lnt = new MyLnt(20); - System.out.println(lnt); - } -} - -``` - -# 第七题 - -```java -package T7; - -public class TestYue { - public static void main(String[] args) { - Yue y = new Yue(); - int min = y.min(44,33,88,342,454,234,5,2); - System.out.println("最小值为:" + min); - int max = y.maxApproximate(4,8,16,20,24); - System.out.println("最大公约数为:" + max); - } -} - - -package T7; - -public class Yue { -// 返回n个整数中的最小值 - int min(int... nums){ - int min = nums[0]; - for (int i = 0; i < nums.length; i++) { - if (min > nums[i]){ - min = nums[i]; - } - } - return min; - } - - public int maxApproximate(int... nums){ - int max=min(nums); - for (int i = max; i >= 1; i--) { - boolean flag=true; - for (int j = 0; j < nums.length; j++) { - if (nums[j]%i!=0){ - flag=false; - break; - } - } - if (flag==true){ - return i; - } - } - return 1; - } -} - -``` - -# 第八题 - -```java -package T8; - -public class ArraysTools { - public String toString(int[]arr) { - String str = "["; - for (int i = 0; i < arr.length; i++) { - int i1 = arr[i]; - str += i1; - if (i < arr.length - 1) { - str += ","; - } - } - str += "]"; - return str; - } - int[] grow(int[] arr){ - int shu = arr.length; - int[] newArr = new int[2 * shu]; - for (int i = 0; i < arr.length; i++) { - newArr[i] = arr[i]; - } - return newArr; - } -} - -package T8; - -public class Test { - public static void main(String[] args) { - int[]old={11,22,33,44}; - ArraysTools arraysTools = new ArraysTools(); - System.out.println(arraysTools.toString(old)); - int[]newArr= arraysTools.grow(old); - System.out.println(arraysTools.toString(newArr)); - } -} - -``` - -# 第九题 - -```java -package T9; - -public class MathTools { - int compare(int a, int b){ - if (a > b){ - return a; - }else if (a < b){ - return -a; - }else { - return 0; - } - } -/* -* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, -* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; -* */ - int compare(double a, double b){ - if (a > b){ - return (int)a; - }else if (a < b){ - return (int)-a; - }else { - return 0; - } - } - -// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, -// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; - int compare(char a, char b){ - if (a > b){ - return (char)a; - }else if (a < b){ - return (char)-a; - }else { - return 0; - } - } -} - - -package T9; - -public class Test { - public static void main(String[] args) { - MathTools ma = new MathTools(); - int a = ma.compare(3,5); - int b = ma.compare('2','3'); - int c = ma.compare(33.5,22.4); - System.out.println(a); - System.out.println(b); - System.out.println(c); - } -} - -``` - -# 第十题 - -```java -package T10; - -public class ArraysTools { - public void sort(int[] arr){ - for (int i = 1; i < arr.length; i++) { - for (int j = 0; j < arr.length - i; j++) { - if (arr[j] > arr[j + 1]){ - int temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } - public void sort(double[] arr){ - for (int i= 1; i arr[j+1]){ - double temp=arr[j]; - arr[j]=arr[j+1]; - arr[j+1]=temp; - } - } - } - } - - public void sort(char[] arr) { - for (int i = 1; i < arr.length; i++) { - for (int j = 0; j < arr.length - i; j++) { - if (arr[j] > arr[j + 1]) { - char temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } - public String toString(int[] arr){ - String re="["; - for (int i = 0; i < arr.length; i++) { - if (i我***你 - -##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 - -String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] - -## String方法小结 - -| **方法名** | **说明** | -| ----------------------------------------------------- | -------------------------------- | -| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | -| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | -| public int length() | 返回此字符串的长度 | - -| public char charAt(int index) | 返回指定索引处的 char 值 | -| ----------------------------- | ---------------------------- | -| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | - -| **方法名** | **说明** | -| ------------------------------------------------------------ | ------------------------------------------------------------ | -| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | -| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | -| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | - -| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | -| ----------------------------------- | ---------------------------------------- | -| | | - -## StringBuilder - -StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 - -作用:提高字符串的操作效率 - -### StringBuilder 构造方法 - -| **方法名** | **说明** | -| -------------------------------- | ------------------------------------------ | -| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | -| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | - -### StringBuilder 的常用方法 - -| **方法名** | **说明** | -| -------------------------------------- | ------------------------ | -| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | -| public StringBuilder reverse() | 返回相反的字符序列 | - -| public int length() | 返回长度 ( 字符出现的个数) | -| ------------------- | -------------------------- | -| | | - -| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | -| ------------------------ | --------------------------------------------------- | -| | | - -## StringBuilder和String的区别 - -**String** :内容是不可变的 - -**StringBuilder**:内容是可变的 - -## StringBuilder 和 String 相互转化 - -### StringBuilder 转换为 String - -public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String - -### String 转换为 StringBuilder - -public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder - -# 作业 - -```java -import java.util.Scanner; - -public class Test1 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - String password = "12345"; - System.out.println("请输入密码登陆"); - int count = 0; - for (int i = 0; i < 3; i++) { - String st = sc.next(); - count++; - if (st.equals(password)){ - System.out.println("密码正确,登陆成功"); - break; - }else { - System.out.println("密码错误"); - System.out.println("您还有" + (3 - count) + "次"); - } - } - } -} - - -import java.util.Scanner; - -public class Test2 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入字符串遍历数组"); - String st = sc.next(); - for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ - da++; - } - if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ - xiao++; - } - if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ - shu++; - } - } - System.out.println("大写出现的次数:" + da); - System.out.println("小写出现的次数:" + xiao); - System.out.println("数字出现的次数:" + shu); - } -} - - -import javax.swing.*; -import java.util.Scanner; - -public class Test4 { - public static void main(String[] args) { - System.out.println("请输入电话号码"); - Scanner sc = new Scanner(System.in); - String st = sc.next(); - String s1 = st.substring(0,3); - String s2 = st.substring(7); - System.out.println("转换后的电话号码为:" + s1 + "****" + s2); - } -} - - -import java.util.Scanner; - -public class Test5 { - public static void main(String[] args) { - System.out.println("请输入脏话"); - Scanner sc = new Scanner(System.in); - String st = sc.next(); - String s1 = st.replace("TMD","不可以说脏话"); - System.out.println(s1); - } -} - - -public class Student { - private String name; - private int age; - - public Student(String name,int age){ - this.name = name; - this.age = age; - } - - public Student(){ - - } - - public void setName(String name){ - this.name = name; - } - - public void setAge(int age){ - this.age = age; - } - - public String getName(){ - return this.name = name; - } - - public int getAge(){ - return this.age = age; - } -} - - -import java.util.Arrays; -import java.util.Scanner; - -public class TestStudent6 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - Student st = new Student(); - System.out.println("请输入名字"); - st.setName(sc.next()); - System.out.println("请输入年龄"); - st.setAge(sc.nextInt()); - String st2 = st.getName() +","+ st.getAge(); - String[] arr = st2.split(","); - System.out.println(Arrays.toString(arr)); - - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" deleted file mode 100644 index 4be6e5e..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" +++ /dev/null @@ -1,205 +0,0 @@ -```java -//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 -public class Student { - private String name; - private int age; - - public Student(String name, int age) { - this.name = name; - this.age = age; - } - - public Student(String name) { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - -} - -import java.util.ArrayList; - -public class TestStudent { - public static void main(String[] args) { - ArrayList list = new ArrayList<>(); - list.add(new Student("张三",18)); - list.add(new Student("李四",19)); - list.add(new Student("王五",20)); - for (int i = 0; i < list.size(); i++) { - Student stu = list.get(i); - System.out.print(stu.getName() + " "); - System.out.println(stu.getAge()); - } - } -} - -``` - -```java -//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 -//学生的姓名和年龄来自于键盘录入 -public class Student { - private String name; - private int age; - - public Student(String name, int age) { - this.name = name; - this.age = age; - } - - public Student(String name) { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - -} -import java.security.AllPermission; -import java.util.ArrayList; -import java.util.Scanner; - -public class TestStudent2 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int count = 0; - ArrayList list = new ArrayList<>(); - while (true) { - count++; - System.out.println("请输入第" + count + "名学生的姓名和年龄"); - list.add(new Student(sc.next(), sc.nextInt())); - if (count == 3){ - break; - } - } - for (int i = 0; i < list.size(); i++) { - Student stu = list.get(i); - System.out.print(stu.getName() + " "); - System.out.println(stu.getAge()); - } - } -} - -``` - -```java - -import java.util.ArrayList; -import java.util.Iterator; - -public class T2 { - public static void main(String[] args) { -// 需求:创建一个存储String的集合,内部存储 -// (test,张三,李四,test,test)字符串 -// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 - ArrayList list = new ArrayList<>(); - list.add("test"); - list.add("张三"); - list.add("李四"); - list.add("test"); - list.add("test"); -// Iterator it = list.iterator(); -// while(it.hasNext()){ -// String str = it.next(); -// if (str.contains("test")){ -// it.remove(); -// } -// } - list.removeIf(s -> s.contains("test")); - System.out.println(list); - } -} - - -``` - -```java -//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 -//并存入新集合对象,方法返回新集合。 -public class Student { - private String name; - private int age; - public Student(String name,int age){ - this.name = name; - this.age = age; - } - - public Student(){ - - } - - public void setName(String name){ - this.name = name; - } - public void setAge(int age){ - this.age = age; - } - - public String getName(){ - return this.name = name; - } - - public int getAge(){ - return this.age = age; - } - - - public String toString(){ - return "姓名:" + getName() + " " + "年龄:" + getAge(); - } -} - -import java.util.ArrayList; - -public class TestStudent { - public static void main(String[] args) { - ArrayList list = new ArrayList<>(); - list.add(new Student("张三", 19)); - list.add(new Student("李四", 20)); - list.add(new Student("王五", 10)); - list.add(new Student("赵六", 15)); - chaXun(new ArrayList<>(list)); - } - public static ArrayList chaXun(ArrayList list - ){ - ArrayList list1 = new ArrayList<>(); - for (int i = 0; i < list.size(); i++) { - Student st = list.get(i); - if (st.getAge()<18){ - list1.add(list.get(i)); - } - } - System.out.println(list1); - return list1; - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" deleted file mode 100644 index ebaa434..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" +++ /dev/null @@ -1,254 +0,0 @@ -```java -package Light; - -public class Student { - private String StuID; - private String name; - private int age; - private String sex; - - - - public String getStuID() { - return StuID; - } - - public void setStuID(String stuID) { - StuID = stuID; - } - - public String getSex() { - return sex; - } - - public void setSex(String sex) { - this.sex = sex; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Student(){} - public Student(String StuID,String name,int age,String sex){ - this.StuID=StuID; - this.name=name; - this.age=age; - this.sex=sex; - } - - @Override - public String toString() { - return "Student{" + - "StuID='" + StuID + '\'' + - ", name='" + name + '\'' + - ", age=" + age + - ", sex='" + sex + '\'' + - '}'; - } -} -``` - -```java -package Light; - -import java.util.ArrayList; -import java.util.Scanner; - -public class T { - public static void main(String[] args) { - ArrayList list=new ArrayList<>(); - Scanner sc=new Scanner(System.in); - - sum: - for (int i=0;;){ - System.out.println("============学生系统============"); - System.out.println("请输入编号:"+ - "\n\t1 添加学生信息"+ - "\n\t2 修改学生信息"+ - "\n\t3 删除学生信息"+ - "\n\t4 查看学生信息"+ - "\n\t5 退出学生系统"); - System.out.println(); - System.out.print("请输入编号:"); - String sum=sc.next();; - switch (sum){ - case "1": -// System.out.println("添加"); - insertStudent(list,sc); - break; - case"2": - updateStudent(list,sc); - break; - case"3": - deleteStudent(list,sc); - break; - case"4": - lookStudent(list,sc); - break; - case"5": - System.out.println("成功退出学生系统"); - break sum; - default: - System.out.println("输入编号错误,请重新输入!!!"); - System.out.println(); - } - } - } - - - /** - * 添加 - * @param list - * @param sc - */ - public static void insertStudent(ArrayList list, Scanner sc){ - System.out.println(); - System.out.println("===*请输入需要添加学生信息*==="); - System.out.println("请输入学号"); - String stuId=sc.next(); - - if (!list.isEmpty()){ - for (int i = 0; i< list.size(); i++){ - Student stu= list.get(i); - if (stuId.equals(stu.getStuID())){ - System.out.println("学号已经存在"); - break; - }else { - System.out.println("请输入姓名"); - String stuName=sc.next(); - System.out.println("请输入年龄"); - int stuAge=sc.nextInt(); - System.out.println("请输入性别"); - String stuSex=sc.next(); - list.add(new Student(stuId,stuName,stuAge,stuSex)); - } - } - }else { - System.out.println("请输入姓名"); - String stuName=sc.next(); - System.out.println("请输入年龄"); - int stuAge=sc.nextInt(); - System.out.println("请输入性别"); - String stuSex=sc.next(); - list.add(new Student(stuId,stuName,stuAge,stuSex)); - } - System.out.println(); - } - - - /** - * 修改 - * @param list - * @param sc - */ - - public static void updateStudent(ArrayList list,Scanner sc){ - System.out.println(); - if (list.isEmpty()){ - System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); - }else { - System.out.println("===修改学生信息==="); - System.out.println("请输入需要修改的学生学号"); - String stuId=sc.next(); - for (int i=0;i list,Scanner sc){ - System.out.println(); - if (list.isEmpty()){ - System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); - }else { - System.out.println("==*删除学生信息*=="); - System.out.println("请输入需要删除的学生学号"); - String stuId=sc.next(); - - for (int i=0;i list,Scanner sc){ - System.out.println(); - - if (list.isEmpty()) { - System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); - }else { - System.out.println("==*查看学生信息*=="); - System.out.println("请输入需要查看学生的学生学号"); - String stuId=sc.next(); - - for (int i=0;i Date: Mon, 8 May 2023 20:34:13 +0800 Subject: [PATCH 12/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0=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" | 260 +++++++ ...55\346\263\225\350\256\255\347\273\203.md" | 149 ++++ ...60\347\273\204\344\275\234\344\270\232.md" | 148 ++++ ...71\350\261\241\344\275\234\344\270\232.md" | 47 ++ ...32\345\222\214\347\254\224\350\256\260.md" | 130 ++++ .../20230414 java\344\275\234\344\270\232.md" | 682 ++++++++++++++++++ ...32\345\222\214\347\254\224\350\256\260.md" | 290 ++++++++ ...06\345\220\210\344\275\234\344\270\232.md" | 205 ++++++ ...41\347\220\206\347\263\273\347\273\237.md" | 254 +++++++ 9 files changed, 2165 insertions(+) create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230408 java\347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230415 JavaAPI\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..63c6bc5 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,260 @@ +```java +import java.util.Scanner; + +public class Class { +// **1、判断一个字符数据是否是数字字符 ** +// +// **分析:** +// +// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 +// +// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, +// 并且还要下于或等于字符9即可。 +// +// 3、判断完成之后,打印判断的结果。 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char i = sc.next.charAt(0); + if (i>=0 && i<10){ + System.out.println("是数字字符"); + }else { + System.out.println("不是数字字符"); + } + } +} + +import java.util.Scanner; + +public class java2 { + /* **2、判断一个字符数据是否是字母字符** + + **分析:** + + 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 + + 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, + 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char st = sc.next().charAt(0); + + if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) + + System.out.println( "是字母"); + + else + + System.out.println("不是字母"); + + } +} + + +import java.util.Scanner; + +public class java3 { +/* + **3、判断指定的年份是否为闰年,请使用键盘录入** + + **分析:** + + 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 + + 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 + +*/ + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int i = sc.nextInt(); + if ((i % 4 == 0 && i% 100 ==0) || i % 400 != 0 ){ + System.out.println("是闰年"); + }else{ + System.out.println("不是闰年"); + } + } +} + + +import java.util.Scanner; + +public class java4 { + // **4、判断一个数字是否为水仙花数,请使用键盘录入** +// +// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, +// +// **分析:** +// +// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 +// +// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 +// +// 2、判断的过程 +// +// a) 将3位数字的每一位上的数字拆分下来 +// +// b) 计算每位数字的3次幂之和 +// +// C) 用和值 和 原来的数字进行比较 +// +// D) 打印判断的比较结果即可 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个数字判断是不是水仙花数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int bai = i / 100 % 10; + if (i > 99 && i < 1000) { + if (ge * ge * ge + si * si * si + bai * bai * bai == i) { + System.out.println("是水仙花数"); + } else { + System.out.println("不是水仙花数"); + } + }else { + System.out.println("您好,水仙花数是三位数"); + } + } +} + + +import java.util.Scanner; + +public class java5 { + /* **5、判断一个5位数字是否为回文数,使用键盘录入** + + 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 + + **分析:** + + 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 + + 2、判断的过程 + + a) 将5位数字的万、千、十、个位数拆分出来 + + b) 判断比较万位和个位 、 千位和十位是否相等 + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入五位数判断是不是回文数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int qian = i / 1000 % 10; + int wan = i / 10000 % 10; + if (i > 9999 && i < 100000) { + if (ge == wan && si == qian) { + System.out.println("是回文数"); + } else { + System.out.println("不是回文数"); + } + }else { + System.out.println("您输入的不是五位数"); + } + } +} + + +import java.util.Scanner; + +public class java6 { +// ## 题目1(训练) +// +// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: +// +// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 +// +// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 +// +// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? +// +// ### 训练提示 +// +//1. 已知的父母身高如何用代码体现? +// +// 2. 题目中的公式如何转化为代码? +// +// ### 解题方案 +// +//1. 使用变量的定义和算术运算符完成本题 +// +//### 操作步骤 +// +//1. 定义小数变量代表父亲身高 +// +//2. 定义小数变量代表母亲身高 +// +//3. 通过儿子身高计算方式计算儿子身高 +// +//4. 通过女儿身高计算方式计算女人身高 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入父亲的身高"); + double baba = sc.nextInt(); + System.out.println("请输入母亲的身高"); + double mama = sc.nextInt(); + double erzi = (baba + mama) * 1.08 / 2; + double nver = (baba * 0.923 + mama) / 2; + System.out.println("儿子的身高为:" + erzi); + System.out.println("女儿的身高为:" + nver); + } +} + +public class java7 { +// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 +// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 +// 那么红茶和绿茶现在的钱一样多,请问对么? + public static void main(String[] args) { + int hocha = 21; + int lvcha = 24; + hocha = hocha * 2 + 3; + lvcha = lvcha * 2; + if (hocha == lvcha){ + System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); + }if (hocha > lvcha){ + System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + }if (hocha < lvcha){ + System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + } + } +} + + +public class java8 { +// 某小伙想定一份外卖,商家的优惠方式如下: +// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, +// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? + /*训练提示 + 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 + 解题方案 + 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 + 操作步骤 + 1. 使用算术运算符求出不使用优惠时的总价 + 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 + 3. 使用算术运算符求出使用优惠价时的总价 + 4. 使用三元运算符判断最终更合算的购买方式和花费*/ + public static void main(String[] args) { + int yuxian = 24; + int youza = 8; + int mifan = 3; + int zojia = yuxian + youza + mifan; + double youhuijia = zojia * 0.8; + System.out.println("使用折扣的总价:" + youhuijia); + int yuxian2 = 16; + int youza2 = 8; + int mifan2 = 3; + int zojia2 = yuxian2 + youza2 + mifan2; + System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); + if (youhuijia < zojia2){ + System.out.println("使用这个:" + youhuijia ); + }else if (youhuijia > zojia2){ + System.out.println("使用这个:" + zojia2); + } + } +} +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" new file mode 100644 index 0000000..d8f1fa4 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" @@ -0,0 +1,149 @@ +```java +package My; + +import javax.lang.model.SourceVersion; + +public class Class { + public static void main(String[] args) { + // 第一题 + //(1)先声明两个byte类型的变量b1,b2, + // 并分别赋值为10和20,求b1和b2变量的和, + // 并将结果保存在byte类型的变量b3中, + // 最后输出b3变量的值 + byte b1,b2; + b1=10; + b2=20; + byte b3=(byte)(b1+b2); + System.out.println("byte类型的b1和b2的和为:"+(b3)); + +// 第二题 +// (2)先声明两个short类型的变量s1,s2, +// 并分别赋值为1000和2000,求s1和s2变量的和, +// 并将结果保存在short类型的变量s3中, +// 最后输出s3变量的值 + short s1=1000,s2=2000; + short s3=(short)(s1+s2); + System.out.println("short类型的是s1和s2的和为:"+s3); + +// 第三题 +// (3)先声明1个char类型的变量c1赋值为'a', +// 再声明一个int类型的变量num赋值为5,求c1和num变量的和, +// 并将结果将结果保存在char类型的变量letter中, +// 最后输出letter变量的值。 + char c1='a'; + int num=5; + char letter=(char)(c1+(char)num); + System.out.println("char类型的c1和int类型的num的和:"+(letter)); + +// 第四题 +// (4)先声明两个int类型的变量i1,i2, +// 并分别赋值5和2,求i1和i2的商, +// 并将结果保存在double类型的变量result中, +// 最后输出result变量的值。如何得到结果2.5呢? + int i1=5,i2=2; + double result=(double) i1/i2; + System.out.println("i1和i2的商为:"+(result)); + } +} + + +package My; + +public class Class1 { + public static void main(String[] args) { + int a1=10,a2=11; + System.out.println("10是偶数?"+(a1%2==0)); + System.out.println("11是偶数?"+(a2%2==0)); + int a3=12,a4=13; + System.out.println("12是奇数?"+(a3%2!=0)); + System.out.println("13是奇数?"+(a4%2!=0)); + } +} + +package My; + +public class Class3 { + public static void main(String[] args) { +// 题目:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时? + int hours=89; + int day=hours/24; + int hour=hours%24; + System.out.println("为抵抗洪水,战士连续作战89小时:"+"89小时是"+(day)+"天"+(hour)+"小时"); + + } +} + +package My; + +public class Class4 { + public static void main(String[] args) { +// 题目:今天是周2,100天以后是周几? + int week=2; + week+=100; + week%=7; + System.out.println("今天是周二,100天以后是周"+(week)); + } +} + +package My; + +import java.util.Scanner; + +public class Class2 { + public static void main(String[] args) { +// ## 5、案例:求三个整数x,y,z中的最大值 +// +// +// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 +// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) +// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) +// 4. 输出结果 + Scanner sc=new Scanner(System.in); + System.out.print("请输入x的值:"); + int x=sc.nextInt(); + System.out.print("请输入y的值:"); + int y=sc.nextInt(); + System.out.print("请输入z的值:"); + int z=sc.nextInt(); + int max; + max=(x>y)?x:y; + max=(max>z)?max:z; + System.out.println(max); + } +} + + + +package My; + +public class Class5 { + public static void main(String[] args) { +// 题目:判断今年是否是闰年 + int year=2023; + boolean a=(year%4==0 && year%100!=0); + boolean b=(year%400==0); + if(a || b){ + System.out.println(year+"是闰年"); + }else { + System.out.println(year + "不是闰年"); + } + } +} + + +package My; + +public class Class6 { + public static void main(String[] args) { +// 题目:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。 +// 它需要一个程序将华氏温度(80度)转换为摄氏度, +// 并以华氏度和摄氏度为单位分别显示该温度。 + double hua=80; + double she; + she=(hua-32)/1.8; + System.out.println("华氏度80.0度转化为摄氏度是"+(she)+"度"); + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000..dc4fd7e --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" @@ -0,0 +1,148 @@ +```java +public class T1 { + public static void main(String[] args) { +// .定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** +// +//**操作步骤:** +// +// 1.定义5个元素数组 +// 2.可以使用初始化数组的两种方式之一为数组元素赋值 +// 3.遍历数组求数组中的最小值 + + + int[] sum={12,32,52,2,5};//创立数组并赋值 + + int min=sum[0]; + for (int i=1;isum[i]){ + min=sum[i]; + } + } + System.out.println("数组sum的最小值为:"+min); + } +} +``` + + + +```java +package Glass; + +import java.util.Arrays; + +public class T2 { + public static void main(String[] args) { +// 需求:求出数组中索引与索引对应的元素都是奇数的元素** +// +//**分析:** +// +// 1、遍历数组 +// 2、判断索引是否是奇数(索引 % 2 != 0) +// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) +// 4、满足条件输出结果 + + int[] sum={12,4,32,6,32,11}; + System.out.println(Arrays.toString(sum)); + + for (int i=0;i= 0; + } + +// 包含一个方法int approximateNumberCount()方法, +// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 + public int approximateNumberCount(){ + int count = 0; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + count++; + } + } + return count; + } + +// 包含一个方法boolean isPrimeNumber()方法, +// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, +// 并且value是大于1的自然数,那么value就是素数。 + public boolean isPrimeNumber(){ + int count = 0; + boolean flag = false; + for (int i = 1; i < value ; i++) { + count++; + if (value % i == 0 && value % 1 == 0 && value > 1){ + if (count >= 3){ + flag = false; + }else { + flag = true; + } + } + } + return flag; + } +// 包含一个方法int[] getAllPrimeNumber()方法, +// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 + public int[] getAllPrimeNumber(){ + int[] arr = new int[approximateNumberCount()]; + int index = arr[0]; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + arr[index++]=i; + } + } + return arr; + } + + @Override + public String toString() { + return "MyInt{" + + "\nvalue=" + value + + "\n是不是自然数:" + isNatural() + + "\n约数个数:" + approximateNumberCount() + + "\n是不是素数:" + isPrimeNumber() + + "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + + "\n}"; + } +} + +package T6; + +public class TestMyLnt { + public static void main(String[] args) { + MyLnt lnt = new MyLnt(20); + System.out.println(lnt); + } +} + +``` + +# 第七题 + +```java +package T7; + +public class TestYue { + public static void main(String[] args) { + Yue y = new Yue(); + int min = y.min(44,33,88,342,454,234,5,2); + System.out.println("最小值为:" + min); + int max = y.maxApproximate(4,8,16,20,24); + System.out.println("最大公约数为:" + max); + } +} + + +package T7; + +public class Yue { +// 返回n个整数中的最小值 + int min(int... nums){ + int min = nums[0]; + for (int i = 0; i < nums.length; i++) { + if (min > nums[i]){ + min = nums[i]; + } + } + return min; + } + + public int maxApproximate(int... nums){ + int max=min(nums); + for (int i = max; i >= 1; i--) { + boolean flag=true; + for (int j = 0; j < nums.length; j++) { + if (nums[j]%i!=0){ + flag=false; + break; + } + } + if (flag==true){ + return i; + } + } + return 1; + } +} + +``` + +# 第八题 + +```java +package T8; + +public class ArraysTools { + public String toString(int[]arr) { + String str = "["; + for (int i = 0; i < arr.length; i++) { + int i1 = arr[i]; + str += i1; + if (i < arr.length - 1) { + str += ","; + } + } + str += "]"; + return str; + } + int[] grow(int[] arr){ + int shu = arr.length; + int[] newArr = new int[2 * shu]; + for (int i = 0; i < arr.length; i++) { + newArr[i] = arr[i]; + } + return newArr; + } +} + +package T8; + +public class Test { + public static void main(String[] args) { + int[]old={11,22,33,44}; + ArraysTools arraysTools = new ArraysTools(); + System.out.println(arraysTools.toString(old)); + int[]newArr= arraysTools.grow(old); + System.out.println(arraysTools.toString(newArr)); + } +} + +``` + +# 第九题 + +```java +package T9; + +public class MathTools { + int compare(int a, int b){ + if (a > b){ + return a; + }else if (a < b){ + return -a; + }else { + return 0; + } + } +/* +* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, +* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; +* */ + int compare(double a, double b){ + if (a > b){ + return (int)a; + }else if (a < b){ + return (int)-a; + }else { + return 0; + } + } + +// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, +// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; + int compare(char a, char b){ + if (a > b){ + return (char)a; + }else if (a < b){ + return (char)-a; + }else { + return 0; + } + } +} + + +package T9; + +public class Test { + public static void main(String[] args) { + MathTools ma = new MathTools(); + int a = ma.compare(3,5); + int b = ma.compare('2','3'); + int c = ma.compare(33.5,22.4); + System.out.println(a); + System.out.println(b); + System.out.println(c); + } +} + +``` + +# 第十题 + +```java +package T10; + +public class ArraysTools { + public void sort(int[] arr){ + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]){ + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public void sort(double[] arr){ + for (int i= 1; i arr[j+1]){ + double temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } + } + } + + public void sort(char[] arr) { + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]) { + char temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public String toString(int[] arr){ + String re="["; + for (int i = 0; i < arr.length; i++) { + if (i我***你 + +##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 + +String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] + +## String方法小结 + +| **方法名** | **说明** | +| ----------------------------------------------------- | -------------------------------- | +| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | +| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | +| public int length() | 返回此字符串的长度 | + +| public char charAt(int index) | 返回指定索引处的 char 值 | +| ----------------------------- | ---------------------------- | +| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | + +| **方法名** | **说明** | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | +| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | +| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | + +| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | +| ----------------------------------- | ---------------------------------------- | +| | | + +## StringBuilder + +StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 + +作用:提高字符串的操作效率 + +### StringBuilder 构造方法 + +| **方法名** | **说明** | +| -------------------------------- | ------------------------------------------ | +| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | +| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | + +### StringBuilder 的常用方法 + +| **方法名** | **说明** | +| -------------------------------------- | ------------------------ | +| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | +| public StringBuilder reverse() | 返回相反的字符序列 | + +| public int length() | 返回长度 ( 字符出现的个数) | +| ------------------- | -------------------------- | +| | | + +| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | +| ------------------------ | --------------------------------------------------- | +| | | + +## StringBuilder和String的区别 + +**String** :内容是不可变的 + +**StringBuilder**:内容是可变的 + +## StringBuilder 和 String 相互转化 + +### StringBuilder 转换为 String + +public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String + +### String 转换为 StringBuilder + +public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder + +# 作业 + +```java +import java.util.Scanner; + +public class Test1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String password = "12345"; + System.out.println("请输入密码登陆"); + int count = 0; + for (int i = 0; i < 3; i++) { + String st = sc.next(); + count++; + if (st.equals(password)){ + System.out.println("密码正确,登陆成功"); + break; + }else { + System.out.println("密码错误"); + System.out.println("您还有" + (3 - count) + "次"); + } + } + } +} + + +import java.util.Scanner; + +public class Test2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入字符串遍历数组"); + String st = sc.next(); + for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ + da++; + } + if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ + xiao++; + } + if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ + shu++; + } + } + System.out.println("大写出现的次数:" + da); + System.out.println("小写出现的次数:" + xiao); + System.out.println("数字出现的次数:" + shu); + } +} + + +import javax.swing.*; +import java.util.Scanner; + +public class Test4 { + public static void main(String[] args) { + System.out.println("请输入电话号码"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.substring(0,3); + String s2 = st.substring(7); + System.out.println("转换后的电话号码为:" + s1 + "****" + s2); + } +} + + +import java.util.Scanner; + +public class Test5 { + public static void main(String[] args) { + System.out.println("请输入脏话"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.replace("TMD","不可以说脏话"); + System.out.println(s1); + } +} + + +public class Student { + private String name; + private int age; + + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } +} + + +import java.util.Arrays; +import java.util.Scanner; + +public class TestStudent6 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Student st = new Student(); + System.out.println("请输入名字"); + st.setName(sc.next()); + System.out.println("请输入年龄"); + st.setAge(sc.nextInt()); + String st2 = st.getName() +","+ st.getAge(); + String[] arr = st2.split(","); + System.out.println(Arrays.toString(arr)); + + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" new file mode 100644 index 0000000..db5bfa5 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" @@ -0,0 +1,205 @@ +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三",18)); + list.add(new Student("李四",19)); + list.add(new Student("王五",20)); + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +//学生的姓名和年龄来自于键盘录入 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} +import java.security.AllPermission; +import java.util.ArrayList; +import java.util.Scanner; + +public class TestStudent2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int count = 0; + ArrayList list = new ArrayList<>(); + while (true) { + count++; + System.out.println("请输入第" + count + "名学生的姓名和年龄"); + list.add(new Student(sc.next(), sc.nextInt())); + if (count == 3){ + break; + } + } + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java + +import java.util.ArrayList; +import java.util.Iterator; + +public class T2 { + public static void main(String[] args) { +// 需求:创建一个存储String的集合,内部存储 +// (test,张三,李四,test,test)字符串 +// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 + ArrayList list = new ArrayList<>(); + list.add("test"); + list.add("张三"); + list.add("李四"); + list.add("test"); + list.add("test"); +// Iterator it = list.iterator(); +// while(it.hasNext()){ +// String str = it.next(); +// if (str.contains("test")){ +// it.remove(); +// } +// } + list.removeIf(s -> s.contains("test")); + System.out.println(list); + } +} + + +``` + +```java +//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 +//并存入新集合对象,方法返回新集合。 +public class Student { + private String name; + private int age; + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } + + + public String toString(){ + return "姓名:" + getName() + " " + "年龄:" + getAge(); + } +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三", 19)); + list.add(new Student("李四", 20)); + list.add(new Student("王五", 10)); + list.add(new Student("赵六", 15)); + chaXun(new ArrayList<>(list)); + } + public static ArrayList chaXun(ArrayList list + ){ + ArrayList list1 = new ArrayList<>(); + for (int i = 0; i < list.size(); i++) { + Student st = list.get(i); + if (st.getAge()<18){ + list1.add(list.get(i)); + } + } + System.out.println(list1); + return list1; + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000..bc280be --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,254 @@ +```java +package Light; + +public class Student { + private String StuID; + private String name; + private int age; + private String sex; + + + + public String getStuID() { + return StuID; + } + + public void setStuID(String stuID) { + StuID = stuID; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Student(){} + public Student(String StuID,String name,int age,String sex){ + this.StuID=StuID; + this.name=name; + this.age=age; + this.sex=sex; + } + + @Override + public String toString() { + return "Student{" + + "StuID='" + StuID + '\'' + + ", name='" + name + '\'' + + ", age=" + age + + ", sex='" + sex + '\'' + + '}'; + } +} +``` + +```java +package Light; + +import java.util.ArrayList; +import java.util.Scanner; + +public class T { + public static void main(String[] args) { + ArrayList list=new ArrayList<>(); + Scanner sc=new Scanner(System.in); + + sum: + for (int i=0;;){ + System.out.println("============学生系统============"); + System.out.println("请输入编号:"+ + "\n\t1 添加学生信息"+ + "\n\t2 修改学生信息"+ + "\n\t3 删除学生信息"+ + "\n\t4 查看学生信息"+ + "\n\t5 退出学生系统"); + System.out.println(); + System.out.print("请输入编号:"); + String sum=sc.next();; + switch (sum){ + case "1": +// System.out.println("添加"); + insertStudent(list,sc); + break; + case"2": + updateStudent(list,sc); + break; + case"3": + deleteStudent(list,sc); + break; + case"4": + lookStudent(list,sc); + break; + case"5": + System.out.println("成功退出学生系统"); + break sum; + default: + System.out.println("输入编号错误,请重新输入!!!"); + System.out.println(); + } + } + } + + + /** + * 添加 + * @param list + * @param sc + */ + public static void insertStudent(ArrayList list, Scanner sc){ + System.out.println(); + System.out.println("===*请输入需要添加学生信息*==="); + System.out.println("请输入学号"); + String stuId=sc.next(); + + if (!list.isEmpty()){ + for (int i = 0; i< list.size(); i++){ + Student stu= list.get(i); + if (stuId.equals(stu.getStuID())){ + System.out.println("学号已经存在"); + break; + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + } + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + System.out.println(); + } + + + /** + * 修改 + * @param list + * @param sc + */ + + public static void updateStudent(ArrayList list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); + }else { + System.out.println("===修改学生信息==="); + System.out.println("请输入需要修改的学生学号"); + String stuId=sc.next(); + for (int i=0;i list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); + }else { + System.out.println("==*删除学生信息*=="); + System.out.println("请输入需要删除的学生学号"); + String stuId=sc.next(); + + for (int i=0;i list,Scanner sc){ + System.out.println(); + + if (list.isEmpty()) { + System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); + }else { + System.out.println("==*查看学生信息*=="); + System.out.println("请输入需要查看学生的学生学号"); + String stuId=sc.next(); + + for (int i=0;i Date: Mon, 8 May 2023 12:37:52 +0000 Subject: [PATCH 13/17] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2025?= =?UTF-8?q?=20=E6=9D=8E=E6=96=87=E6=9D=B0?= 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" | 260 ------- ...55\346\263\225\350\256\255\347\273\203.md" | 149 ---- ...60\347\273\204\344\275\234\344\270\232.md" | 148 ---- ...71\350\261\241\344\275\234\344\270\232.md" | 47 -- ...32\345\222\214\347\254\224\350\256\260.md" | 130 ---- .../20230414 java\344\275\234\344\270\232.md" | 682 ------------------ ...32\345\222\214\347\254\224\350\256\260.md" | 290 -------- ...06\345\220\210\344\275\234\344\270\232.md" | 205 ------ ...41\347\220\206\347\263\273\347\273\237.md" | 254 ------- 9 files changed, 2165 deletions(-) delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230408 java\347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230415 JavaAPI\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index 63c6bc5..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,260 +0,0 @@ -```java -import java.util.Scanner; - -public class Class { -// **1、判断一个字符数据是否是数字字符 ** -// -// **分析:** -// -// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 -// -// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, -// 并且还要下于或等于字符9即可。 -// -// 3、判断完成之后,打印判断的结果。 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - char i = sc.next.charAt(0); - if (i>=0 && i<10){ - System.out.println("是数字字符"); - }else { - System.out.println("不是数字字符"); - } - } -} - -import java.util.Scanner; - -public class java2 { - /* **2、判断一个字符数据是否是字母字符** - - **分析:** - - 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 - - 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, - 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z - - 3、判断完成之后,打印判断的结果。*/ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - char st = sc.next().charAt(0); - - if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) - - System.out.println( "是字母"); - - else - - System.out.println("不是字母"); - - } -} - - -import java.util.Scanner; - -public class java3 { -/* - **3、判断指定的年份是否为闰年,请使用键盘录入** - - **分析:** - - 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 - - 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 - -*/ - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int i = sc.nextInt(); - if ((i % 4 == 0 && i% 100 ==0) || i % 400 != 0 ){ - System.out.println("是闰年"); - }else{ - System.out.println("不是闰年"); - } - } -} - - -import java.util.Scanner; - -public class java4 { - // **4、判断一个数字是否为水仙花数,请使用键盘录入** -// -// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, -// -// **分析:** -// -// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 -// -// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 -// -// 2、判断的过程 -// -// a) 将3位数字的每一位上的数字拆分下来 -// -// b) 计算每位数字的3次幂之和 -// -// C) 用和值 和 原来的数字进行比较 -// -// D) 打印判断的比较结果即可 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入一个数字判断是不是水仙花数"); - int i = sc.nextInt(); - int ge = i % 10; - int si = i / 10 % 10; - int bai = i / 100 % 10; - if (i > 99 && i < 1000) { - if (ge * ge * ge + si * si * si + bai * bai * bai == i) { - System.out.println("是水仙花数"); - } else { - System.out.println("不是水仙花数"); - } - }else { - System.out.println("您好,水仙花数是三位数"); - } - } -} - - -import java.util.Scanner; - -public class java5 { - /* **5、判断一个5位数字是否为回文数,使用键盘录入** - - 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 - - **分析:** - - 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 - - 2、判断的过程 - - a) 将5位数字的万、千、十、个位数拆分出来 - - b) 判断比较万位和个位 、 千位和十位是否相等 - - 3、判断完成之后,打印判断的结果。*/ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入五位数判断是不是回文数"); - int i = sc.nextInt(); - int ge = i % 10; - int si = i / 10 % 10; - int qian = i / 1000 % 10; - int wan = i / 10000 % 10; - if (i > 9999 && i < 100000) { - if (ge == wan && si == qian) { - System.out.println("是回文数"); - } else { - System.out.println("不是回文数"); - } - }else { - System.out.println("您输入的不是五位数"); - } - } -} - - -import java.util.Scanner; - -public class java6 { -// ## 题目1(训练) -// -// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: -// -// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 -// -// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 -// -// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? -// -// ### 训练提示 -// -//1. 已知的父母身高如何用代码体现? -// -// 2. 题目中的公式如何转化为代码? -// -// ### 解题方案 -// -//1. 使用变量的定义和算术运算符完成本题 -// -//### 操作步骤 -// -//1. 定义小数变量代表父亲身高 -// -//2. 定义小数变量代表母亲身高 -// -//3. 通过儿子身高计算方式计算儿子身高 -// -//4. 通过女儿身高计算方式计算女人身高 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入父亲的身高"); - double baba = sc.nextInt(); - System.out.println("请输入母亲的身高"); - double mama = sc.nextInt(); - double erzi = (baba + mama) * 1.08 / 2; - double nver = (baba * 0.923 + mama) / 2; - System.out.println("儿子的身高为:" + erzi); - System.out.println("女儿的身高为:" + nver); - } -} - -public class java7 { -// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 -// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 -// 那么红茶和绿茶现在的钱一样多,请问对么? - public static void main(String[] args) { - int hocha = 21; - int lvcha = 24; - hocha = hocha * 2 + 3; - lvcha = lvcha * 2; - if (hocha == lvcha){ - System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); - }if (hocha > lvcha){ - System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); - }if (hocha < lvcha){ - System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); - } - } -} - - -public class java8 { -// 某小伙想定一份外卖,商家的优惠方式如下: -// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, -// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? - /*训练提示 - 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 - 解题方案 - 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 - 操作步骤 - 1. 使用算术运算符求出不使用优惠时的总价 - 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 - 3. 使用算术运算符求出使用优惠价时的总价 - 4. 使用三元运算符判断最终更合算的购买方式和花费*/ - public static void main(String[] args) { - int yuxian = 24; - int youza = 8; - int mifan = 3; - int zojia = yuxian + youza + mifan; - double youhuijia = zojia * 0.8; - System.out.println("使用折扣的总价:" + youhuijia); - int yuxian2 = 16; - int youza2 = 8; - int mifan2 = 3; - int zojia2 = yuxian2 + youza2 + mifan2; - System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); - if (youhuijia < zojia2){ - System.out.println("使用这个:" + youhuijia ); - }else if (youhuijia > zojia2){ - System.out.println("使用这个:" + zojia2); - } - } -} -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" deleted file mode 100644 index d8f1fa4..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230331 java\345\237\272\347\241\200\350\257\255\346\263\225\350\256\255\347\273\203.md" +++ /dev/null @@ -1,149 +0,0 @@ -```java -package My; - -import javax.lang.model.SourceVersion; - -public class Class { - public static void main(String[] args) { - // 第一题 - //(1)先声明两个byte类型的变量b1,b2, - // 并分别赋值为10和20,求b1和b2变量的和, - // 并将结果保存在byte类型的变量b3中, - // 最后输出b3变量的值 - byte b1,b2; - b1=10; - b2=20; - byte b3=(byte)(b1+b2); - System.out.println("byte类型的b1和b2的和为:"+(b3)); - -// 第二题 -// (2)先声明两个short类型的变量s1,s2, -// 并分别赋值为1000和2000,求s1和s2变量的和, -// 并将结果保存在short类型的变量s3中, -// 最后输出s3变量的值 - short s1=1000,s2=2000; - short s3=(short)(s1+s2); - System.out.println("short类型的是s1和s2的和为:"+s3); - -// 第三题 -// (3)先声明1个char类型的变量c1赋值为'a', -// 再声明一个int类型的变量num赋值为5,求c1和num变量的和, -// 并将结果将结果保存在char类型的变量letter中, -// 最后输出letter变量的值。 - char c1='a'; - int num=5; - char letter=(char)(c1+(char)num); - System.out.println("char类型的c1和int类型的num的和:"+(letter)); - -// 第四题 -// (4)先声明两个int类型的变量i1,i2, -// 并分别赋值5和2,求i1和i2的商, -// 并将结果保存在double类型的变量result中, -// 最后输出result变量的值。如何得到结果2.5呢? - int i1=5,i2=2; - double result=(double) i1/i2; - System.out.println("i1和i2的商为:"+(result)); - } -} - - -package My; - -public class Class1 { - public static void main(String[] args) { - int a1=10,a2=11; - System.out.println("10是偶数?"+(a1%2==0)); - System.out.println("11是偶数?"+(a2%2==0)); - int a3=12,a4=13; - System.out.println("12是奇数?"+(a3%2!=0)); - System.out.println("13是奇数?"+(a4%2!=0)); - } -} - -package My; - -public class Class3 { - public static void main(String[] args) { -// 题目:为抵抗洪水,战士连续作战89小时,编程计算共多少天零多少小时? - int hours=89; - int day=hours/24; - int hour=hours%24; - System.out.println("为抵抗洪水,战士连续作战89小时:"+"89小时是"+(day)+"天"+(hour)+"小时"); - - } -} - -package My; - -public class Class4 { - public static void main(String[] args) { -// 题目:今天是周2,100天以后是周几? - int week=2; - week+=100; - week%=7; - System.out.println("今天是周二,100天以后是周"+(week)); - } -} - -package My; - -import java.util.Scanner; - -public class Class2 { - public static void main(String[] args) { -// ## 5、案例:求三个整数x,y,z中的最大值 -// -// -// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 -// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) -// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) -// 4. 输出结果 - Scanner sc=new Scanner(System.in); - System.out.print("请输入x的值:"); - int x=sc.nextInt(); - System.out.print("请输入y的值:"); - int y=sc.nextInt(); - System.out.print("请输入z的值:"); - int z=sc.nextInt(); - int max; - max=(x>y)?x:y; - max=(max>z)?max:z; - System.out.println(max); - } -} - - - -package My; - -public class Class5 { - public static void main(String[] args) { -// 题目:判断今年是否是闰年 - int year=2023; - boolean a=(year%4==0 && year%100!=0); - boolean b=(year%400==0); - if(a || b){ - System.out.println(year+"是闰年"); - }else { - System.out.println(year + "不是闰年"); - } - } -} - - -package My; - -public class Class6 { - public static void main(String[] args) { -// 题目:小明要到美国旅游,可是那里的温度是以华氏度为单位记录的。 -// 它需要一个程序将华氏温度(80度)转换为摄氏度, -// 并以华氏度和摄氏度为单位分别显示该温度。 - double hua=80; - double she; - she=(hua-32)/1.8; - System.out.println("华氏度80.0度转化为摄氏度是"+(she)+"度"); - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" deleted file mode 100644 index dc4fd7e..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" +++ /dev/null @@ -1,148 +0,0 @@ -```java -public class T1 { - public static void main(String[] args) { -// .定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** -// -//**操作步骤:** -// -// 1.定义5个元素数组 -// 2.可以使用初始化数组的两种方式之一为数组元素赋值 -// 3.遍历数组求数组中的最小值 - - - int[] sum={12,32,52,2,5};//创立数组并赋值 - - int min=sum[0]; - for (int i=1;isum[i]){ - min=sum[i]; - } - } - System.out.println("数组sum的最小值为:"+min); - } -} -``` - - - -```java -package Glass; - -import java.util.Arrays; - -public class T2 { - public static void main(String[] args) { -// 需求:求出数组中索引与索引对应的元素都是奇数的元素** -// -//**分析:** -// -// 1、遍历数组 -// 2、判断索引是否是奇数(索引 % 2 != 0) -// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) -// 4、满足条件输出结果 - - int[] sum={12,4,32,6,32,11}; - System.out.println(Arrays.toString(sum)); - - for (int i=0;i= 0; - } - -// 包含一个方法int approximateNumberCount()方法, -// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 - public int approximateNumberCount(){ - int count = 0; - for (int i = 1; i <= value; i++) { - if (value % i == 0){ - count++; - } - } - return count; - } - -// 包含一个方法boolean isPrimeNumber()方法, -// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, -// 并且value是大于1的自然数,那么value就是素数。 - public boolean isPrimeNumber(){ - int count = 0; - boolean flag = false; - for (int i = 1; i < value ; i++) { - count++; - if (value % i == 0 && value % 1 == 0 && value > 1){ - if (count >= 3){ - flag = false; - }else { - flag = true; - } - } - } - return flag; - } -// 包含一个方法int[] getAllPrimeNumber()方法, -// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 - public int[] getAllPrimeNumber(){ - int[] arr = new int[approximateNumberCount()]; - int index = arr[0]; - for (int i = 1; i <= value; i++) { - if (value % i == 0){ - arr[index++]=i; - } - } - return arr; - } - - @Override - public String toString() { - return "MyInt{" + - "\nvalue=" + value + - "\n是不是自然数:" + isNatural() + - "\n约数个数:" + approximateNumberCount() + - "\n是不是素数:" + isPrimeNumber() + - "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + - "\n}"; - } -} - -package T6; - -public class TestMyLnt { - public static void main(String[] args) { - MyLnt lnt = new MyLnt(20); - System.out.println(lnt); - } -} - -``` - -# 第七题 - -```java -package T7; - -public class TestYue { - public static void main(String[] args) { - Yue y = new Yue(); - int min = y.min(44,33,88,342,454,234,5,2); - System.out.println("最小值为:" + min); - int max = y.maxApproximate(4,8,16,20,24); - System.out.println("最大公约数为:" + max); - } -} - - -package T7; - -public class Yue { -// 返回n个整数中的最小值 - int min(int... nums){ - int min = nums[0]; - for (int i = 0; i < nums.length; i++) { - if (min > nums[i]){ - min = nums[i]; - } - } - return min; - } - - public int maxApproximate(int... nums){ - int max=min(nums); - for (int i = max; i >= 1; i--) { - boolean flag=true; - for (int j = 0; j < nums.length; j++) { - if (nums[j]%i!=0){ - flag=false; - break; - } - } - if (flag==true){ - return i; - } - } - return 1; - } -} - -``` - -# 第八题 - -```java -package T8; - -public class ArraysTools { - public String toString(int[]arr) { - String str = "["; - for (int i = 0; i < arr.length; i++) { - int i1 = arr[i]; - str += i1; - if (i < arr.length - 1) { - str += ","; - } - } - str += "]"; - return str; - } - int[] grow(int[] arr){ - int shu = arr.length; - int[] newArr = new int[2 * shu]; - for (int i = 0; i < arr.length; i++) { - newArr[i] = arr[i]; - } - return newArr; - } -} - -package T8; - -public class Test { - public static void main(String[] args) { - int[]old={11,22,33,44}; - ArraysTools arraysTools = new ArraysTools(); - System.out.println(arraysTools.toString(old)); - int[]newArr= arraysTools.grow(old); - System.out.println(arraysTools.toString(newArr)); - } -} - -``` - -# 第九题 - -```java -package T9; - -public class MathTools { - int compare(int a, int b){ - if (a > b){ - return a; - }else if (a < b){ - return -a; - }else { - return 0; - } - } -/* -* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, -* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; -* */ - int compare(double a, double b){ - if (a > b){ - return (int)a; - }else if (a < b){ - return (int)-a; - }else { - return 0; - } - } - -// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, -// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; - int compare(char a, char b){ - if (a > b){ - return (char)a; - }else if (a < b){ - return (char)-a; - }else { - return 0; - } - } -} - - -package T9; - -public class Test { - public static void main(String[] args) { - MathTools ma = new MathTools(); - int a = ma.compare(3,5); - int b = ma.compare('2','3'); - int c = ma.compare(33.5,22.4); - System.out.println(a); - System.out.println(b); - System.out.println(c); - } -} - -``` - -# 第十题 - -```java -package T10; - -public class ArraysTools { - public void sort(int[] arr){ - for (int i = 1; i < arr.length; i++) { - for (int j = 0; j < arr.length - i; j++) { - if (arr[j] > arr[j + 1]){ - int temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } - public void sort(double[] arr){ - for (int i= 1; i arr[j+1]){ - double temp=arr[j]; - arr[j]=arr[j+1]; - arr[j+1]=temp; - } - } - } - } - - public void sort(char[] arr) { - for (int i = 1; i < arr.length; i++) { - for (int j = 0; j < arr.length - i; j++) { - if (arr[j] > arr[j + 1]) { - char temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } - public String toString(int[] arr){ - String re="["; - for (int i = 0; i < arr.length; i++) { - if (i我***你 - -##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 - -String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] - -## String方法小结 - -| **方法名** | **说明** | -| ----------------------------------------------------- | -------------------------------- | -| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | -| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | -| public int length() | 返回此字符串的长度 | - -| public char charAt(int index) | 返回指定索引处的 char 值 | -| ----------------------------- | ---------------------------- | -| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | - -| **方法名** | **说明** | -| ------------------------------------------------------------ | ------------------------------------------------------------ | -| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | -| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | -| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | - -| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | -| ----------------------------------- | ---------------------------------------- | -| | | - -## StringBuilder - -StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 - -作用:提高字符串的操作效率 - -### StringBuilder 构造方法 - -| **方法名** | **说明** | -| -------------------------------- | ------------------------------------------ | -| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | -| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | - -### StringBuilder 的常用方法 - -| **方法名** | **说明** | -| -------------------------------------- | ------------------------ | -| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | -| public StringBuilder reverse() | 返回相反的字符序列 | - -| public int length() | 返回长度 ( 字符出现的个数) | -| ------------------- | -------------------------- | -| | | - -| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | -| ------------------------ | --------------------------------------------------- | -| | | - -## StringBuilder和String的区别 - -**String** :内容是不可变的 - -**StringBuilder**:内容是可变的 - -## StringBuilder 和 String 相互转化 - -### StringBuilder 转换为 String - -public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String - -### String 转换为 StringBuilder - -public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder - -# 作业 - -```java -import java.util.Scanner; - -public class Test1 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - String password = "12345"; - System.out.println("请输入密码登陆"); - int count = 0; - for (int i = 0; i < 3; i++) { - String st = sc.next(); - count++; - if (st.equals(password)){ - System.out.println("密码正确,登陆成功"); - break; - }else { - System.out.println("密码错误"); - System.out.println("您还有" + (3 - count) + "次"); - } - } - } -} - - -import java.util.Scanner; - -public class Test2 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入字符串遍历数组"); - String st = sc.next(); - for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ - da++; - } - if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ - xiao++; - } - if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ - shu++; - } - } - System.out.println("大写出现的次数:" + da); - System.out.println("小写出现的次数:" + xiao); - System.out.println("数字出现的次数:" + shu); - } -} - - -import javax.swing.*; -import java.util.Scanner; - -public class Test4 { - public static void main(String[] args) { - System.out.println("请输入电话号码"); - Scanner sc = new Scanner(System.in); - String st = sc.next(); - String s1 = st.substring(0,3); - String s2 = st.substring(7); - System.out.println("转换后的电话号码为:" + s1 + "****" + s2); - } -} - - -import java.util.Scanner; - -public class Test5 { - public static void main(String[] args) { - System.out.println("请输入脏话"); - Scanner sc = new Scanner(System.in); - String st = sc.next(); - String s1 = st.replace("TMD","不可以说脏话"); - System.out.println(s1); - } -} - - -public class Student { - private String name; - private int age; - - public Student(String name,int age){ - this.name = name; - this.age = age; - } - - public Student(){ - - } - - public void setName(String name){ - this.name = name; - } - - public void setAge(int age){ - this.age = age; - } - - public String getName(){ - return this.name = name; - } - - public int getAge(){ - return this.age = age; - } -} - - -import java.util.Arrays; -import java.util.Scanner; - -public class TestStudent6 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - Student st = new Student(); - System.out.println("请输入名字"); - st.setName(sc.next()); - System.out.println("请输入年龄"); - st.setAge(sc.nextInt()); - String st2 = st.getName() +","+ st.getAge(); - String[] arr = st2.split(","); - System.out.println(Arrays.toString(arr)); - - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" deleted file mode 100644 index db5bfa5..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" +++ /dev/null @@ -1,205 +0,0 @@ -```java -//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 -public class Student { - private String name; - private int age; - - public Student(String name, int age) { - this.name = name; - this.age = age; - } - - public Student(String name) { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - -} - -import java.util.ArrayList; - -public class TestStudent { - public static void main(String[] args) { - ArrayList list = new ArrayList<>(); - list.add(new Student("张三",18)); - list.add(new Student("李四",19)); - list.add(new Student("王五",20)); - for (int i = 0; i < list.size(); i++) { - Student stu = list.get(i); - System.out.print(stu.getName() + " "); - System.out.println(stu.getAge()); - } - } -} - -``` - -```java -//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 -//学生的姓名和年龄来自于键盘录入 -public class Student { - private String name; - private int age; - - public Student(String name, int age) { - this.name = name; - this.age = age; - } - - public Student(String name) { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - -} -import java.security.AllPermission; -import java.util.ArrayList; -import java.util.Scanner; - -public class TestStudent2 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int count = 0; - ArrayList list = new ArrayList<>(); - while (true) { - count++; - System.out.println("请输入第" + count + "名学生的姓名和年龄"); - list.add(new Student(sc.next(), sc.nextInt())); - if (count == 3){ - break; - } - } - for (int i = 0; i < list.size(); i++) { - Student stu = list.get(i); - System.out.print(stu.getName() + " "); - System.out.println(stu.getAge()); - } - } -} - -``` - -```java - -import java.util.ArrayList; -import java.util.Iterator; - -public class T2 { - public static void main(String[] args) { -// 需求:创建一个存储String的集合,内部存储 -// (test,张三,李四,test,test)字符串 -// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 - ArrayList list = new ArrayList<>(); - list.add("test"); - list.add("张三"); - list.add("李四"); - list.add("test"); - list.add("test"); -// Iterator it = list.iterator(); -// while(it.hasNext()){ -// String str = it.next(); -// if (str.contains("test")){ -// it.remove(); -// } -// } - list.removeIf(s -> s.contains("test")); - System.out.println(list); - } -} - - -``` - -```java -//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 -//并存入新集合对象,方法返回新集合。 -public class Student { - private String name; - private int age; - public Student(String name,int age){ - this.name = name; - this.age = age; - } - - public Student(){ - - } - - public void setName(String name){ - this.name = name; - } - public void setAge(int age){ - this.age = age; - } - - public String getName(){ - return this.name = name; - } - - public int getAge(){ - return this.age = age; - } - - - public String toString(){ - return "姓名:" + getName() + " " + "年龄:" + getAge(); - } -} - -import java.util.ArrayList; - -public class TestStudent { - public static void main(String[] args) { - ArrayList list = new ArrayList<>(); - list.add(new Student("张三", 19)); - list.add(new Student("李四", 20)); - list.add(new Student("王五", 10)); - list.add(new Student("赵六", 15)); - chaXun(new ArrayList<>(list)); - } - public static ArrayList chaXun(ArrayList list - ){ - ArrayList list1 = new ArrayList<>(); - for (int i = 0; i < list.size(); i++) { - Student st = list.get(i); - if (st.getAge()<18){ - list1.add(list.get(i)); - } - } - System.out.println(list1); - return list1; - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" deleted file mode 100644 index bc280be..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" +++ /dev/null @@ -1,254 +0,0 @@ -```java -package Light; - -public class Student { - private String StuID; - private String name; - private int age; - private String sex; - - - - public String getStuID() { - return StuID; - } - - public void setStuID(String stuID) { - StuID = stuID; - } - - public String getSex() { - return sex; - } - - public void setSex(String sex) { - this.sex = sex; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Student(){} - public Student(String StuID,String name,int age,String sex){ - this.StuID=StuID; - this.name=name; - this.age=age; - this.sex=sex; - } - - @Override - public String toString() { - return "Student{" + - "StuID='" + StuID + '\'' + - ", name='" + name + '\'' + - ", age=" + age + - ", sex='" + sex + '\'' + - '}'; - } -} -``` - -```java -package Light; - -import java.util.ArrayList; -import java.util.Scanner; - -public class T { - public static void main(String[] args) { - ArrayList list=new ArrayList<>(); - Scanner sc=new Scanner(System.in); - - sum: - for (int i=0;;){ - System.out.println("============学生系统============"); - System.out.println("请输入编号:"+ - "\n\t1 添加学生信息"+ - "\n\t2 修改学生信息"+ - "\n\t3 删除学生信息"+ - "\n\t4 查看学生信息"+ - "\n\t5 退出学生系统"); - System.out.println(); - System.out.print("请输入编号:"); - String sum=sc.next();; - switch (sum){ - case "1": -// System.out.println("添加"); - insertStudent(list,sc); - break; - case"2": - updateStudent(list,sc); - break; - case"3": - deleteStudent(list,sc); - break; - case"4": - lookStudent(list,sc); - break; - case"5": - System.out.println("成功退出学生系统"); - break sum; - default: - System.out.println("输入编号错误,请重新输入!!!"); - System.out.println(); - } - } - } - - - /** - * 添加 - * @param list - * @param sc - */ - public static void insertStudent(ArrayList list, Scanner sc){ - System.out.println(); - System.out.println("===*请输入需要添加学生信息*==="); - System.out.println("请输入学号"); - String stuId=sc.next(); - - if (!list.isEmpty()){ - for (int i = 0; i< list.size(); i++){ - Student stu= list.get(i); - if (stuId.equals(stu.getStuID())){ - System.out.println("学号已经存在"); - break; - }else { - System.out.println("请输入姓名"); - String stuName=sc.next(); - System.out.println("请输入年龄"); - int stuAge=sc.nextInt(); - System.out.println("请输入性别"); - String stuSex=sc.next(); - list.add(new Student(stuId,stuName,stuAge,stuSex)); - } - } - }else { - System.out.println("请输入姓名"); - String stuName=sc.next(); - System.out.println("请输入年龄"); - int stuAge=sc.nextInt(); - System.out.println("请输入性别"); - String stuSex=sc.next(); - list.add(new Student(stuId,stuName,stuAge,stuSex)); - } - System.out.println(); - } - - - /** - * 修改 - * @param list - * @param sc - */ - - public static void updateStudent(ArrayList list,Scanner sc){ - System.out.println(); - if (list.isEmpty()){ - System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); - }else { - System.out.println("===修改学生信息==="); - System.out.println("请输入需要修改的学生学号"); - String stuId=sc.next(); - for (int i=0;i list,Scanner sc){ - System.out.println(); - if (list.isEmpty()){ - System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); - }else { - System.out.println("==*删除学生信息*=="); - System.out.println("请输入需要删除的学生学号"); - String stuId=sc.next(); - - for (int i=0;i list,Scanner sc){ - System.out.println(); - - if (list.isEmpty()) { - System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); - }else { - System.out.println("==*查看学生信息*=="); - System.out.println("请输入需要查看学生的学生学号"); - String stuId=sc.next(); - - for (int i=0;i Date: Mon, 8 May 2023 20:39:08 +0800 Subject: [PATCH 14/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...60\347\273\204\344\275\234\344\270\232.md" | 148 ++++ ...71\350\261\241\344\275\234\344\270\232.md" | 47 ++ ...32\345\222\214\347\254\224\350\256\260.md" | 130 ++++ .../20230414 java\344\275\234\344\270\232.md" | 682 ++++++++++++++++++ ...32\345\222\214\347\254\224\350\256\260.md" | 290 ++++++++ ...06\345\220\210\344\275\234\344\270\232.md" | 205 ++++++ ...41\347\220\206\347\263\273\347\273\237.md" | 254 +++++++ 7 files changed, 1756 insertions(+) create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230408 java\347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230415 JavaAPI\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000..dc4fd7e --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" @@ -0,0 +1,148 @@ +```java +public class T1 { + public static void main(String[] args) { +// .定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** +// +//**操作步骤:** +// +// 1.定义5个元素数组 +// 2.可以使用初始化数组的两种方式之一为数组元素赋值 +// 3.遍历数组求数组中的最小值 + + + int[] sum={12,32,52,2,5};//创立数组并赋值 + + int min=sum[0]; + for (int i=1;isum[i]){ + min=sum[i]; + } + } + System.out.println("数组sum的最小值为:"+min); + } +} +``` + + + +```java +package Glass; + +import java.util.Arrays; + +public class T2 { + public static void main(String[] args) { +// 需求:求出数组中索引与索引对应的元素都是奇数的元素** +// +//**分析:** +// +// 1、遍历数组 +// 2、判断索引是否是奇数(索引 % 2 != 0) +// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) +// 4、满足条件输出结果 + + int[] sum={12,4,32,6,32,11}; + System.out.println(Arrays.toString(sum)); + + for (int i=0;i= 0; + } + +// 包含一个方法int approximateNumberCount()方法, +// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 + public int approximateNumberCount(){ + int count = 0; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + count++; + } + } + return count; + } + +// 包含一个方法boolean isPrimeNumber()方法, +// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, +// 并且value是大于1的自然数,那么value就是素数。 + public boolean isPrimeNumber(){ + int count = 0; + boolean flag = false; + for (int i = 1; i < value ; i++) { + count++; + if (value % i == 0 && value % 1 == 0 && value > 1){ + if (count >= 3){ + flag = false; + }else { + flag = true; + } + } + } + return flag; + } +// 包含一个方法int[] getAllPrimeNumber()方法, +// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 + public int[] getAllPrimeNumber(){ + int[] arr = new int[approximateNumberCount()]; + int index = arr[0]; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + arr[index++]=i; + } + } + return arr; + } + + @Override + public String toString() { + return "MyInt{" + + "\nvalue=" + value + + "\n是不是自然数:" + isNatural() + + "\n约数个数:" + approximateNumberCount() + + "\n是不是素数:" + isPrimeNumber() + + "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + + "\n}"; + } +} + +package T6; + +public class TestMyLnt { + public static void main(String[] args) { + MyLnt lnt = new MyLnt(20); + System.out.println(lnt); + } +} + +``` + +# 第七题 + +```java +package T7; + +public class TestYue { + public static void main(String[] args) { + Yue y = new Yue(); + int min = y.min(44,33,88,342,454,234,5,2); + System.out.println("最小值为:" + min); + int max = y.maxApproximate(4,8,16,20,24); + System.out.println("最大公约数为:" + max); + } +} + + +package T7; + +public class Yue { +// 返回n个整数中的最小值 + int min(int... nums){ + int min = nums[0]; + for (int i = 0; i < nums.length; i++) { + if (min > nums[i]){ + min = nums[i]; + } + } + return min; + } + + public int maxApproximate(int... nums){ + int max=min(nums); + for (int i = max; i >= 1; i--) { + boolean flag=true; + for (int j = 0; j < nums.length; j++) { + if (nums[j]%i!=0){ + flag=false; + break; + } + } + if (flag==true){ + return i; + } + } + return 1; + } +} + +``` + +# 第八题 + +```java +package T8; + +public class ArraysTools { + public String toString(int[]arr) { + String str = "["; + for (int i = 0; i < arr.length; i++) { + int i1 = arr[i]; + str += i1; + if (i < arr.length - 1) { + str += ","; + } + } + str += "]"; + return str; + } + int[] grow(int[] arr){ + int shu = arr.length; + int[] newArr = new int[2 * shu]; + for (int i = 0; i < arr.length; i++) { + newArr[i] = arr[i]; + } + return newArr; + } +} + +package T8; + +public class Test { + public static void main(String[] args) { + int[]old={11,22,33,44}; + ArraysTools arraysTools = new ArraysTools(); + System.out.println(arraysTools.toString(old)); + int[]newArr= arraysTools.grow(old); + System.out.println(arraysTools.toString(newArr)); + } +} + +``` + +# 第九题 + +```java +package T9; + +public class MathTools { + int compare(int a, int b){ + if (a > b){ + return a; + }else if (a < b){ + return -a; + }else { + return 0; + } + } +/* +* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, +* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; +* */ + int compare(double a, double b){ + if (a > b){ + return (int)a; + }else if (a < b){ + return (int)-a; + }else { + return 0; + } + } + +// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, +// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; + int compare(char a, char b){ + if (a > b){ + return (char)a; + }else if (a < b){ + return (char)-a; + }else { + return 0; + } + } +} + + +package T9; + +public class Test { + public static void main(String[] args) { + MathTools ma = new MathTools(); + int a = ma.compare(3,5); + int b = ma.compare('2','3'); + int c = ma.compare(33.5,22.4); + System.out.println(a); + System.out.println(b); + System.out.println(c); + } +} + +``` + +# 第十题 + +```java +package T10; + +public class ArraysTools { + public void sort(int[] arr){ + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]){ + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public void sort(double[] arr){ + for (int i= 1; i arr[j+1]){ + double temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } + } + } + + public void sort(char[] arr) { + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]) { + char temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public String toString(int[] arr){ + String re="["; + for (int i = 0; i < arr.length; i++) { + if (i我***你 + +##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 + +String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] + +## String方法小结 + +| **方法名** | **说明** | +| ----------------------------------------------------- | -------------------------------- | +| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | +| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | +| public int length() | 返回此字符串的长度 | + +| public char charAt(int index) | 返回指定索引处的 char 值 | +| ----------------------------- | ---------------------------- | +| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | + +| **方法名** | **说明** | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | +| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | +| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | + +| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | +| ----------------------------------- | ---------------------------------------- | +| | | + +## StringBuilder + +StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 + +作用:提高字符串的操作效率 + +### StringBuilder 构造方法 + +| **方法名** | **说明** | +| -------------------------------- | ------------------------------------------ | +| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | +| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | + +### StringBuilder 的常用方法 + +| **方法名** | **说明** | +| -------------------------------------- | ------------------------ | +| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | +| public StringBuilder reverse() | 返回相反的字符序列 | + +| public int length() | 返回长度 ( 字符出现的个数) | +| ------------------- | -------------------------- | +| | | + +| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | +| ------------------------ | --------------------------------------------------- | +| | | + +## StringBuilder和String的区别 + +**String** :内容是不可变的 + +**StringBuilder**:内容是可变的 + +## StringBuilder 和 String 相互转化 + +### StringBuilder 转换为 String + +public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String + +### String 转换为 StringBuilder + +public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder + +# 作业 + +```java +import java.util.Scanner; + +public class Test1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String password = "12345"; + System.out.println("请输入密码登陆"); + int count = 0; + for (int i = 0; i < 3; i++) { + String st = sc.next(); + count++; + if (st.equals(password)){ + System.out.println("密码正确,登陆成功"); + break; + }else { + System.out.println("密码错误"); + System.out.println("您还有" + (3 - count) + "次"); + } + } + } +} + + +import java.util.Scanner; + +public class Test2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入字符串遍历数组"); + String st = sc.next(); + for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ + da++; + } + if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ + xiao++; + } + if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ + shu++; + } + } + System.out.println("大写出现的次数:" + da); + System.out.println("小写出现的次数:" + xiao); + System.out.println("数字出现的次数:" + shu); + } +} + + +import javax.swing.*; +import java.util.Scanner; + +public class Test4 { + public static void main(String[] args) { + System.out.println("请输入电话号码"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.substring(0,3); + String s2 = st.substring(7); + System.out.println("转换后的电话号码为:" + s1 + "****" + s2); + } +} + + +import java.util.Scanner; + +public class Test5 { + public static void main(String[] args) { + System.out.println("请输入脏话"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.replace("TMD","不可以说脏话"); + System.out.println(s1); + } +} + + +public class Student { + private String name; + private int age; + + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } +} + + +import java.util.Arrays; +import java.util.Scanner; + +public class TestStudent6 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Student st = new Student(); + System.out.println("请输入名字"); + st.setName(sc.next()); + System.out.println("请输入年龄"); + st.setAge(sc.nextInt()); + String st2 = st.getName() +","+ st.getAge(); + String[] arr = st2.split(","); + System.out.println(Arrays.toString(arr)); + + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" new file mode 100644 index 0000000..db5bfa5 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" @@ -0,0 +1,205 @@ +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三",18)); + list.add(new Student("李四",19)); + list.add(new Student("王五",20)); + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +//学生的姓名和年龄来自于键盘录入 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} +import java.security.AllPermission; +import java.util.ArrayList; +import java.util.Scanner; + +public class TestStudent2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int count = 0; + ArrayList list = new ArrayList<>(); + while (true) { + count++; + System.out.println("请输入第" + count + "名学生的姓名和年龄"); + list.add(new Student(sc.next(), sc.nextInt())); + if (count == 3){ + break; + } + } + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java + +import java.util.ArrayList; +import java.util.Iterator; + +public class T2 { + public static void main(String[] args) { +// 需求:创建一个存储String的集合,内部存储 +// (test,张三,李四,test,test)字符串 +// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 + ArrayList list = new ArrayList<>(); + list.add("test"); + list.add("张三"); + list.add("李四"); + list.add("test"); + list.add("test"); +// Iterator it = list.iterator(); +// while(it.hasNext()){ +// String str = it.next(); +// if (str.contains("test")){ +// it.remove(); +// } +// } + list.removeIf(s -> s.contains("test")); + System.out.println(list); + } +} + + +``` + +```java +//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 +//并存入新集合对象,方法返回新集合。 +public class Student { + private String name; + private int age; + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } + + + public String toString(){ + return "姓名:" + getName() + " " + "年龄:" + getAge(); + } +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三", 19)); + list.add(new Student("李四", 20)); + list.add(new Student("王五", 10)); + list.add(new Student("赵六", 15)); + chaXun(new ArrayList<>(list)); + } + public static ArrayList chaXun(ArrayList list + ){ + ArrayList list1 = new ArrayList<>(); + for (int i = 0; i < list.size(); i++) { + Student st = list.get(i); + if (st.getAge()<18){ + list1.add(list.get(i)); + } + } + System.out.println(list1); + return list1; + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000..bc280be --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,254 @@ +```java +package Light; + +public class Student { + private String StuID; + private String name; + private int age; + private String sex; + + + + public String getStuID() { + return StuID; + } + + public void setStuID(String stuID) { + StuID = stuID; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Student(){} + public Student(String StuID,String name,int age,String sex){ + this.StuID=StuID; + this.name=name; + this.age=age; + this.sex=sex; + } + + @Override + public String toString() { + return "Student{" + + "StuID='" + StuID + '\'' + + ", name='" + name + '\'' + + ", age=" + age + + ", sex='" + sex + '\'' + + '}'; + } +} +``` + +```java +package Light; + +import java.util.ArrayList; +import java.util.Scanner; + +public class T { + public static void main(String[] args) { + ArrayList list=new ArrayList<>(); + Scanner sc=new Scanner(System.in); + + sum: + for (int i=0;;){ + System.out.println("============学生系统============"); + System.out.println("请输入编号:"+ + "\n\t1 添加学生信息"+ + "\n\t2 修改学生信息"+ + "\n\t3 删除学生信息"+ + "\n\t4 查看学生信息"+ + "\n\t5 退出学生系统"); + System.out.println(); + System.out.print("请输入编号:"); + String sum=sc.next();; + switch (sum){ + case "1": +// System.out.println("添加"); + insertStudent(list,sc); + break; + case"2": + updateStudent(list,sc); + break; + case"3": + deleteStudent(list,sc); + break; + case"4": + lookStudent(list,sc); + break; + case"5": + System.out.println("成功退出学生系统"); + break sum; + default: + System.out.println("输入编号错误,请重新输入!!!"); + System.out.println(); + } + } + } + + + /** + * 添加 + * @param list + * @param sc + */ + public static void insertStudent(ArrayList list, Scanner sc){ + System.out.println(); + System.out.println("===*请输入需要添加学生信息*==="); + System.out.println("请输入学号"); + String stuId=sc.next(); + + if (!list.isEmpty()){ + for (int i = 0; i< list.size(); i++){ + Student stu= list.get(i); + if (stuId.equals(stu.getStuID())){ + System.out.println("学号已经存在"); + break; + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + } + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + System.out.println(); + } + + + /** + * 修改 + * @param list + * @param sc + */ + + public static void updateStudent(ArrayList list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); + }else { + System.out.println("===修改学生信息==="); + System.out.println("请输入需要修改的学生学号"); + String stuId=sc.next(); + for (int i=0;i list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); + }else { + System.out.println("==*删除学生信息*=="); + System.out.println("请输入需要删除的学生学号"); + String stuId=sc.next(); + + for (int i=0;i list,Scanner sc){ + System.out.println(); + + if (list.isEmpty()) { + System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); + }else { + System.out.println("==*查看学生信息*=="); + System.out.println("请输入需要查看学生的学生学号"); + String stuId=sc.next(); + + for (int i=0;i Date: Mon, 8 May 2023 20:48:02 +0800 Subject: [PATCH 15/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0=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" | 260 +++++++++ ...51\345\261\225\350\256\255\347\273\203.md" | 252 +++++++++ ...47\346\211\277\344\275\234\344\270\232.md" | 517 ++++++++++++++++++ .../20230425 Java\344\275\234\344\270\232.md" | 26 + ...45\345\217\243\344\275\234\344\270\232.md" | 194 +++++++ ...5\345\217\243\351\242\230\347\233\2562.md" | 165 ++++++ 6 files changed, 1414 insertions(+) create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230422 Java\347\273\247\346\211\277\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230425 Java\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230506 \346\216\245\345\217\243\351\242\230\347\233\2562.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..67a659d --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,260 @@ +```java +import java.util.Scanner; + +public class java1 { +// **1、判断一个字符数据是否是数字字符 ** +// +// **分析:** +// +// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 +// +// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, +// 并且还要下于或等于字符9即可。 +// +// 3、判断完成之后,打印判断的结果。 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int i = sc.nextInt(); + if (i>=0 && i<10){ + System.out.println("是数字字符"); + }else { + System.out.println("不是数字字符"); + } + } +} + +import java.util.Scanner; + +public class java2 { + /* **2、判断一个字符数据是否是字母字符** + + **分析:** + + 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 + + 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, + 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char st = sc.next().charAt(0); + + if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) + + System.out.println( "是字母"); + + else + + System.out.println("不是字母"); + + } +} + + +import java.util.Scanner; + +public class java3 { +/* + **3、判断指定的年份是否为闰年,请使用键盘录入** + + **分析:** + + 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 + + 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 + +*/ + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int i = sc.nextInt(); + if ((i / 4 == 0 && i / 400 ==0) || i / 100 != 0 ){ + System.out.println("是闰年"); + }else{ + System.out.println("不是闰年"); + } + } +} + + +import java.util.Scanner; + +public class java4 { + // **4、判断一个数字是否为水仙花数,请使用键盘录入** +// +// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, +// +// **分析:** +// +// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 +// +// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 +// +// 2、判断的过程 +// +// a) 将3位数字的每一位上的数字拆分下来 +// +// b) 计算每位数字的3次幂之和 +// +// C) 用和值 和 原来的数字进行比较 +// +// D) 打印判断的比较结果即可 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个数字判断是不是水仙花数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int bai = i / 100 % 10; + if (i > 99 && i < 1000) { + if (ge * ge * ge + si * si * si + bai * bai * bai == i) { + System.out.println("是水仙花数"); + } else { + System.out.println("不是水仙花数"); + } + }else { + System.out.println("您好,水仙花数是三位数"); + } + } +} + + +import java.util.Scanner; + +public class java5 { + /* **5、判断一个5位数字是否为回文数,使用键盘录入** + + 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 + + **分析:** + + 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 + + 2、判断的过程 + + a) 将5位数字的万、千、十、个位数拆分出来 + + b) 判断比较万位和个位 、 千位和十位是否相等 + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入五位数判断是不是回文数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int qian = i / 1000 % 10; + int wan = i / 10000 % 10; + if (i > 9999 && i < 100000) { + if (ge == wan && si == qian) { + System.out.println("是回文数"); + } else { + System.out.println("不是回文数"); + } + }else { + System.out.println("您输入的不是五位数"); + } + } +} + + +import java.util.Scanner; + +public class java6 { +// ## 题目1(训练) +// +// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: +// +// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 +// +// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 +// +// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? +// +// ### 训练提示 +// +//1. 已知的父母身高如何用代码体现? +// +// 2. 题目中的公式如何转化为代码? +// +// ### 解题方案 +// +//1. 使用变量的定义和算术运算符完成本题 +// +//### 操作步骤 +// +//1. 定义小数变量代表父亲身高 +// +//2. 定义小数变量代表母亲身高 +// +//3. 通过儿子身高计算方式计算儿子身高 +// +//4. 通过女儿身高计算方式计算女人身高 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入父亲的身高"); + double baba = sc.nextInt(); + System.out.println("请输入母亲的身高"); + double mama = sc.nextInt(); + double erzi = (baba + mama) * 1.08 / 2; + double nver = (baba * 0.923 + mama) / 2; + System.out.println("儿子的身高为:" + erzi); + System.out.println("女儿的身高为:" + nver); + } +} + +public class java7 { +// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 +// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 +// 那么红茶和绿茶现在的钱一样多,请问对么? + public static void main(String[] args) { + int hocha = 21; + int lvcha = 24; + hocha = hocha * 2 + 3; + lvcha = lvcha * 2; + if (hocha == lvcha){ + System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); + }if (hocha > lvcha){ + System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + }if (hocha < lvcha){ + System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + } + } +} + + +public class java8 { +// 某小伙想定一份外卖,商家的优惠方式如下: +// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, +// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? + /*训练提示 + 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 + 解题方案 + 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 + 操作步骤 + 1. 使用算术运算符求出不使用优惠时的总价 + 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 + 3. 使用算术运算符求出使用优惠价时的总价 + 4. 使用三元运算符判断最终更合算的购买方式和花费*/ + public static void main(String[] args) { + int yuxian = 24; + int youza = 8; + int mifan = 3; + int zojia = yuxian + youza + mifan; + double youhuijia = zojia * 0.8; + System.out.println("使用折扣的总价:" + youhuijia); + int yuxian2 = 16; + int youza2 = 8; + int mifan2 = 3; + int zojia2 = yuxian2 + youza2 + mifan2; + System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); + if (youhuijia < zojia2){ + System.out.println("使用这个:" + youhuijia ); + }else if (youhuijia > zojia2){ + System.out.println("使用这个:" + zojia2); + } + } +} +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" new file mode 100644 index 0000000..81070fc --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" @@ -0,0 +1,252 @@ +~~~java +package test1; + +public class test1 { +// ## 1、强制类型转换练习 +// +//(1)先声明两个byte类型的变量b1,b2,并分别赋值为10和20,求b1和b2变量的和,并将结果保存在byte类型的变量b3中, +// 最后输出b3变量的值 +// +//(2)先声明两个short类型的变量s1,s2,并分别赋值为1000和2000,求s1和s2变量的和,并将结果保存在short类型的变量s3中, +// 最后输出s3变量的值 +// +//(3)先声明1个char类型的变量c1赋值为'a',再声明一个int类型的变量num赋值为5,求c1和num变量的和, +// 并将结果将结果保存在char类型的变量letter中,最后输出letter变量的值。 +// +// (4)先声明两个int类型的变量i1,i2,并分别赋值5和2,求i1和i2的商,并将结果保存在double类型的变量result中, +// 最后输出result变量的值。如何得到结果2.5呢? + public static void main(String[] args) { + byte b1 = 10; + byte b2 = 20; + byte b3 = (byte)(b1 + b2); + System.out.println("他们的和为:" + b3); + + short s1 = 1000; + short s2 = 2000; + short s3 = (short)(s1 + s2); + System.out.println("他们的和为:" + s3); + + char c1 = 'a'; + int num = 5; + char letter = (char)(c1 + 5); + System.out.println("他们的和为:" + letter); + + int i1 = 5; + int i2 = 2; + double result = ((double) i1 / i2) ; + System.out.println("他们的商为:" + result); + } +} + + +package test1; + +public class test2 { +// 1. 定义两个int类型变量a1和a2,分别赋值10,11,判断变量是否为偶数,拼接输出结果 +// 2. 定义两个int类型变量a3和a4,分别赋值12,13,判断变量是否为奇数,拼接输出结果 + public static void main(String[] args) { + int a1 = 10; + int a2 = 11; + if (a1 % 2 ==0){ + System.out.println("10是偶数?" + true); + } + if (a2 % 2 == 0){ + System.out.println("11是偶数?" + true); + }else + System.out.println("11是偶数?" + false); + + int a3 = 12; + int a4 = 13; + if (a3 % 2 ==0){ + System.out.println("12是偶数?" + true); + } + if (a4 % 2 == 0){ + System.out.println("13是偶数?" + true); + }else + System.out.println("13是偶数?" + false); + } +} + + +package test1; + +public class test3 { +// 1. 定义一个int类型变量hours,赋值为89 +//2. 定义一个int类型变量day,用来保存89小时中天数的结果 +//3. 定义一个int类型变量hour,用来保存89小时中不够一天的剩余小时数的结果 +//4. 输出结果 + + + public static void main(String[] args) { + int hours = 89; + int day = 89 / 24; + int hour = 89 % 24; + System.out.println("为抵抗洪水,战士连续作战89小时:"); + System.out.println("89天是" + day + '天' + hour + "小时"); + } +} + + +package test1; + +public class test4 { +// 1. 定义一个int类型变量week,赋值为2 +//2. 修改week的值,在原值基础上加上100 +//3. 修改week的值,在原值基础上模以7 +//4. 输出结果,在输出结果的时候考虑特殊值,例如周日 + public static void main(String[] args) { + int week = 2; + System.out.print("今天是周"+week+','); + week = week + 100 ; + System.out.print(100+"天以后是"); + week = week % 7; + System.out.print("周"+week); + + } +} + + +package test1; + +public class test5 { +// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 +// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) +// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) +// 4. 输出结果 + public static void main(String[] args) { + int x = (int)(Math.random()*(10000-0)); + int y = (int)(Math.random()*(10000-0)); + int z = (int)(Math.random()*(10000-0)); + int max; + max = x > y ? x : y; + int maxx = max > z ? max : z ; + System.out.println((x)+"," + (y)+"," + (z)+"," + "最大值为:"+ max); + } +} + + +package test1; + +import java.util.Scanner; + +public class test6 { +// 闰年的判断标准是: +// +// 1)可以被4整除,但不可被100整除 +// +// 2)可以被400整除 +// +// +// 1. 定义一个int类型变量year,赋值为今年年份值 +// 2. 定一个一个boolean类型变量,用来保存这个年份是否是闰年的结果 +// 3. 输出结果 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入年份判断是不是闰年"); + int year = sc.nextInt(); + if ((year % 4 == 0 && year % 100 !=0) || year % 400 ==0 ){ + System.out.println(year + "是闰年"); + }else { + System.out.println(year + "不是闰年"); + } + } +} + + +package test1; + +public class test7 { +// 1. 定义一个double类型变量hua,存储华氏温度80 +//2. 定义一个double类型变量she,存储摄氏温度,根据公式求值 +//3. 输出结果 + public static void main(String[] args) { + double hua = 80; + double she = (hua - 32) / 1.8; + System.out.println("华氏度80.0°F转化摄氏度是" + she); + } +} + + +# 拔高题 + +## 第一题 + +```java +如下代码的计算结果是: +int i = 1; +i *= 0.2; +i++; +System.out.println("i=" + i); + +i=1 +``` + +## 第二题 + +```java +如下代码的运算结果是: +int i = 2; +i *= i++; + +int j = 2; +j *= j+1; + +int k = 2; +k *= ++k; + +System.out.println("i=" + i); +System.out.println("j=" + j); +System.out.println("k=" + k); + + +i=4 +j=6 +k=6 +``` + +## 第三题 + +```java +如下代码的运算结果是: +int a = 3; +int b = 1; +if(a = b){ + System.out.println("Equal"); +}else{ + System.out.println("Not Equal"); +} + +Not Equal +``` + +## 第四题 + +```java +如下代码的运算结果是: +int a = 8, b = 3; +System.out.println(a>>>b); +System.out.println(a>>>b | 2); + + +1 +3 +``` + +## 第五题 + +如何用最有效的的方法计算2乘以8 + +```java +package test1; + +public class test8 { + public static void main(String[] args) { + int num = 2 * 8; + System.out.println(num); + } +} +``` + + +~~~ + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230422 Java\347\273\247\346\211\277\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230422 Java\347\273\247\346\211\277\344\275\234\344\270\232.md" new file mode 100644 index 0000000..5cb0542 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230422 Java\347\273\247\346\211\277\344\275\234\344\270\232.md" @@ -0,0 +1,517 @@ +```java +//我们计划为一个电器销售公司制作一套系统,公司的主要业务是销售一些家用电器,例如:电冰箱、洗衣机、电视机产品。 +//父类 +package T1; + +public class Electrica { + private String brand; + private String model; + private String colour; + private double price; + + public Electrica(String brand,String model,String colour,double price){ + this.brand = brand; + this.model = model; + this.colour = colour; + this.price = price; + } + public Electrica(){ + + } + + public void setBrand(String brand){ + this.brand = brand; + } + public void setModel(String model){ + this.model = model ; + } + public void setColour(String colour){ + this.colour = colour; + } + public void setPrice(double price){ + this.price = price; + } + + public String getBrand(){ + return this.brand = brand; + } + public String getModel(){ + return this.model = model; + } + public String getColour(){ + return this.colour = colour; + } + public double getPrice(){ + return this.price = price; + } +} + + +//子类 冰箱 +package T1; + +public class Refrigerator extends Electrica { + private String doorStyle; + private String RefrigerationMethod; + public Refrigerator(){ + + } + + public void setDoorStyle(String doorStyle){ + this.doorStyle = doorStyle; + } + public void setRefrigerationMethod(String RefrigerationMethod){ + this.RefrigerationMethod = RefrigerationMethod; + } + + public String getDoorStyle(){ + return this.doorStyle = doorStyle; + } + public String getRefrigerationMethod(){ + return this.RefrigerationMethod = RefrigerationMethod; + } + + public Refrigerator(String doorStyle,String RefrigerationMethod){ + this.doorStyle = doorStyle; + this.RefrigerationMethod = RefrigerationMethod; + } + + public String toString(){ + return "冰箱的品牌为:" + super.getBrand() + "\n型号为:" + super.getModel() + + "\n颜色为:" + super.getColour() + "\n售价为:" + super.getPrice() + + "\n门款式为:" + getDoorStyle() + "\n制冷方式为:" + getRefrigerationMethod(); + } +} + + +//子类 洗衣机 +package T1; + +public class washingMachine extends Electrica{ + private String MotorType; + private String WashingCapacity; + public washingMachine(){ + + } + + public void setMotorType(String MotorType){ + this.MotorType = MotorType; + } + public void setWashingCapacity(String WashingCapacity){ + this.WashingCapacity = WashingCapacity; + } + + public String getMotorType(){ + return this.MotorType = MotorType; + } + public String getWashingCapacity(){ + return this.WashingCapacity = WashingCapacity; + } + public washingMachine(String MotorType,String WashingCapacity ){ + this.MotorType = MotorType; + this.WashingCapacity = WashingCapacity; + } + + public String toString(){ + return "洗衣机的品牌为:" + super.getBrand() + "\n型号为:" + super.getModel() + + "\n颜色为:" + super.getColour() + "\n售价为:" + super.getPrice() + + "\n电机类型为:" + getMotorType() + "\n洗涤容量为:" + getWashingCapacity(); + } +} + + +//子类 电视 +package T1; + +public class Television extends Electrica { + private String screen; + private String resolutionRatio; + + public Television(){ + + } + + public void setScreen(String screen){ + this.screen = screen; + } + public void setResolutionRatio(String resolutionRatio){ + this.resolutionRatio = resolutionRatio; + } + + public String getScreen(){ + return this.screen =screen; + } + public String getResolutionRatio(){ + return this.resolutionRatio = resolutionRatio; + } + + public Television(String screen,String resolutionRatio){ + this.screen = screen; + this.resolutionRatio = resolutionRatio; + } + + public String toString(){ + return "电视的品牌为:" + super.getBrand() + "\n型号为:" + super.getModel() + + "\n颜色为:" + super.getColour() + "\n售价为:" + super.getPrice() + + "\n屏幕尺寸为:" + getScreen() + "\n分辨率为:" + getResolutionRatio(); + } +} + + +//测试 +package T1; + +public class Test { + public static void main(String[] args) { + //冰箱 + Refrigerator re = new Refrigerator("双开门","速冻"); + re.setBrand("华为"); + re.setModel("超大型"); + re.setColour("白色"); + re.setPrice(1000); + System.out.println(re); + System.out.println(); + + //洗衣机 + washingMachine wa = new washingMachine("超大型","10T"); + wa.setBrand("苹果"); + wa.setModel("苹果No.1"); + wa.setColour("红色"); + wa.setPrice(1200); + System.out.println(wa); + System.out.println(); + + //电视 + Television te = new Television("8k","1920x1080"); + te.setBrand("三星"); + te.setModel("三星No.1"); + te.setColour("黑色"); + te.setPrice(2200); + System.out.println(te); + } +} + +``` + +```java +我们计划为一个动物园制作一套信息管理系统,根据与甲方沟通,我们归纳了有以下几种动物需要记录到系统中: + 鸟类: 鹦鹉、猫头鹰、喜鹊 + 哺乳类: 大象、狼、长颈鹿 + 爬行类: 鳄鱼、蛇、乌龟 + //爷类 + package T2; + +public class Animal { + private String name; + private String sex; + + public Animal(){ + + } + + public void setName(String name){ + this.name = name; + } + public void setSex(String sex){ + this.sex = sex; + } + + public String getName(){ + return this.name = name; + } + public String getSex(){ + return this.sex = sex; + } +} + + +//父类 鸟类 +package T2.Bird; + +import T2.Animal; + +public class Bird extends Animal { + private String speedPerHour; + private String colour; + + public Bird(){ + + } + + public void setSpeedPerHour(String speedPerHour){ + this.speedPerHour = speedPerHour; + } + public void setColour(String colour){ + this.colour = colour; + } + + public String getSpeedPerHour(){ + return this.speedPerHour = speedPerHour; + } + public String getColour(){ + return this.colour = colour; + } +} + + +//鹦鹉类 +package T2.Bird; + +public class Parrot extends Bird{ + private String speak; + public Parrot(){ + + } + public void setSpeak(String speak){ + this.speak = speak; + } + + public String getSpeak(){ + return this.speak = speak; + } + + public Parrot(String speak){ + this.speak = speak; + } + + public String toString(){ + return "这只名叫" + super.getName() + "的" + super.getColour() + + "的"+ super.getSex() +"鹦鹉正在说:" + getSpeak() + "\n它飞行的速度为:" + getSpeedPerHour(); + } +} + +//鹦鹉类 +package T2.Bird; + +public class Owl extends Bird{ + private String sleep; + + public Owl(){ + + } + public void setSleep(String sleep){ + this.sleep = sleep; + } + + public String getSleep(){ + return this.sleep = sleep; + } + + public Owl(String sleep){ + this.sleep = sleep; + } + + public String toString(){ + return "这只名叫" + super.getName() + "的" + super.getColour() + + "的"+ super.getSex() +"猫头鹰正在:" + getSleep() + "\n它飞行的速度为:" + getSpeedPerHour(); + } +} + + +//喜鹊 +package T2.Bird; + +public class Magpie extends Bird{ + private String speak; + + public Magpie(){ + + } + + public void setSpeak(String speak){ + this.speak = speak; + } + + public String getSpeak(){ + return this.speak = speak; + } + + public Magpie(String speak){ + this.speak = speak; + } + + public String toString(){ + return "这只名叫" + super.getName() + "的" + super.getColour() + + "的"+ super.getSex() +"喜鹊正在:" + getSpeak() + "\n它飞行的速度为:" + getSpeedPerHour(); + } +} + + +//测试 +package T2.Bird; + +public class TestBird { + public static void main(String[] args) { + Parrot p = new Parrot("你好呀"); + p.setName("小樱"); + p.setSex("公"); + p.setColour("红白相间"); + p.setSpeedPerHour("10米每秒"); + System.out.println(p); + + System.out.println(); + + Owl o = new Owl("呼呼大睡"); + o.setName("小猫"); + o.setSex("母"); + o.setColour("黑色"); + o.setSpeedPerHour("20米每秒"); + System.out.println(o); + + System.out.println(); + + Magpie m = new Magpie("吃小鱼干"); + m.setName("小白"); + m.setSex("母"); + m.setColour("黑色"); + m.setSpeedPerHour("15米每秒"); + System.out.println(m); + } +} + + +//哺乳类 父类 +package T2.Lactation; + +import T2.Animal; + +public class Lactation extends Animal { + private String hairColour; + + public Lactation(String hairColour) { + this.hairColour = hairColour; + } + + public Lactation() { + + } + + public String getHairColour() { + return hairColour; + } + + public void setHairColour(String hairColour) { + this.hairColour = hairColour; + } +} + + +//大象 +package T2.Lactation; + +public class Elephant extends Lactation{ + private double height; + + public Elephant(double height) { + this.height = height; + } + + public Elephant() { + } + + public double getHeight() { + return height; + } + + public void setHeight(double height) { + this.height = height; + } + + @Override + public String toString() { + return "这个名为" + super.getName() + "的" + super.getHairColour() +super.getSex() + + "大象" + "\n他的身高有:" + getHeight() + "米"; + } +} + + +//狼 +package T2.Lactation; + +public class Wolf extends Lactation{ + private String eat; + + public Wolf(String eat) { + this.eat = eat; + } + + public Wolf() { + } + + public String getEat() { + return eat; + } + + public void setEat(String eat) { + this.eat = eat; + } + + @Override + public String toString() { + return "这个名为" + super.getName() + "的" + super.getHairColour() +super.getSex() + + "狼" + "\n正在吃:" + getEat() ; + } +} + + +//长颈鹿 +package T2.Lactation; + +public class Giraffe extends Lactation{ + private String height; + + public Giraffe(String height) { + this.height = height; + } + + public Giraffe() { + } + + public String getHeight() { + return height; + } + + public void setHeight(String height) { + this.height = height; + } + + + @Override + public String toString() { + return "这个名为" + super.getName() + "的" + super.getHairColour() +super.getSex() + + "长颈鹿" + "\n他的脖子有:" + getHeight() + "长" ; + } +} + + +//测试 +package T2.Lactation; + +public class TestLactation { + public static void main(String[] args) { + Elephant el = new Elephant(5); + el.setName("小象"); + el.setSex("公"); + el.setHairColour("黑色"); + System.out.println(el); + + System.out.println(); + + Wolf w = new Wolf("野生肉"); + w.setName("黑狼"); + w.setSex("公"); + w.setHairColour("白色"); + System.out.println(w); + + System.out.println(); + + Giraffe gi = new Giraffe("7"); + gi.setName("小鹿"); + gi.setSex("母"); + gi.setHairColour("黄色"); + System.out.println(gi); + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230425 Java\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230425 Java\344\275\234\344\270\232.md" new file mode 100644 index 0000000..b311310 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230425 Java\344\275\234\344\270\232.md" @@ -0,0 +1,26 @@ +```java +public class MyMath { +// 要求: +// +// 1. 该工具类不能有子类,不接受扩展。 +// 2. 该工具类不能被外界创建对象,构造方法需要私有化。 +// 3. 定义静态常量:PI,用来保存圆周率:3.14。 +// 4. 定义静态方法max,获取两个整数的较大值。 +// 5. 定义静态方法min,获取两个整数的较小值。 + private MyMath(){ + + } + + public static final double Pi = 3.14; + + public static int max(int a,int b) { + return (a >= b) ? a : b; + } + + public static int min(int a,int b){ + return (a <= b) ? b : a; + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" new file mode 100644 index 0000000..ac1858b --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" @@ -0,0 +1,194 @@ +```java +//父类是:战斗机 + +//1. 属性:名称,颜色,载弹量,打击半径 +//2. 功能:起飞,巡航,降落,雷达扫射,开火 +import java.util.ArrayList; + +public class Zdj { + private String name; + private String color; + private String dang; + private String ban; + + public Zdj(String name, String color, String dang, String ban) { + this.name = name; + this.color = color; + this.dang = dang; + this.ban = ban; + } + + public Zdj() { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String getDang() { + return dang; + } + + public void setDang(String dang) { + this.dang = dang; + } + + public String getBan() { + return ban; + } + + public void setBan(String ban) { + this.ban = ban; + } + + public void fei(){ + System.out.println(color + "的" + name + "正在起飞"); + } + + public void xun(){ + System.out.println(color + "的" + name + "正在巡逻"); + } + + public void jiang(){ + System.out.println(color + "的" + name +"正在降落"); + } + + public void sao(){ + System.out.println(color + "的" + name +"正在扫射"); + } + + public void kai(){ + System.out.println(color + "的" + name +"遭受敌人的攻击,正在开火"); + } + + +} + +//接口:对空,对地 +public interface kong{ + void lan(); + void fangKong(); +} + +public interface di { + void duiDi(); + void saoShe(); +} +//子类: + +//1. 空空战斗机(只对空) +//2. 空地战斗机(能对空能对地) +//3. 轰炸机 (只能对地) +public class kong01 extends Zdj implements kong{ + @Override + public void lan() { + System.out.println(super.getName() + "正在拦截导弹的追踪"); + } + + @Override + public void fangKong() { + System.out.println(super.getName() + "发射防空导弹"); + } +} + +public class D1 extends Zdj implements di{ + @Override + public void duiDi() { + System.out.println(getName() + "正在发射对地导弹"); + } + + @Override + public void saoShe() { + System.out.println(getName() + "正在对地面进行扫射轰炸"); + } +} + +public class KongDi extends Zdj implements di,kong{ + @Override + public void duiDi() { + System.out.println(getName() + "正在对地面进行猛烈的轰炸"); + } + + @Override + public void saoShe() { + System.out.println(getName() + "正在对地面进行扫射"); + } + + @Override + public void lan() { + System.out.println(getName() + "正在阻止空中的战机"); + } + + @Override + public void fangKong() { + System.out.println(getName() + "正在拦截空中的导弹"); + } +} + + +//测试类: + +//1. 生成每一种战斗并属性赋值,并调用相关功能 + +public class TestKongDi { + public static void main(String[] args) { + KongDi k = new KongDi(); + k.setName("陆空战斗机"); + k.setColor("蓝色"); + k.setDang("20kg"); + k.setBan("100公里"); + k.lan(); + k.fangKong(); + k.fei(); + k.xun(); + k.kai(); + k.sao(); + k.saoShe(); + k.duiDi(); + } +} + + +public class TestDi { + public static void main(String[] args) { + D1 d = new D1(); + d.setName("对地战斗机"); + d.setColor("黑色"); + d.setBan("6公里"); + d.setDang("10kg"); + d.duiDi(); + d.saoShe(); + } +} + +public class Test { + public static void main(String[] args) { + kong01 k = new kong01(); + k.setName("空空战斗机"); + k.setColor("红色"); + k.setDang("5kg"); + k.setBan("5公里"); + k.lan(); + k.fangKong(); + k.fei(); + k.xun(); + k.kai(); + k.sao(); + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230506 \346\216\245\345\217\243\351\242\230\347\233\2562.md" "b/25 \346\235\216\346\226\207\346\235\260/20230506 \346\216\245\345\217\243\351\242\230\347\233\2562.md" new file mode 100644 index 0000000..6b5ecba --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230506 \346\216\245\345\217\243\351\242\230\347\233\2562.md" @@ -0,0 +1,165 @@ +## 接口题目2 + +1. 请定义“员工(类)”: + 属性:姓名、性别、年龄(全部私有) + 行为:工作(抽象) + 无参、全参构造方法 + get/set方法 + +2. 请定义“绘画(接口)” + 抽象方法:绘画 +3. 请定义“Java讲师类”继承自“员工类” +4. 请定义”UI讲师类”,继承自“员工类”,并实现“绘画”接口。 + +**要求**: + +1. 请按上述要求设计出类结构,并实现相关的方法。 +2. 测试类中创建对象测试,属性可自定义: + - 创建一个Java讲师类对象,调用工作的方法。 + - 创建一个UI讲师类对象,调用工作方法,和绘画方法。 + +**答案:** + +```java +//员工类 +package Light; + +public abstract class Employee { + private String name; + private String sex; + private int age; + + public abstract void work(); + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public Employee(String name, String sex, int age) { + this.name = name; + this.sex = sex; + this.age = age; + } + + public Employee() { + } +} + +// 接口 +package Light; + +public interface Draw { +// 绘画 + void drawing(); +} + + +// JavaTeacher类 +package Light; + +public class JavaTeacher extends Employee{ + +// 工作 + @Override + public void work() { + System.out.println(getName()+"讲Java课"); + } + + public JavaTeacher(String name, String sex, int age) { + super(name, sex, age); + } + + public JavaTeacher() { + } + + @Override + public String toString() { + return "JavaTeacher{name="+getName()+",gender="+getSex()+",age="+getAge()+"}"; + } +} + +// UITeacher类 +package Light; + +public class UITeacher extends Employee implements Draw { + +// 工作 + @Override + public void work() { + System.out.println(getName()+"讲UI设计课"); + } + +// 绘画 + @Override + public void drawing() { + System.out.println(getName()+"画画"); + } + + + public UITeacher(String name, String sex, int age) { + super(name, sex, age); + } + + public UITeacher() { + } + + @Override + public String toString() { + return "UITeacher{name="+getName()+",gender="+getSex()+",age="+getAge()+"}"; + } +} + + +//测试 +package Light; + +public class Tesx { + public static void main(String[] args) { +// 小明 + JavaTeacher javaTeacher=new JavaTeacher("小明","男",25); + System.out.println(javaTeacher); + javaTeacher.work(); + +// 小红 + UITeacher uiTeacher=new UITeacher("小红","女",18); + System.out.println(uiTeacher); + uiTeacher.work(); + uiTeacher.drawing(); + } +} +``` + + + +**运行结果:** + +```java +JavaTeacher{name='小明', gender='男', age=25} +小明讲Java课 +UITeacher{name='小红', gender='女', age=18} +小红讲UI设计课 +小红画画 +``` + + + -- Gitee From db78e44ad44ed9dd804ea1d5ed7b9ffddff705f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0?= <11802249+lwjlmy@user.noreply.gitee.com> Date: Thu, 11 May 2023 12:51:06 +0000 Subject: [PATCH 16/17] =?UTF-8?q?=E5=88=A0=E9=99=A4=E6=96=87=E4=BB=B6=2025?= =?UTF-8?q?=20=E6=9D=8E=E6=96=87=E6=9D=B0?= 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" | 260 ------- ...51\345\261\225\350\256\255\347\273\203.md" | 252 ------- ...60\347\273\204\344\275\234\344\270\232.md" | 148 ---- ...71\350\261\241\344\275\234\344\270\232.md" | 47 -- ...32\345\222\214\347\254\224\350\256\260.md" | 130 ---- .../20230414 java\344\275\234\344\270\232.md" | 682 ------------------ ...32\345\222\214\347\254\224\350\256\260.md" | 290 -------- ...06\345\220\210\344\275\234\344\270\232.md" | 205 ------ ...41\347\220\206\347\263\273\347\273\237.md" | 254 ------- ...47\346\211\277\344\275\234\344\270\232.md" | 517 ------------- .../20230425 Java\344\275\234\344\270\232.md" | 26 - ...45\345\217\243\344\275\234\344\270\232.md" | 194 ----- ...5\345\217\243\351\242\230\347\233\2562.md" | 165 ----- 13 files changed, 3170 deletions(-) delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230408 java\347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230415 JavaAPI\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230422 Java\347\273\247\346\211\277\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230425 Java\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" delete mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230506 \346\216\245\345\217\243\351\242\230\347\233\2562.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" deleted file mode 100644 index 67a659d..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" +++ /dev/null @@ -1,260 +0,0 @@ -```java -import java.util.Scanner; - -public class java1 { -// **1、判断一个字符数据是否是数字字符 ** -// -// **分析:** -// -// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 -// -// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, -// 并且还要下于或等于字符9即可。 -// -// 3、判断完成之后,打印判断的结果。 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int i = sc.nextInt(); - if (i>=0 && i<10){ - System.out.println("是数字字符"); - }else { - System.out.println("不是数字字符"); - } - } -} - -import java.util.Scanner; - -public class java2 { - /* **2、判断一个字符数据是否是字母字符** - - **分析:** - - 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 - - 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, - 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z - - 3、判断完成之后,打印判断的结果。*/ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - char st = sc.next().charAt(0); - - if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) - - System.out.println( "是字母"); - - else - - System.out.println("不是字母"); - - } -} - - -import java.util.Scanner; - -public class java3 { -/* - **3、判断指定的年份是否为闰年,请使用键盘录入** - - **分析:** - - 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 - - 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 - -*/ - - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int i = sc.nextInt(); - if ((i / 4 == 0 && i / 400 ==0) || i / 100 != 0 ){ - System.out.println("是闰年"); - }else{ - System.out.println("不是闰年"); - } - } -} - - -import java.util.Scanner; - -public class java4 { - // **4、判断一个数字是否为水仙花数,请使用键盘录入** -// -// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, -// -// **分析:** -// -// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 -// -// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 -// -// 2、判断的过程 -// -// a) 将3位数字的每一位上的数字拆分下来 -// -// b) 计算每位数字的3次幂之和 -// -// C) 用和值 和 原来的数字进行比较 -// -// D) 打印判断的比较结果即可 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入一个数字判断是不是水仙花数"); - int i = sc.nextInt(); - int ge = i % 10; - int si = i / 10 % 10; - int bai = i / 100 % 10; - if (i > 99 && i < 1000) { - if (ge * ge * ge + si * si * si + bai * bai * bai == i) { - System.out.println("是水仙花数"); - } else { - System.out.println("不是水仙花数"); - } - }else { - System.out.println("您好,水仙花数是三位数"); - } - } -} - - -import java.util.Scanner; - -public class java5 { - /* **5、判断一个5位数字是否为回文数,使用键盘录入** - - 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 - - **分析:** - - 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 - - 2、判断的过程 - - a) 将5位数字的万、千、十、个位数拆分出来 - - b) 判断比较万位和个位 、 千位和十位是否相等 - - 3、判断完成之后,打印判断的结果。*/ - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入五位数判断是不是回文数"); - int i = sc.nextInt(); - int ge = i % 10; - int si = i / 10 % 10; - int qian = i / 1000 % 10; - int wan = i / 10000 % 10; - if (i > 9999 && i < 100000) { - if (ge == wan && si == qian) { - System.out.println("是回文数"); - } else { - System.out.println("不是回文数"); - } - }else { - System.out.println("您输入的不是五位数"); - } - } -} - - -import java.util.Scanner; - -public class java6 { -// ## 题目1(训练) -// -// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: -// -// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 -// -// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 -// -// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? -// -// ### 训练提示 -// -//1. 已知的父母身高如何用代码体现? -// -// 2. 题目中的公式如何转化为代码? -// -// ### 解题方案 -// -//1. 使用变量的定义和算术运算符完成本题 -// -//### 操作步骤 -// -//1. 定义小数变量代表父亲身高 -// -//2. 定义小数变量代表母亲身高 -// -//3. 通过儿子身高计算方式计算儿子身高 -// -//4. 通过女儿身高计算方式计算女人身高 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入父亲的身高"); - double baba = sc.nextInt(); - System.out.println("请输入母亲的身高"); - double mama = sc.nextInt(); - double erzi = (baba + mama) * 1.08 / 2; - double nver = (baba * 0.923 + mama) / 2; - System.out.println("儿子的身高为:" + erzi); - System.out.println("女儿的身高为:" + nver); - } -} - -public class java7 { -// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 -// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 -// 那么红茶和绿茶现在的钱一样多,请问对么? - public static void main(String[] args) { - int hocha = 21; - int lvcha = 24; - hocha = hocha * 2 + 3; - lvcha = lvcha * 2; - if (hocha == lvcha){ - System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); - }if (hocha > lvcha){ - System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); - }if (hocha < lvcha){ - System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); - } - } -} - - -public class java8 { -// 某小伙想定一份外卖,商家的优惠方式如下: -// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, -// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? - /*训练提示 - 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 - 解题方案 - 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 - 操作步骤 - 1. 使用算术运算符求出不使用优惠时的总价 - 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 - 3. 使用算术运算符求出使用优惠价时的总价 - 4. 使用三元运算符判断最终更合算的购买方式和花费*/ - public static void main(String[] args) { - int yuxian = 24; - int youza = 8; - int mifan = 3; - int zojia = yuxian + youza + mifan; - double youhuijia = zojia * 0.8; - System.out.println("使用折扣的总价:" + youhuijia); - int yuxian2 = 16; - int youza2 = 8; - int mifan2 = 3; - int zojia2 = yuxian2 + youza2 + mifan2; - System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); - if (youhuijia < zojia2){ - System.out.println("使用这个:" + youhuijia ); - }else if (youhuijia > zojia2){ - System.out.println("使用这个:" + zojia2); - } - } -} -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" deleted file mode 100644 index 81070fc..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" +++ /dev/null @@ -1,252 +0,0 @@ -~~~java -package test1; - -public class test1 { -// ## 1、强制类型转换练习 -// -//(1)先声明两个byte类型的变量b1,b2,并分别赋值为10和20,求b1和b2变量的和,并将结果保存在byte类型的变量b3中, -// 最后输出b3变量的值 -// -//(2)先声明两个short类型的变量s1,s2,并分别赋值为1000和2000,求s1和s2变量的和,并将结果保存在short类型的变量s3中, -// 最后输出s3变量的值 -// -//(3)先声明1个char类型的变量c1赋值为'a',再声明一个int类型的变量num赋值为5,求c1和num变量的和, -// 并将结果将结果保存在char类型的变量letter中,最后输出letter变量的值。 -// -// (4)先声明两个int类型的变量i1,i2,并分别赋值5和2,求i1和i2的商,并将结果保存在double类型的变量result中, -// 最后输出result变量的值。如何得到结果2.5呢? - public static void main(String[] args) { - byte b1 = 10; - byte b2 = 20; - byte b3 = (byte)(b1 + b2); - System.out.println("他们的和为:" + b3); - - short s1 = 1000; - short s2 = 2000; - short s3 = (short)(s1 + s2); - System.out.println("他们的和为:" + s3); - - char c1 = 'a'; - int num = 5; - char letter = (char)(c1 + 5); - System.out.println("他们的和为:" + letter); - - int i1 = 5; - int i2 = 2; - double result = ((double) i1 / i2) ; - System.out.println("他们的商为:" + result); - } -} - - -package test1; - -public class test2 { -// 1. 定义两个int类型变量a1和a2,分别赋值10,11,判断变量是否为偶数,拼接输出结果 -// 2. 定义两个int类型变量a3和a4,分别赋值12,13,判断变量是否为奇数,拼接输出结果 - public static void main(String[] args) { - int a1 = 10; - int a2 = 11; - if (a1 % 2 ==0){ - System.out.println("10是偶数?" + true); - } - if (a2 % 2 == 0){ - System.out.println("11是偶数?" + true); - }else - System.out.println("11是偶数?" + false); - - int a3 = 12; - int a4 = 13; - if (a3 % 2 ==0){ - System.out.println("12是偶数?" + true); - } - if (a4 % 2 == 0){ - System.out.println("13是偶数?" + true); - }else - System.out.println("13是偶数?" + false); - } -} - - -package test1; - -public class test3 { -// 1. 定义一个int类型变量hours,赋值为89 -//2. 定义一个int类型变量day,用来保存89小时中天数的结果 -//3. 定义一个int类型变量hour,用来保存89小时中不够一天的剩余小时数的结果 -//4. 输出结果 - - - public static void main(String[] args) { - int hours = 89; - int day = 89 / 24; - int hour = 89 % 24; - System.out.println("为抵抗洪水,战士连续作战89小时:"); - System.out.println("89天是" + day + '天' + hour + "小时"); - } -} - - -package test1; - -public class test4 { -// 1. 定义一个int类型变量week,赋值为2 -//2. 修改week的值,在原值基础上加上100 -//3. 修改week的值,在原值基础上模以7 -//4. 输出结果,在输出结果的时候考虑特殊值,例如周日 - public static void main(String[] args) { - int week = 2; - System.out.print("今天是周"+week+','); - week = week + 100 ; - System.out.print(100+"天以后是"); - week = week % 7; - System.out.print("周"+week); - - } -} - - -package test1; - -public class test5 { -// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 -// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) -// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) -// 4. 输出结果 - public static void main(String[] args) { - int x = (int)(Math.random()*(10000-0)); - int y = (int)(Math.random()*(10000-0)); - int z = (int)(Math.random()*(10000-0)); - int max; - max = x > y ? x : y; - int maxx = max > z ? max : z ; - System.out.println((x)+"," + (y)+"," + (z)+"," + "最大值为:"+ max); - } -} - - -package test1; - -import java.util.Scanner; - -public class test6 { -// 闰年的判断标准是: -// -// 1)可以被4整除,但不可被100整除 -// -// 2)可以被400整除 -// -// -// 1. 定义一个int类型变量year,赋值为今年年份值 -// 2. 定一个一个boolean类型变量,用来保存这个年份是否是闰年的结果 -// 3. 输出结果 - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入年份判断是不是闰年"); - int year = sc.nextInt(); - if ((year % 4 == 0 && year % 100 !=0) || year % 400 ==0 ){ - System.out.println(year + "是闰年"); - }else { - System.out.println(year + "不是闰年"); - } - } -} - - -package test1; - -public class test7 { -// 1. 定义一个double类型变量hua,存储华氏温度80 -//2. 定义一个double类型变量she,存储摄氏温度,根据公式求值 -//3. 输出结果 - public static void main(String[] args) { - double hua = 80; - double she = (hua - 32) / 1.8; - System.out.println("华氏度80.0°F转化摄氏度是" + she); - } -} - - -# 拔高题 - -## 第一题 - -```java -如下代码的计算结果是: -int i = 1; -i *= 0.2; -i++; -System.out.println("i=" + i); - -i=1 -``` - -## 第二题 - -```java -如下代码的运算结果是: -int i = 2; -i *= i++; - -int j = 2; -j *= j+1; - -int k = 2; -k *= ++k; - -System.out.println("i=" + i); -System.out.println("j=" + j); -System.out.println("k=" + k); - - -i=4 -j=6 -k=6 -``` - -## 第三题 - -```java -如下代码的运算结果是: -int a = 3; -int b = 1; -if(a = b){ - System.out.println("Equal"); -}else{ - System.out.println("Not Equal"); -} - -Not Equal -``` - -## 第四题 - -```java -如下代码的运算结果是: -int a = 8, b = 3; -System.out.println(a>>>b); -System.out.println(a>>>b | 2); - - -1 -3 -``` - -## 第五题 - -如何用最有效的的方法计算2乘以8 - -```java -package test1; - -public class test8 { - public static void main(String[] args) { - int num = 2 * 8; - System.out.println(num); - } -} -``` - - -~~~ - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" deleted file mode 100644 index dc4fd7e..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" +++ /dev/null @@ -1,148 +0,0 @@ -```java -public class T1 { - public static void main(String[] args) { -// .定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** -// -//**操作步骤:** -// -// 1.定义5个元素数组 -// 2.可以使用初始化数组的两种方式之一为数组元素赋值 -// 3.遍历数组求数组中的最小值 - - - int[] sum={12,32,52,2,5};//创立数组并赋值 - - int min=sum[0]; - for (int i=1;isum[i]){ - min=sum[i]; - } - } - System.out.println("数组sum的最小值为:"+min); - } -} -``` - - - -```java -package Glass; - -import java.util.Arrays; - -public class T2 { - public static void main(String[] args) { -// 需求:求出数组中索引与索引对应的元素都是奇数的元素** -// -//**分析:** -// -// 1、遍历数组 -// 2、判断索引是否是奇数(索引 % 2 != 0) -// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) -// 4、满足条件输出结果 - - int[] sum={12,4,32,6,32,11}; - System.out.println(Arrays.toString(sum)); - - for (int i=0;i= 0; - } - -// 包含一个方法int approximateNumberCount()方法, -// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 - public int approximateNumberCount(){ - int count = 0; - for (int i = 1; i <= value; i++) { - if (value % i == 0){ - count++; - } - } - return count; - } - -// 包含一个方法boolean isPrimeNumber()方法, -// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, -// 并且value是大于1的自然数,那么value就是素数。 - public boolean isPrimeNumber(){ - int count = 0; - boolean flag = false; - for (int i = 1; i < value ; i++) { - count++; - if (value % i == 0 && value % 1 == 0 && value > 1){ - if (count >= 3){ - flag = false; - }else { - flag = true; - } - } - } - return flag; - } -// 包含一个方法int[] getAllPrimeNumber()方法, -// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 - public int[] getAllPrimeNumber(){ - int[] arr = new int[approximateNumberCount()]; - int index = arr[0]; - for (int i = 1; i <= value; i++) { - if (value % i == 0){ - arr[index++]=i; - } - } - return arr; - } - - @Override - public String toString() { - return "MyInt{" + - "\nvalue=" + value + - "\n是不是自然数:" + isNatural() + - "\n约数个数:" + approximateNumberCount() + - "\n是不是素数:" + isPrimeNumber() + - "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + - "\n}"; - } -} - -package T6; - -public class TestMyLnt { - public static void main(String[] args) { - MyLnt lnt = new MyLnt(20); - System.out.println(lnt); - } -} - -``` - -# 第七题 - -```java -package T7; - -public class TestYue { - public static void main(String[] args) { - Yue y = new Yue(); - int min = y.min(44,33,88,342,454,234,5,2); - System.out.println("最小值为:" + min); - int max = y.maxApproximate(4,8,16,20,24); - System.out.println("最大公约数为:" + max); - } -} - - -package T7; - -public class Yue { -// 返回n个整数中的最小值 - int min(int... nums){ - int min = nums[0]; - for (int i = 0; i < nums.length; i++) { - if (min > nums[i]){ - min = nums[i]; - } - } - return min; - } - - public int maxApproximate(int... nums){ - int max=min(nums); - for (int i = max; i >= 1; i--) { - boolean flag=true; - for (int j = 0; j < nums.length; j++) { - if (nums[j]%i!=0){ - flag=false; - break; - } - } - if (flag==true){ - return i; - } - } - return 1; - } -} - -``` - -# 第八题 - -```java -package T8; - -public class ArraysTools { - public String toString(int[]arr) { - String str = "["; - for (int i = 0; i < arr.length; i++) { - int i1 = arr[i]; - str += i1; - if (i < arr.length - 1) { - str += ","; - } - } - str += "]"; - return str; - } - int[] grow(int[] arr){ - int shu = arr.length; - int[] newArr = new int[2 * shu]; - for (int i = 0; i < arr.length; i++) { - newArr[i] = arr[i]; - } - return newArr; - } -} - -package T8; - -public class Test { - public static void main(String[] args) { - int[]old={11,22,33,44}; - ArraysTools arraysTools = new ArraysTools(); - System.out.println(arraysTools.toString(old)); - int[]newArr= arraysTools.grow(old); - System.out.println(arraysTools.toString(newArr)); - } -} - -``` - -# 第九题 - -```java -package T9; - -public class MathTools { - int compare(int a, int b){ - if (a > b){ - return a; - }else if (a < b){ - return -a; - }else { - return 0; - } - } -/* -* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, -* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; -* */ - int compare(double a, double b){ - if (a > b){ - return (int)a; - }else if (a < b){ - return (int)-a; - }else { - return 0; - } - } - -// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, -// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; - int compare(char a, char b){ - if (a > b){ - return (char)a; - }else if (a < b){ - return (char)-a; - }else { - return 0; - } - } -} - - -package T9; - -public class Test { - public static void main(String[] args) { - MathTools ma = new MathTools(); - int a = ma.compare(3,5); - int b = ma.compare('2','3'); - int c = ma.compare(33.5,22.4); - System.out.println(a); - System.out.println(b); - System.out.println(c); - } -} - -``` - -# 第十题 - -```java -package T10; - -public class ArraysTools { - public void sort(int[] arr){ - for (int i = 1; i < arr.length; i++) { - for (int j = 0; j < arr.length - i; j++) { - if (arr[j] > arr[j + 1]){ - int temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } - public void sort(double[] arr){ - for (int i= 1; i arr[j+1]){ - double temp=arr[j]; - arr[j]=arr[j+1]; - arr[j+1]=temp; - } - } - } - } - - public void sort(char[] arr) { - for (int i = 1; i < arr.length; i++) { - for (int j = 0; j < arr.length - i; j++) { - if (arr[j] > arr[j + 1]) { - char temp = arr[j]; - arr[j] = arr[j + 1]; - arr[j + 1] = temp; - } - } - } - } - public String toString(int[] arr){ - String re="["; - for (int i = 0; i < arr.length; i++) { - if (i我***你 - -##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 - -String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] - -## String方法小结 - -| **方法名** | **说明** | -| ----------------------------------------------------- | -------------------------------- | -| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | -| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | -| public int length() | 返回此字符串的长度 | - -| public char charAt(int index) | 返回指定索引处的 char 值 | -| ----------------------------- | ---------------------------- | -| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | - -| **方法名** | **说明** | -| ------------------------------------------------------------ | ------------------------------------------------------------ | -| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | -| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | -| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | - -| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | -| ----------------------------------- | ---------------------------------------- | -| | | - -## StringBuilder - -StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 - -作用:提高字符串的操作效率 - -### StringBuilder 构造方法 - -| **方法名** | **说明** | -| -------------------------------- | ------------------------------------------ | -| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | -| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | - -### StringBuilder 的常用方法 - -| **方法名** | **说明** | -| -------------------------------------- | ------------------------ | -| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | -| public StringBuilder reverse() | 返回相反的字符序列 | - -| public int length() | 返回长度 ( 字符出现的个数) | -| ------------------- | -------------------------- | -| | | - -| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | -| ------------------------ | --------------------------------------------------- | -| | | - -## StringBuilder和String的区别 - -**String** :内容是不可变的 - -**StringBuilder**:内容是可变的 - -## StringBuilder 和 String 相互转化 - -### StringBuilder 转换为 String - -public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String - -### String 转换为 StringBuilder - -public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder - -# 作业 - -```java -import java.util.Scanner; - -public class Test1 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - String password = "12345"; - System.out.println("请输入密码登陆"); - int count = 0; - for (int i = 0; i < 3; i++) { - String st = sc.next(); - count++; - if (st.equals(password)){ - System.out.println("密码正确,登陆成功"); - break; - }else { - System.out.println("密码错误"); - System.out.println("您还有" + (3 - count) + "次"); - } - } - } -} - - -import java.util.Scanner; - -public class Test2 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - System.out.println("请输入字符串遍历数组"); - String st = sc.next(); - for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ - da++; - } - if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ - xiao++; - } - if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ - shu++; - } - } - System.out.println("大写出现的次数:" + da); - System.out.println("小写出现的次数:" + xiao); - System.out.println("数字出现的次数:" + shu); - } -} - - -import javax.swing.*; -import java.util.Scanner; - -public class Test4 { - public static void main(String[] args) { - System.out.println("请输入电话号码"); - Scanner sc = new Scanner(System.in); - String st = sc.next(); - String s1 = st.substring(0,3); - String s2 = st.substring(7); - System.out.println("转换后的电话号码为:" + s1 + "****" + s2); - } -} - - -import java.util.Scanner; - -public class Test5 { - public static void main(String[] args) { - System.out.println("请输入脏话"); - Scanner sc = new Scanner(System.in); - String st = sc.next(); - String s1 = st.replace("TMD","不可以说脏话"); - System.out.println(s1); - } -} - - -public class Student { - private String name; - private int age; - - public Student(String name,int age){ - this.name = name; - this.age = age; - } - - public Student(){ - - } - - public void setName(String name){ - this.name = name; - } - - public void setAge(int age){ - this.age = age; - } - - public String getName(){ - return this.name = name; - } - - public int getAge(){ - return this.age = age; - } -} - - -import java.util.Arrays; -import java.util.Scanner; - -public class TestStudent6 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - Student st = new Student(); - System.out.println("请输入名字"); - st.setName(sc.next()); - System.out.println("请输入年龄"); - st.setAge(sc.nextInt()); - String st2 = st.getName() +","+ st.getAge(); - String[] arr = st2.split(","); - System.out.println(Arrays.toString(arr)); - - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" deleted file mode 100644 index db5bfa5..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" +++ /dev/null @@ -1,205 +0,0 @@ -```java -//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 -public class Student { - private String name; - private int age; - - public Student(String name, int age) { - this.name = name; - this.age = age; - } - - public Student(String name) { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - -} - -import java.util.ArrayList; - -public class TestStudent { - public static void main(String[] args) { - ArrayList list = new ArrayList<>(); - list.add(new Student("张三",18)); - list.add(new Student("李四",19)); - list.add(new Student("王五",20)); - for (int i = 0; i < list.size(); i++) { - Student stu = list.get(i); - System.out.print(stu.getName() + " "); - System.out.println(stu.getAge()); - } - } -} - -``` - -```java -//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 -//学生的姓名和年龄来自于键盘录入 -public class Student { - private String name; - private int age; - - public Student(String name, int age) { - this.name = name; - this.age = age; - } - - public Student(String name) { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - -} -import java.security.AllPermission; -import java.util.ArrayList; -import java.util.Scanner; - -public class TestStudent2 { - public static void main(String[] args) { - Scanner sc = new Scanner(System.in); - int count = 0; - ArrayList list = new ArrayList<>(); - while (true) { - count++; - System.out.println("请输入第" + count + "名学生的姓名和年龄"); - list.add(new Student(sc.next(), sc.nextInt())); - if (count == 3){ - break; - } - } - for (int i = 0; i < list.size(); i++) { - Student stu = list.get(i); - System.out.print(stu.getName() + " "); - System.out.println(stu.getAge()); - } - } -} - -``` - -```java - -import java.util.ArrayList; -import java.util.Iterator; - -public class T2 { - public static void main(String[] args) { -// 需求:创建一个存储String的集合,内部存储 -// (test,张三,李四,test,test)字符串 -// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 - ArrayList list = new ArrayList<>(); - list.add("test"); - list.add("张三"); - list.add("李四"); - list.add("test"); - list.add("test"); -// Iterator it = list.iterator(); -// while(it.hasNext()){ -// String str = it.next(); -// if (str.contains("test")){ -// it.remove(); -// } -// } - list.removeIf(s -> s.contains("test")); - System.out.println(list); - } -} - - -``` - -```java -//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 -//并存入新集合对象,方法返回新集合。 -public class Student { - private String name; - private int age; - public Student(String name,int age){ - this.name = name; - this.age = age; - } - - public Student(){ - - } - - public void setName(String name){ - this.name = name; - } - public void setAge(int age){ - this.age = age; - } - - public String getName(){ - return this.name = name; - } - - public int getAge(){ - return this.age = age; - } - - - public String toString(){ - return "姓名:" + getName() + " " + "年龄:" + getAge(); - } -} - -import java.util.ArrayList; - -public class TestStudent { - public static void main(String[] args) { - ArrayList list = new ArrayList<>(); - list.add(new Student("张三", 19)); - list.add(new Student("李四", 20)); - list.add(new Student("王五", 10)); - list.add(new Student("赵六", 15)); - chaXun(new ArrayList<>(list)); - } - public static ArrayList chaXun(ArrayList list - ){ - ArrayList list1 = new ArrayList<>(); - for (int i = 0; i < list.size(); i++) { - Student st = list.get(i); - if (st.getAge()<18){ - list1.add(list.get(i)); - } - } - System.out.println(list1); - return list1; - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" deleted file mode 100644 index bc280be..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" +++ /dev/null @@ -1,254 +0,0 @@ -```java -package Light; - -public class Student { - private String StuID; - private String name; - private int age; - private String sex; - - - - public String getStuID() { - return StuID; - } - - public void setStuID(String stuID) { - StuID = stuID; - } - - public String getSex() { - return sex; - } - - public void setSex(String sex) { - this.sex = sex; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public Student(){} - public Student(String StuID,String name,int age,String sex){ - this.StuID=StuID; - this.name=name; - this.age=age; - this.sex=sex; - } - - @Override - public String toString() { - return "Student{" + - "StuID='" + StuID + '\'' + - ", name='" + name + '\'' + - ", age=" + age + - ", sex='" + sex + '\'' + - '}'; - } -} -``` - -```java -package Light; - -import java.util.ArrayList; -import java.util.Scanner; - -public class T { - public static void main(String[] args) { - ArrayList list=new ArrayList<>(); - Scanner sc=new Scanner(System.in); - - sum: - for (int i=0;;){ - System.out.println("============学生系统============"); - System.out.println("请输入编号:"+ - "\n\t1 添加学生信息"+ - "\n\t2 修改学生信息"+ - "\n\t3 删除学生信息"+ - "\n\t4 查看学生信息"+ - "\n\t5 退出学生系统"); - System.out.println(); - System.out.print("请输入编号:"); - String sum=sc.next();; - switch (sum){ - case "1": -// System.out.println("添加"); - insertStudent(list,sc); - break; - case"2": - updateStudent(list,sc); - break; - case"3": - deleteStudent(list,sc); - break; - case"4": - lookStudent(list,sc); - break; - case"5": - System.out.println("成功退出学生系统"); - break sum; - default: - System.out.println("输入编号错误,请重新输入!!!"); - System.out.println(); - } - } - } - - - /** - * 添加 - * @param list - * @param sc - */ - public static void insertStudent(ArrayList list, Scanner sc){ - System.out.println(); - System.out.println("===*请输入需要添加学生信息*==="); - System.out.println("请输入学号"); - String stuId=sc.next(); - - if (!list.isEmpty()){ - for (int i = 0; i< list.size(); i++){ - Student stu= list.get(i); - if (stuId.equals(stu.getStuID())){ - System.out.println("学号已经存在"); - break; - }else { - System.out.println("请输入姓名"); - String stuName=sc.next(); - System.out.println("请输入年龄"); - int stuAge=sc.nextInt(); - System.out.println("请输入性别"); - String stuSex=sc.next(); - list.add(new Student(stuId,stuName,stuAge,stuSex)); - } - } - }else { - System.out.println("请输入姓名"); - String stuName=sc.next(); - System.out.println("请输入年龄"); - int stuAge=sc.nextInt(); - System.out.println("请输入性别"); - String stuSex=sc.next(); - list.add(new Student(stuId,stuName,stuAge,stuSex)); - } - System.out.println(); - } - - - /** - * 修改 - * @param list - * @param sc - */ - - public static void updateStudent(ArrayList list,Scanner sc){ - System.out.println(); - if (list.isEmpty()){ - System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); - }else { - System.out.println("===修改学生信息==="); - System.out.println("请输入需要修改的学生学号"); - String stuId=sc.next(); - for (int i=0;i list,Scanner sc){ - System.out.println(); - if (list.isEmpty()){ - System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); - }else { - System.out.println("==*删除学生信息*=="); - System.out.println("请输入需要删除的学生学号"); - String stuId=sc.next(); - - for (int i=0;i list,Scanner sc){ - System.out.println(); - - if (list.isEmpty()) { - System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); - }else { - System.out.println("==*查看学生信息*=="); - System.out.println("请输入需要查看学生的学生学号"); - String stuId=sc.next(); - - for (int i=0;i= b) ? a : b; - } - - public static int min(int a,int b){ - return (a <= b) ? b : a; - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" deleted file mode 100644 index ac1858b..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" +++ /dev/null @@ -1,194 +0,0 @@ -```java -//父类是:战斗机 - -//1. 属性:名称,颜色,载弹量,打击半径 -//2. 功能:起飞,巡航,降落,雷达扫射,开火 -import java.util.ArrayList; - -public class Zdj { - private String name; - private String color; - private String dang; - private String ban; - - public Zdj(String name, String color, String dang, String ban) { - this.name = name; - this.color = color; - this.dang = dang; - this.ban = ban; - } - - public Zdj() { - - } - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getColor() { - return color; - } - - public void setColor(String color) { - this.color = color; - } - - public String getDang() { - return dang; - } - - public void setDang(String dang) { - this.dang = dang; - } - - public String getBan() { - return ban; - } - - public void setBan(String ban) { - this.ban = ban; - } - - public void fei(){ - System.out.println(color + "的" + name + "正在起飞"); - } - - public void xun(){ - System.out.println(color + "的" + name + "正在巡逻"); - } - - public void jiang(){ - System.out.println(color + "的" + name +"正在降落"); - } - - public void sao(){ - System.out.println(color + "的" + name +"正在扫射"); - } - - public void kai(){ - System.out.println(color + "的" + name +"遭受敌人的攻击,正在开火"); - } - - -} - -//接口:对空,对地 -public interface kong{ - void lan(); - void fangKong(); -} - -public interface di { - void duiDi(); - void saoShe(); -} -//子类: - -//1. 空空战斗机(只对空) -//2. 空地战斗机(能对空能对地) -//3. 轰炸机 (只能对地) -public class kong01 extends Zdj implements kong{ - @Override - public void lan() { - System.out.println(super.getName() + "正在拦截导弹的追踪"); - } - - @Override - public void fangKong() { - System.out.println(super.getName() + "发射防空导弹"); - } -} - -public class D1 extends Zdj implements di{ - @Override - public void duiDi() { - System.out.println(getName() + "正在发射对地导弹"); - } - - @Override - public void saoShe() { - System.out.println(getName() + "正在对地面进行扫射轰炸"); - } -} - -public class KongDi extends Zdj implements di,kong{ - @Override - public void duiDi() { - System.out.println(getName() + "正在对地面进行猛烈的轰炸"); - } - - @Override - public void saoShe() { - System.out.println(getName() + "正在对地面进行扫射"); - } - - @Override - public void lan() { - System.out.println(getName() + "正在阻止空中的战机"); - } - - @Override - public void fangKong() { - System.out.println(getName() + "正在拦截空中的导弹"); - } -} - - -//测试类: - -//1. 生成每一种战斗并属性赋值,并调用相关功能 - -public class TestKongDi { - public static void main(String[] args) { - KongDi k = new KongDi(); - k.setName("陆空战斗机"); - k.setColor("蓝色"); - k.setDang("20kg"); - k.setBan("100公里"); - k.lan(); - k.fangKong(); - k.fei(); - k.xun(); - k.kai(); - k.sao(); - k.saoShe(); - k.duiDi(); - } -} - - -public class TestDi { - public static void main(String[] args) { - D1 d = new D1(); - d.setName("对地战斗机"); - d.setColor("黑色"); - d.setBan("6公里"); - d.setDang("10kg"); - d.duiDi(); - d.saoShe(); - } -} - -public class Test { - public static void main(String[] args) { - kong01 k = new kong01(); - k.setName("空空战斗机"); - k.setColor("红色"); - k.setDang("5kg"); - k.setBan("5公里"); - k.lan(); - k.fangKong(); - k.fei(); - k.xun(); - k.kai(); - k.sao(); - } -} - -``` - diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230506 \346\216\245\345\217\243\351\242\230\347\233\2562.md" "b/25 \346\235\216\346\226\207\346\235\260/20230506 \346\216\245\345\217\243\351\242\230\347\233\2562.md" deleted file mode 100644 index 6b5ecba..0000000 --- "a/25 \346\235\216\346\226\207\346\235\260/20230506 \346\216\245\345\217\243\351\242\230\347\233\2562.md" +++ /dev/null @@ -1,165 +0,0 @@ -## 接口题目2 - -1. 请定义“员工(类)”: - 属性:姓名、性别、年龄(全部私有) - 行为:工作(抽象) - 无参、全参构造方法 - get/set方法 - -2. 请定义“绘画(接口)” - 抽象方法:绘画 -3. 请定义“Java讲师类”继承自“员工类” -4. 请定义”UI讲师类”,继承自“员工类”,并实现“绘画”接口。 - -**要求**: - -1. 请按上述要求设计出类结构,并实现相关的方法。 -2. 测试类中创建对象测试,属性可自定义: - - 创建一个Java讲师类对象,调用工作的方法。 - - 创建一个UI讲师类对象,调用工作方法,和绘画方法。 - -**答案:** - -```java -//员工类 -package Light; - -public abstract class Employee { - private String name; - private String sex; - private int age; - - public abstract void work(); - - public String getName() { - return name; - } - - public void setName(String name) { - this.name = name; - } - - public String getSex() { - return sex; - } - - public void setSex(String sex) { - this.sex = sex; - } - - public int getAge() { - return age; - } - - public void setAge(int age) { - this.age = age; - } - - public Employee(String name, String sex, int age) { - this.name = name; - this.sex = sex; - this.age = age; - } - - public Employee() { - } -} - -// 接口 -package Light; - -public interface Draw { -// 绘画 - void drawing(); -} - - -// JavaTeacher类 -package Light; - -public class JavaTeacher extends Employee{ - -// 工作 - @Override - public void work() { - System.out.println(getName()+"讲Java课"); - } - - public JavaTeacher(String name, String sex, int age) { - super(name, sex, age); - } - - public JavaTeacher() { - } - - @Override - public String toString() { - return "JavaTeacher{name="+getName()+",gender="+getSex()+",age="+getAge()+"}"; - } -} - -// UITeacher类 -package Light; - -public class UITeacher extends Employee implements Draw { - -// 工作 - @Override - public void work() { - System.out.println(getName()+"讲UI设计课"); - } - -// 绘画 - @Override - public void drawing() { - System.out.println(getName()+"画画"); - } - - - public UITeacher(String name, String sex, int age) { - super(name, sex, age); - } - - public UITeacher() { - } - - @Override - public String toString() { - return "UITeacher{name="+getName()+",gender="+getSex()+",age="+getAge()+"}"; - } -} - - -//测试 -package Light; - -public class Tesx { - public static void main(String[] args) { -// 小明 - JavaTeacher javaTeacher=new JavaTeacher("小明","男",25); - System.out.println(javaTeacher); - javaTeacher.work(); - -// 小红 - UITeacher uiTeacher=new UITeacher("小红","女",18); - System.out.println(uiTeacher); - uiTeacher.work(); - uiTeacher.drawing(); - } -} -``` - - - -**运行结果:** - -```java -JavaTeacher{name='小明', gender='男', age=25} -小明讲Java课 -UITeacher{name='小红', gender='女', age=18} -小红讲UI设计课 -小红画画 -``` - - - -- Gitee From d6d6505f333f726ac1b244401f60b36482a785e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0?= <3023096670@qq.com> Date: Thu, 11 May 2023 20:53:09 +0800 Subject: [PATCH 17/17] =?UTF-8?q?=E6=9D=8E=E6=96=87=E6=9D=B0=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" | 260 +++++++ ...51\345\261\225\350\256\255\347\273\203.md" | 252 +++++++ ...60\347\273\204\344\275\234\344\270\232.md" | 148 ++++ ...71\350\261\241\344\275\234\344\270\232.md" | 47 ++ ...32\345\222\214\347\254\224\350\256\260.md" | 130 ++++ .../20230414 java\344\275\234\344\270\232.md" | 682 ++++++++++++++++++ ...32\345\222\214\347\254\224\350\256\260.md" | 290 ++++++++ ...06\345\220\210\344\275\234\344\270\232.md" | 205 ++++++ ...41\347\220\206\347\263\273\347\273\237.md" | 254 +++++++ ...47\346\211\277\344\275\234\344\270\232.md" | 517 +++++++++++++ .../20230425 Java\344\275\234\344\270\232.md" | 26 + ...45\345\217\243\344\275\234\344\270\232.md" | 194 +++++ 12 files changed, 3005 insertions(+) create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230408 java\347\261\273\345\222\214\345\257\271\350\261\241\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230411 Java\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230414 java\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230415 JavaAPI\344\275\234\344\270\232\345\222\214\347\254\224\350\256\260.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230422 Java\347\273\247\346\211\277\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230425 Java\344\275\234\344\270\232.md" create mode 100644 "25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" new file mode 100644 index 0000000..67a659d --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230330 Java\347\254\254\344\270\200\346\254\241\344\275\234\344\270\232.md" @@ -0,0 +1,260 @@ +```java +import java.util.Scanner; + +public class java1 { +// **1、判断一个字符数据是否是数字字符 ** +// +// **分析:** +// +// 1、需要判断一个字符是否是数字字符,首先需要提供一个字符数据 +// +// 2、字符是否为数字字符: 数字字符的范围 0 - 9 之间都属于数字字符,因此提供的字符只要大于或等于字符0, +// 并且还要下于或等于字符9即可。 +// +// 3、判断完成之后,打印判断的结果。 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int i = sc.nextInt(); + if (i>=0 && i<10){ + System.out.println("是数字字符"); + }else { + System.out.println("不是数字字符"); + } + } +} + +import java.util.Scanner; + +public class java2 { + /* **2、判断一个字符数据是否是字母字符** + + **分析:** + + 1、需要判断一个字符是否是字母字符,首先需要提供一个字符数据 + + 2、字符是否为字母字符: 数字字符的范围 a - z 或者 A - Z 之间都属于字母字符, + 因此提供的字符只要大于或等于a,并且还要下于或等于z 或者 大于或等于A,并且还要下于或等于Z + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + char st = sc.next().charAt(0); + + if( (st >= 'a' && st <= 'z') || (st >= 'A' && st <= 'Z')) + + System.out.println( "是字母"); + + else + + System.out.println("不是字母"); + + } +} + + +import java.util.Scanner; + +public class java3 { +/* + **3、判断指定的年份是否为闰年,请使用键盘录入** + + **分析:** + + 1、闰年的判断公式为:能被4整除,但是不能被100整除 或者 能被400整除 + + 2、首先需要提供一个需要判断的年份,判断完成之后,打印判断的结果。 + +*/ + + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int i = sc.nextInt(); + if ((i / 4 == 0 && i / 400 ==0) || i / 100 != 0 ){ + System.out.println("是闰年"); + }else{ + System.out.println("不是闰年"); + } + } +} + + +import java.util.Scanner; + +public class java4 { + // **4、判断一个数字是否为水仙花数,请使用键盘录入** +// +// 水仙花是指3位数字,表示的是每位上的数字的3次幂相加之后的和值和原数相等,则为水仙花数, +// +// **分析:** +// +// 如:153 ---> 1×1×*1 + 5*×5×*5 + 3×*3×3 = 153; 就是水仙花数 +// +// 1、首先需要提供一个需要判断的3位数字,因此需要一个数值 +// +// 2、判断的过程 +// +// a) 将3位数字的每一位上的数字拆分下来 +// +// b) 计算每位数字的3次幂之和 +// +// C) 用和值 和 原来的数字进行比较 +// +// D) 打印判断的比较结果即可 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入一个数字判断是不是水仙花数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int bai = i / 100 % 10; + if (i > 99 && i < 1000) { + if (ge * ge * ge + si * si * si + bai * bai * bai == i) { + System.out.println("是水仙花数"); + } else { + System.out.println("不是水仙花数"); + } + }else { + System.out.println("您好,水仙花数是三位数"); + } + } +} + + +import java.util.Scanner; + +public class java5 { + /* **5、判断一个5位数字是否为回文数,使用键盘录入** + + 五位数的回文数是指最高位和最低位相等,次高位和次低位相等。如:12321 23732 56665 + + **分析:** + + 1、首先需要提供一个需要判断的5位数字,因此需要一个数值 + + 2、判断的过程 + + a) 将5位数字的万、千、十、个位数拆分出来 + + b) 判断比较万位和个位 、 千位和十位是否相等 + + 3、判断完成之后,打印判断的结果。*/ + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入五位数判断是不是回文数"); + int i = sc.nextInt(); + int ge = i % 10; + int si = i / 10 % 10; + int qian = i / 1000 % 10; + int wan = i / 10000 % 10; + if (i > 9999 && i < 100000) { + if (ge == wan && si == qian) { + System.out.println("是回文数"); + } else { + System.out.println("不是回文数"); + } + }else { + System.out.println("您输入的不是五位数"); + } + } +} + + +import java.util.Scanner; + +public class java6 { +// ## 题目1(训练) +// +// 身高是具有遗传性的,子女的身高和父母的身高有一定的关系。假定,父母和子女的身高遗传关系如下: +// +// ​ 儿子身高(厘米)=(父亲身高+母亲身高) ×1.08÷2 +// +// ​ 女儿身高(厘米)=(父亲身高×0.923+母亲身高) ÷2 +// +// 现有父亲身高177CM,母亲身高165CM。求子女身高分别预计为多少? +// +// ### 训练提示 +// +//1. 已知的父母身高如何用代码体现? +// +// 2. 题目中的公式如何转化为代码? +// +// ### 解题方案 +// +//1. 使用变量的定义和算术运算符完成本题 +// +//### 操作步骤 +// +//1. 定义小数变量代表父亲身高 +// +//2. 定义小数变量代表母亲身高 +// +//3. 通过儿子身高计算方式计算儿子身高 +// +//4. 通过女儿身高计算方式计算女人身高 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入父亲的身高"); + double baba = sc.nextInt(); + System.out.println("请输入母亲的身高"); + double mama = sc.nextInt(); + double erzi = (baba + mama) * 1.08 / 2; + double nver = (baba * 0.923 + mama) / 2; + System.out.println("儿子的身高为:" + erzi); + System.out.println("女儿的身高为:" + nver); + } +} + +public class java7 { +// 红茶妹妹有21元钱,她攒了几天钱之后自己的钱比原来的两倍还多三块。 +// 绿茶妹妹有24元钱,她攒了几天钱之后自己的钱正好是原来的两倍。 +// 那么红茶和绿茶现在的钱一样多,请问对么? + public static void main(String[] args) { + int hocha = 21; + int lvcha = 24; + hocha = hocha * 2 + 3; + lvcha = lvcha * 2; + if (hocha == lvcha){ + System.out.println("钱是一样的,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha ); + }if (hocha > lvcha){ + System.out.println("红茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + }if (hocha < lvcha){ + System.out.println("绿茶妹妹钱多,红茶妹妹的钱" + hocha + ",绿茶妹妹的钱" + lvcha); + } + } +} + + +public class java8 { +// 某小伙想定一份外卖,商家的优惠方式如下: +// 鱼香肉丝单点24元,油炸花生米单点8元,米饭单点3元。订单满30元8折优惠。鱼香肉丝优惠价16元, +// 但是优惠价和折扣不能同时使用。那么这个小伙要点这三样东西,最少要花多少钱? + /*训练提示 + 1. 有两种购买方式,一种是不使用优惠价,另一种是使用优惠价。分别计算花费后,对两种方式的花费作对比。 + 解题方案 + 1. 使用算术运算符、赋值运算符和三元运算符联合完成本题 + 操作步骤 + 1. 使用算术运算符求出不使用优惠时的总价 + 2. 使用三元运算符判断总价是否满足打折条件,并求出折后总价 + 3. 使用算术运算符求出使用优惠价时的总价 + 4. 使用三元运算符判断最终更合算的购买方式和花费*/ + public static void main(String[] args) { + int yuxian = 24; + int youza = 8; + int mifan = 3; + int zojia = yuxian + youza + mifan; + double youhuijia = zojia * 0.8; + System.out.println("使用折扣的总价:" + youhuijia); + int yuxian2 = 16; + int youza2 = 8; + int mifan2 = 3; + int zojia2 = yuxian2 + youza2 + mifan2; + System.out.println("使用鱼香肉丝优惠的总价:" + zojia2); + if (youhuijia < zojia2){ + System.out.println("使用这个:" + youhuijia ); + }else if (youhuijia > zojia2){ + System.out.println("使用这个:" + zojia2); + } + } +} +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" "b/25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" new file mode 100644 index 0000000..81070fc --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230331 Java\345\237\272\347\241\200\350\257\255\346\263\225\346\211\251\345\261\225\350\256\255\347\273\203.md" @@ -0,0 +1,252 @@ +~~~java +package test1; + +public class test1 { +// ## 1、强制类型转换练习 +// +//(1)先声明两个byte类型的变量b1,b2,并分别赋值为10和20,求b1和b2变量的和,并将结果保存在byte类型的变量b3中, +// 最后输出b3变量的值 +// +//(2)先声明两个short类型的变量s1,s2,并分别赋值为1000和2000,求s1和s2变量的和,并将结果保存在short类型的变量s3中, +// 最后输出s3变量的值 +// +//(3)先声明1个char类型的变量c1赋值为'a',再声明一个int类型的变量num赋值为5,求c1和num变量的和, +// 并将结果将结果保存在char类型的变量letter中,最后输出letter变量的值。 +// +// (4)先声明两个int类型的变量i1,i2,并分别赋值5和2,求i1和i2的商,并将结果保存在double类型的变量result中, +// 最后输出result变量的值。如何得到结果2.5呢? + public static void main(String[] args) { + byte b1 = 10; + byte b2 = 20; + byte b3 = (byte)(b1 + b2); + System.out.println("他们的和为:" + b3); + + short s1 = 1000; + short s2 = 2000; + short s3 = (short)(s1 + s2); + System.out.println("他们的和为:" + s3); + + char c1 = 'a'; + int num = 5; + char letter = (char)(c1 + 5); + System.out.println("他们的和为:" + letter); + + int i1 = 5; + int i2 = 2; + double result = ((double) i1 / i2) ; + System.out.println("他们的商为:" + result); + } +} + + +package test1; + +public class test2 { +// 1. 定义两个int类型变量a1和a2,分别赋值10,11,判断变量是否为偶数,拼接输出结果 +// 2. 定义两个int类型变量a3和a4,分别赋值12,13,判断变量是否为奇数,拼接输出结果 + public static void main(String[] args) { + int a1 = 10; + int a2 = 11; + if (a1 % 2 ==0){ + System.out.println("10是偶数?" + true); + } + if (a2 % 2 == 0){ + System.out.println("11是偶数?" + true); + }else + System.out.println("11是偶数?" + false); + + int a3 = 12; + int a4 = 13; + if (a3 % 2 ==0){ + System.out.println("12是偶数?" + true); + } + if (a4 % 2 == 0){ + System.out.println("13是偶数?" + true); + }else + System.out.println("13是偶数?" + false); + } +} + + +package test1; + +public class test3 { +// 1. 定义一个int类型变量hours,赋值为89 +//2. 定义一个int类型变量day,用来保存89小时中天数的结果 +//3. 定义一个int类型变量hour,用来保存89小时中不够一天的剩余小时数的结果 +//4. 输出结果 + + + public static void main(String[] args) { + int hours = 89; + int day = 89 / 24; + int hour = 89 % 24; + System.out.println("为抵抗洪水,战士连续作战89小时:"); + System.out.println("89天是" + day + '天' + hour + "小时"); + } +} + + +package test1; + +public class test4 { +// 1. 定义一个int类型变量week,赋值为2 +//2. 修改week的值,在原值基础上加上100 +//3. 修改week的值,在原值基础上模以7 +//4. 输出结果,在输出结果的时候考虑特殊值,例如周日 + public static void main(String[] args) { + int week = 2; + System.out.print("今天是周"+week+','); + week = week + 100 ; + System.out.print(100+"天以后是"); + week = week % 7; + System.out.print("周"+week); + + } +} + + +package test1; + +public class test5 { +// 1. 定义三个int类型变量,x,y,z,随意赋值整数值 +// 2. 定义一个int类型变量max,先存储x与y中的最大值(使用三元运算符) +// 3. 再次对max赋值,让它等于上面max与z中的最大值(使用三元运算符) +// 4. 输出结果 + public static void main(String[] args) { + int x = (int)(Math.random()*(10000-0)); + int y = (int)(Math.random()*(10000-0)); + int z = (int)(Math.random()*(10000-0)); + int max; + max = x > y ? x : y; + int maxx = max > z ? max : z ; + System.out.println((x)+"," + (y)+"," + (z)+"," + "最大值为:"+ max); + } +} + + +package test1; + +import java.util.Scanner; + +public class test6 { +// 闰年的判断标准是: +// +// 1)可以被4整除,但不可被100整除 +// +// 2)可以被400整除 +// +// +// 1. 定义一个int类型变量year,赋值为今年年份值 +// 2. 定一个一个boolean类型变量,用来保存这个年份是否是闰年的结果 +// 3. 输出结果 + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入年份判断是不是闰年"); + int year = sc.nextInt(); + if ((year % 4 == 0 && year % 100 !=0) || year % 400 ==0 ){ + System.out.println(year + "是闰年"); + }else { + System.out.println(year + "不是闰年"); + } + } +} + + +package test1; + +public class test7 { +// 1. 定义一个double类型变量hua,存储华氏温度80 +//2. 定义一个double类型变量she,存储摄氏温度,根据公式求值 +//3. 输出结果 + public static void main(String[] args) { + double hua = 80; + double she = (hua - 32) / 1.8; + System.out.println("华氏度80.0°F转化摄氏度是" + she); + } +} + + +# 拔高题 + +## 第一题 + +```java +如下代码的计算结果是: +int i = 1; +i *= 0.2; +i++; +System.out.println("i=" + i); + +i=1 +``` + +## 第二题 + +```java +如下代码的运算结果是: +int i = 2; +i *= i++; + +int j = 2; +j *= j+1; + +int k = 2; +k *= ++k; + +System.out.println("i=" + i); +System.out.println("j=" + j); +System.out.println("k=" + k); + + +i=4 +j=6 +k=6 +``` + +## 第三题 + +```java +如下代码的运算结果是: +int a = 3; +int b = 1; +if(a = b){ + System.out.println("Equal"); +}else{ + System.out.println("Not Equal"); +} + +Not Equal +``` + +## 第四题 + +```java +如下代码的运算结果是: +int a = 8, b = 3; +System.out.println(a>>>b); +System.out.println(a>>>b | 2); + + +1 +3 +``` + +## 第五题 + +如何用最有效的的方法计算2乘以8 + +```java +package test1; + +public class test8 { + public static void main(String[] args) { + int num = 2 * 8; + System.out.println(num); + } +} +``` + + +~~~ + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" new file mode 100644 index 0000000..dc4fd7e --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230404 Java\346\225\260\347\273\204\344\275\234\344\270\232.md" @@ -0,0 +1,148 @@ +```java +public class T1 { + public static void main(String[] args) { +// .定义一个含有五个元素的数组,并为每个元素赋值,求数组中所有元素的最小值** +// +//**操作步骤:** +// +// 1.定义5个元素数组 +// 2.可以使用初始化数组的两种方式之一为数组元素赋值 +// 3.遍历数组求数组中的最小值 + + + int[] sum={12,32,52,2,5};//创立数组并赋值 + + int min=sum[0]; + for (int i=1;isum[i]){ + min=sum[i]; + } + } + System.out.println("数组sum的最小值为:"+min); + } +} +``` + + + +```java +package Glass; + +import java.util.Arrays; + +public class T2 { + public static void main(String[] args) { +// 需求:求出数组中索引与索引对应的元素都是奇数的元素** +// +//**分析:** +// +// 1、遍历数组 +// 2、判断索引是否是奇数(索引 % 2 != 0) +// 3、判断索引对应的元素是否是奇数(arr[索引] % 2 != 0) +// 4、满足条件输出结果 + + int[] sum={12,4,32,6,32,11}; + System.out.println(Arrays.toString(sum)); + + for (int i=0;i= 0; + } + +// 包含一个方法int approximateNumberCount()方法, +// 用于返回value属性值的约数个数。在[1, value]之间可以把value整除的整数都是value的约数。 + public int approximateNumberCount(){ + int count = 0; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + count++; + } + } + return count; + } + +// 包含一个方法boolean isPrimeNumber()方法, +// 用于判断value属性值是否是素数。如果value值在[1, value]之间只有1和value本身两个约数, +// 并且value是大于1的自然数,那么value就是素数。 + public boolean isPrimeNumber(){ + int count = 0; + boolean flag = false; + for (int i = 1; i < value ; i++) { + count++; + if (value % i == 0 && value % 1 == 0 && value > 1){ + if (count >= 3){ + flag = false; + }else { + flag = true; + } + } + } + return flag; + } +// 包含一个方法int[] getAllPrimeNumber()方法, +// 用于返回value属性值的所有约数。返回[1, value]之间可以把value整除的所有整数。 + public int[] getAllPrimeNumber(){ + int[] arr = new int[approximateNumberCount()]; + int index = arr[0]; + for (int i = 1; i <= value; i++) { + if (value % i == 0){ + arr[index++]=i; + } + } + return arr; + } + + @Override + public String toString() { + return "MyInt{" + + "\nvalue=" + value + + "\n是不是自然数:" + isNatural() + + "\n约数个数:" + approximateNumberCount() + + "\n是不是素数:" + isPrimeNumber() + + "\n所有的约数:" + Arrays.toString(getAllPrimeNumber()) + + "\n}"; + } +} + +package T6; + +public class TestMyLnt { + public static void main(String[] args) { + MyLnt lnt = new MyLnt(20); + System.out.println(lnt); + } +} + +``` + +# 第七题 + +```java +package T7; + +public class TestYue { + public static void main(String[] args) { + Yue y = new Yue(); + int min = y.min(44,33,88,342,454,234,5,2); + System.out.println("最小值为:" + min); + int max = y.maxApproximate(4,8,16,20,24); + System.out.println("最大公约数为:" + max); + } +} + + +package T7; + +public class Yue { +// 返回n个整数中的最小值 + int min(int... nums){ + int min = nums[0]; + for (int i = 0; i < nums.length; i++) { + if (min > nums[i]){ + min = nums[i]; + } + } + return min; + } + + public int maxApproximate(int... nums){ + int max=min(nums); + for (int i = max; i >= 1; i--) { + boolean flag=true; + for (int j = 0; j < nums.length; j++) { + if (nums[j]%i!=0){ + flag=false; + break; + } + } + if (flag==true){ + return i; + } + } + return 1; + } +} + +``` + +# 第八题 + +```java +package T8; + +public class ArraysTools { + public String toString(int[]arr) { + String str = "["; + for (int i = 0; i < arr.length; i++) { + int i1 = arr[i]; + str += i1; + if (i < arr.length - 1) { + str += ","; + } + } + str += "]"; + return str; + } + int[] grow(int[] arr){ + int shu = arr.length; + int[] newArr = new int[2 * shu]; + for (int i = 0; i < arr.length; i++) { + newArr[i] = arr[i]; + } + return newArr; + } +} + +package T8; + +public class Test { + public static void main(String[] args) { + int[]old={11,22,33,44}; + ArraysTools arraysTools = new ArraysTools(); + System.out.println(arraysTools.toString(old)); + int[]newArr= arraysTools.grow(old); + System.out.println(arraysTools.toString(newArr)); + } +} + +``` + +# 第九题 + +```java +package T9; + +public class MathTools { + int compare(int a, int b){ + if (a > b){ + return a; + }else if (a < b){ + return -a; + }else { + return 0; + } + } +/* +* int compare(double a, double b):比较两个小数大小关系,如果第一个小数比第二个小数大, +* 则返回正整数,如果第一个小数比第二个小数小,则返回负整数,如果两个小数相等则返回0; +* */ + int compare(double a, double b){ + if (a > b){ + return (int)a; + }else if (a < b){ + return (int)-a; + }else { + return 0; + } + } + +// int compare(char a, char b):比较两个字符大小关系,如果第一个字符比第二个字符编码值大, +// 则返回正整数,如果第一个字符比第二个字符编码值小,则返回负整数,如果两个字符相等则返回0; + int compare(char a, char b){ + if (a > b){ + return (char)a; + }else if (a < b){ + return (char)-a; + }else { + return 0; + } + } +} + + +package T9; + +public class Test { + public static void main(String[] args) { + MathTools ma = new MathTools(); + int a = ma.compare(3,5); + int b = ma.compare('2','3'); + int c = ma.compare(33.5,22.4); + System.out.println(a); + System.out.println(b); + System.out.println(c); + } +} + +``` + +# 第十题 + +```java +package T10; + +public class ArraysTools { + public void sort(int[] arr){ + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]){ + int temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public void sort(double[] arr){ + for (int i= 1; i arr[j+1]){ + double temp=arr[j]; + arr[j]=arr[j+1]; + arr[j+1]=temp; + } + } + } + } + + public void sort(char[] arr) { + for (int i = 1; i < arr.length; i++) { + for (int j = 0; j < arr.length - i; j++) { + if (arr[j] > arr[j + 1]) { + char temp = arr[j]; + arr[j] = arr[j + 1]; + arr[j + 1] = temp; + } + } + } + } + public String toString(int[] arr){ + String re="["; + for (int i = 0; i < arr.length; i++) { + if (i我***你 + +##### String[] split(String regex) :根据传入的字符串作为规则进行切割,将切割后的内容存入字符串数组中,并将字符串数组返回 + +String stu= "张三,23" String[] stuArr=stu.split(","); //遇到","进行切割 //调用String[] split(String regex)方法将字符串切割 System.out.println(Arrays.toString(stuArr)); //输出结果-----> [张三,23] + +## String方法小结 + +| **方法名** | **说明** | +| ----------------------------------------------------- | -------------------------------- | +| public boolean equals(Object anObject) | 比较字符串的内容,严格区分大小写 | +| public boolean equalsIgnoreCase(String anotherString) | 比较字符串的内容,忽略大小写 | +| public int length() | 返回此字符串的长度 | + +| public char charAt(int index) | 返回指定索引处的 char 值 | +| ----------------------------- | ---------------------------- | +| public char[] toCharArray() | 将字符串拆分为字符数组后返回 | + +| **方法名** | **说明** | +| ------------------------------------------------------------ | ------------------------------------------------------------ | +| public String substring(int beginIndex, int endIndex) | 根据开始和结束索引进行截取,得到新的字符串(包含头,不包含尾) | +| public String substring(int beginIndex) | 从传入的索引处截取,截取到末尾,得到新的字符串 | +| public String replace(CharSequence target, CharSequence replacement) | 使用新值,将字符串中的旧值替换,得到新的字符串 | + +| public String[] split(String regex) | 根据传入的规则切割字符串,得到字符串数组 | +| ----------------------------------- | ---------------------------------------- | +| | | + +## StringBuilder + +StringBuilder 是一个可变的字符串类,我们可以把它看成是一个容器 + +作用:提高字符串的操作效率 + +### StringBuilder 构造方法 + +| **方法名** | **说明** | +| -------------------------------- | ------------------------------------------ | +| public StringBuilder() | 创建一个空白可变字符串对象,不含有任何内容 | +| public StringBuilder(String str) | 根据字符串的内容,来创建可变字符串对象 | + +### StringBuilder 的常用方法 + +| **方法名** | **说明** | +| -------------------------------------- | ------------------------ | +| public StringBuilder append (任意类型) | 添加数据,并返回对象本身 | +| public StringBuilder reverse() | 返回相反的字符序列 | + +| public int length() | 返回长度 ( 字符出现的个数) | +| ------------------- | -------------------------- | +| | | + +| public String toString() | 通过toString()就可以实现把StringBuilder转换为String | +| ------------------------ | --------------------------------------------------- | +| | | + +## StringBuilder和String的区别 + +**String** :内容是不可变的 + +**StringBuilder**:内容是可变的 + +## StringBuilder 和 String 相互转化 + +### StringBuilder 转换为 String + +public String toString():通过 toString() 就可以实现把 StringBuilder 转换为 String + +### String 转换为 StringBuilder + +public StringBuilder(String s):通过构造方法就可以实现把 String 转换为 StringBuilder + +# 作业 + +```java +import java.util.Scanner; + +public class Test1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + String password = "12345"; + System.out.println("请输入密码登陆"); + int count = 0; + for (int i = 0; i < 3; i++) { + String st = sc.next(); + count++; + if (st.equals(password)){ + System.out.println("密码正确,登陆成功"); + break; + }else { + System.out.println("密码错误"); + System.out.println("您还有" + (3 - count) + "次"); + } + } + } +} + + +import java.util.Scanner; + +public class Test2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入字符串遍历数组"); + String st = sc.next(); + for (int i = 0; i = 'A' && st.charAt(i) <= 'Z'){ + da++; + } + if (st.charAt(i) >= 'a' && st.charAt(i) <= 'z'){ + xiao++; + } + if (st.charAt(i) >= '0' && st.charAt(i) <= '9'){ + shu++; + } + } + System.out.println("大写出现的次数:" + da); + System.out.println("小写出现的次数:" + xiao); + System.out.println("数字出现的次数:" + shu); + } +} + + +import javax.swing.*; +import java.util.Scanner; + +public class Test4 { + public static void main(String[] args) { + System.out.println("请输入电话号码"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.substring(0,3); + String s2 = st.substring(7); + System.out.println("转换后的电话号码为:" + s1 + "****" + s2); + } +} + + +import java.util.Scanner; + +public class Test5 { + public static void main(String[] args) { + System.out.println("请输入脏话"); + Scanner sc = new Scanner(System.in); + String st = sc.next(); + String s1 = st.replace("TMD","不可以说脏话"); + System.out.println(s1); + } +} + + +public class Student { + private String name; + private int age; + + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } +} + + +import java.util.Arrays; +import java.util.Scanner; + +public class TestStudent6 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + Student st = new Student(); + System.out.println("请输入名字"); + st.setName(sc.next()); + System.out.println("请输入年龄"); + st.setAge(sc.nextInt()); + String st2 = st.getName() +","+ st.getAge(); + String[] arr = st2.split(","); + System.out.println(Arrays.toString(arr)); + + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" new file mode 100644 index 0000000..db5bfa5 --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230418 Java\351\233\206\345\220\210\344\275\234\344\270\232.md" @@ -0,0 +1,205 @@ +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三",18)); + list.add(new Student("李四",19)); + list.add(new Student("王五",20)); + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java +//创建一个存储学生对象的集合,存储3个学生对象,使用程序实现在控制台遍历该集合 +//学生的姓名和年龄来自于键盘录入 +public class Student { + private String name; + private int age; + + public Student(String name, int age) { + this.name = name; + this.age = age; + } + + public Student(String name) { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + +} +import java.security.AllPermission; +import java.util.ArrayList; +import java.util.Scanner; + +public class TestStudent2 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + int count = 0; + ArrayList list = new ArrayList<>(); + while (true) { + count++; + System.out.println("请输入第" + count + "名学生的姓名和年龄"); + list.add(new Student(sc.next(), sc.nextInt())); + if (count == 3){ + break; + } + } + for (int i = 0; i < list.size(); i++) { + Student stu = list.get(i); + System.out.print(stu.getName() + " "); + System.out.println(stu.getAge()); + } + } +} + +``` + +```java + +import java.util.ArrayList; +import java.util.Iterator; + +public class T2 { + public static void main(String[] args) { +// 需求:创建一个存储String的集合,内部存储 +// (test,张三,李四,test,test)字符串 +// 删除所有的test字符串,删除后,将集合剩余元素打印在控制台 + ArrayList list = new ArrayList<>(); + list.add("test"); + list.add("张三"); + list.add("李四"); + list.add("test"); + list.add("test"); +// Iterator it = list.iterator(); +// while(it.hasNext()){ +// String str = it.next(); +// if (str.contains("test")){ +// it.remove(); +// } +// } + list.removeIf(s -> s.contains("test")); + System.out.println(list); + } +} + + +``` + +```java +//定义一个方法,方法接收一个集合对象(泛型为Student),方法内部将年龄低于18的学生对象找出 +//并存入新集合对象,方法返回新集合。 +public class Student { + private String name; + private int age; + public Student(String name,int age){ + this.name = name; + this.age = age; + } + + public Student(){ + + } + + public void setName(String name){ + this.name = name; + } + public void setAge(int age){ + this.age = age; + } + + public String getName(){ + return this.name = name; + } + + public int getAge(){ + return this.age = age; + } + + + public String toString(){ + return "姓名:" + getName() + " " + "年龄:" + getAge(); + } +} + +import java.util.ArrayList; + +public class TestStudent { + public static void main(String[] args) { + ArrayList list = new ArrayList<>(); + list.add(new Student("张三", 19)); + list.add(new Student("李四", 20)); + list.add(new Student("王五", 10)); + list.add(new Student("赵六", 15)); + chaXun(new ArrayList<>(list)); + } + public static ArrayList chaXun(ArrayList list + ){ + ArrayList list1 = new ArrayList<>(); + for (int i = 0; i < list.size(); i++) { + Student st = list.get(i); + if (st.getAge()<18){ + list1.add(list.get(i)); + } + } + System.out.println(list1); + return list1; + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" new file mode 100644 index 0000000..bc280be --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230421 java\345\255\246\347\224\237\347\256\241\347\220\206\347\263\273\347\273\237.md" @@ -0,0 +1,254 @@ +```java +package Light; + +public class Student { + private String StuID; + private String name; + private int age; + private String sex; + + + + public String getStuID() { + return StuID; + } + + public void setStuID(String stuID) { + StuID = stuID; + } + + public String getSex() { + return sex; + } + + public void setSex(String sex) { + this.sex = sex; + } + + public int getAge() { + return age; + } + + public void setAge(int age) { + this.age = age; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public Student(){} + public Student(String StuID,String name,int age,String sex){ + this.StuID=StuID; + this.name=name; + this.age=age; + this.sex=sex; + } + + @Override + public String toString() { + return "Student{" + + "StuID='" + StuID + '\'' + + ", name='" + name + '\'' + + ", age=" + age + + ", sex='" + sex + '\'' + + '}'; + } +} +``` + +```java +package Light; + +import java.util.ArrayList; +import java.util.Scanner; + +public class T { + public static void main(String[] args) { + ArrayList list=new ArrayList<>(); + Scanner sc=new Scanner(System.in); + + sum: + for (int i=0;;){ + System.out.println("============学生系统============"); + System.out.println("请输入编号:"+ + "\n\t1 添加学生信息"+ + "\n\t2 修改学生信息"+ + "\n\t3 删除学生信息"+ + "\n\t4 查看学生信息"+ + "\n\t5 退出学生系统"); + System.out.println(); + System.out.print("请输入编号:"); + String sum=sc.next();; + switch (sum){ + case "1": +// System.out.println("添加"); + insertStudent(list,sc); + break; + case"2": + updateStudent(list,sc); + break; + case"3": + deleteStudent(list,sc); + break; + case"4": + lookStudent(list,sc); + break; + case"5": + System.out.println("成功退出学生系统"); + break sum; + default: + System.out.println("输入编号错误,请重新输入!!!"); + System.out.println(); + } + } + } + + + /** + * 添加 + * @param list + * @param sc + */ + public static void insertStudent(ArrayList list, Scanner sc){ + System.out.println(); + System.out.println("===*请输入需要添加学生信息*==="); + System.out.println("请输入学号"); + String stuId=sc.next(); + + if (!list.isEmpty()){ + for (int i = 0; i< list.size(); i++){ + Student stu= list.get(i); + if (stuId.equals(stu.getStuID())){ + System.out.println("学号已经存在"); + break; + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + } + }else { + System.out.println("请输入姓名"); + String stuName=sc.next(); + System.out.println("请输入年龄"); + int stuAge=sc.nextInt(); + System.out.println("请输入性别"); + String stuSex=sc.next(); + list.add(new Student(stuId,stuName,stuAge,stuSex)); + } + System.out.println(); + } + + + /** + * 修改 + * @param list + * @param sc + */ + + public static void updateStudent(ArrayList list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来修改!!!"); + }else { + System.out.println("===修改学生信息==="); + System.out.println("请输入需要修改的学生学号"); + String stuId=sc.next(); + for (int i=0;i list,Scanner sc){ + System.out.println(); + if (list.isEmpty()){ + System.out.println("没有学生信息,请添加学生信息后再来删除!!!"); + }else { + System.out.println("==*删除学生信息*=="); + System.out.println("请输入需要删除的学生学号"); + String stuId=sc.next(); + + for (int i=0;i list,Scanner sc){ + System.out.println(); + + if (list.isEmpty()) { + System.out.println("没有学生信息,请添加学生信息后再来查看!!!"); + }else { + System.out.println("==*查看学生信息*=="); + System.out.println("请输入需要查看学生的学生学号"); + String stuId=sc.next(); + + for (int i=0;i= b) ? a : b; + } + + public static int min(int a,int b){ + return (a <= b) ? b : a; + } +} + +``` + diff --git "a/25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" "b/25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" new file mode 100644 index 0000000..ac1858b --- /dev/null +++ "b/25 \346\235\216\346\226\207\346\235\260/20230428 Java\346\216\245\345\217\243\344\275\234\344\270\232.md" @@ -0,0 +1,194 @@ +```java +//父类是:战斗机 + +//1. 属性:名称,颜色,载弹量,打击半径 +//2. 功能:起飞,巡航,降落,雷达扫射,开火 +import java.util.ArrayList; + +public class Zdj { + private String name; + private String color; + private String dang; + private String ban; + + public Zdj(String name, String color, String dang, String ban) { + this.name = name; + this.color = color; + this.dang = dang; + this.ban = ban; + } + + public Zdj() { + + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getColor() { + return color; + } + + public void setColor(String color) { + this.color = color; + } + + public String getDang() { + return dang; + } + + public void setDang(String dang) { + this.dang = dang; + } + + public String getBan() { + return ban; + } + + public void setBan(String ban) { + this.ban = ban; + } + + public void fei(){ + System.out.println(color + "的" + name + "正在起飞"); + } + + public void xun(){ + System.out.println(color + "的" + name + "正在巡逻"); + } + + public void jiang(){ + System.out.println(color + "的" + name +"正在降落"); + } + + public void sao(){ + System.out.println(color + "的" + name +"正在扫射"); + } + + public void kai(){ + System.out.println(color + "的" + name +"遭受敌人的攻击,正在开火"); + } + + +} + +//接口:对空,对地 +public interface kong{ + void lan(); + void fangKong(); +} + +public interface di { + void duiDi(); + void saoShe(); +} +//子类: + +//1. 空空战斗机(只对空) +//2. 空地战斗机(能对空能对地) +//3. 轰炸机 (只能对地) +public class kong01 extends Zdj implements kong{ + @Override + public void lan() { + System.out.println(super.getName() + "正在拦截导弹的追踪"); + } + + @Override + public void fangKong() { + System.out.println(super.getName() + "发射防空导弹"); + } +} + +public class D1 extends Zdj implements di{ + @Override + public void duiDi() { + System.out.println(getName() + "正在发射对地导弹"); + } + + @Override + public void saoShe() { + System.out.println(getName() + "正在对地面进行扫射轰炸"); + } +} + +public class KongDi extends Zdj implements di,kong{ + @Override + public void duiDi() { + System.out.println(getName() + "正在对地面进行猛烈的轰炸"); + } + + @Override + public void saoShe() { + System.out.println(getName() + "正在对地面进行扫射"); + } + + @Override + public void lan() { + System.out.println(getName() + "正在阻止空中的战机"); + } + + @Override + public void fangKong() { + System.out.println(getName() + "正在拦截空中的导弹"); + } +} + + +//测试类: + +//1. 生成每一种战斗并属性赋值,并调用相关功能 + +public class TestKongDi { + public static void main(String[] args) { + KongDi k = new KongDi(); + k.setName("陆空战斗机"); + k.setColor("蓝色"); + k.setDang("20kg"); + k.setBan("100公里"); + k.lan(); + k.fangKong(); + k.fei(); + k.xun(); + k.kai(); + k.sao(); + k.saoShe(); + k.duiDi(); + } +} + + +public class TestDi { + public static void main(String[] args) { + D1 d = new D1(); + d.setName("对地战斗机"); + d.setColor("黑色"); + d.setBan("6公里"); + d.setDang("10kg"); + d.duiDi(); + d.saoShe(); + } +} + +public class Test { + public static void main(String[] args) { + kong01 k = new kong01(); + k.setName("空空战斗机"); + k.setColor("红色"); + k.setDang("5kg"); + k.setBan("5公里"); + k.lan(); + k.fangKong(); + k.fei(); + k.xun(); + k.kai(); + k.sao(); + } +} + +``` + -- Gitee