diff --git "a/13\345\217\267\350\224\241\345\230\211\344\271\220/2022121 java\345\205\263\347\263\273\350\277\220\347\256\227\347\254\246\345\222\214\344\275\234\344\270\232.md" "b/13\345\217\267\350\224\241\345\230\211\344\271\220/2022121 java\345\205\263\347\263\273\350\277\220\347\256\227\347\254\246\345\222\214\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..2bddcc24c14ab7d9ee98d4df4eb24cfc2e7a4f91 --- /dev/null +++ "b/13\345\217\267\350\224\241\345\230\211\344\271\220/2022121 java\345\205\263\347\263\273\350\277\220\347\256\227\347\254\246\345\222\214\344\275\234\344\270\232.md" @@ -0,0 +1,419 @@ +# 1.关系运算符(应用) + +运算符有6种关系,分别为小于、小于等于、大于、等于、大于等于、不等于 + +| 符号 | 说明 | +| ---- | ------------------------------------------------------- | +| == | a==b,判断a和b的值是否相等,成立为true,不成立为false | +| != | a!=b,判断a和b的值是否不相等,成立为true,不成立为false | +| > | a>b,判断a是否大于b,成立为true,不成立为false | +| >= | a>=b,判断a是否大于等于b,成立为true,不成立为false | +| < | a j) & (i > k)); //false & false,输出false +System.out.println((i < j) & (i > k)); //true & false,输出false +System.out.println((i > j) & (i < k)); //false & true,输出false +System.out.println((i < j) & (i < k)); //true & true,输出true +System.out.println("--------"); + +//| “或”,或者的关系,只要表达式中有一个值为true,结果即为true +System.out.println((i > j) | (i > k)); //false | false,输出false +System.out.println((i < j) | (i > k)); //true | false,输出true +System.out.println((i > j) | (i < k)); //false | true,输出true +System.out.println((i < j) | (i < k)); //true | true,输出true +System.out.println("--------"); + +//^ “异或”,相同为false,不同为true +System.out.println((i > j) ^ (i > k)); //false ^ false,输出false +System.out.println((i < j) ^ (i > k)); //true ^ false,输出true +System.out.println((i > j) ^ (i < k)); //false ^ true,输出true +System.out.println((i < j) ^ (i < k)); //true ^ true,输出false +System.out.println("--------"); + +//! “非”,取反 +System.out.println((i > j)); //false +System.out.println(!(i > j)); //!false,,输出true +``` + + + +# 3.短路逻辑运算符 + +| 符号 | 作用 | 说明 | +| ---- | ------ | ---------------------------- | +| && | 短路与 | 作用和&相同,但是有短路效果 | +| \|\| | 短路或 | 作用和\|相同,但是有短路效果 | + +在逻辑与运算中,只要有一个表达式的值为false,那么结果就可以判定为false了,没有必要将所有表达式的值都计算出来,短路与操作就有这样的效果,可以提高效率。同理在逻辑或运算中,一旦发现值为true,右边的表达式将不再参与运算。 + +- 逻辑与&,无论左边真假,右边都要执行。 + +- 短路与&&,如果左边为真,右边执行;如果左边为假,右边不执行。 + +- 逻辑或|,无论左边真假,右边都要执行。 + +- 短路或||,如果左边为假,右边执行;如果左边为真,右边不执行 + +```java +int x = 3; +int y = 4; +System.out.println((x++ > 4) & (y++ > 5)); // 两个表达都会运算 +System.out.println(x); // 4 +System.out.println(y); // 5 + +System.out.println((x++ > 4) && (y++ > 5)); // 左边已经可以确定结果为false,右边不参与运算 +System.out.println(x); // 4 +System.out.println(y); // 4 +``` + +# 4. 三元运算符案例(重点 有点不会) + +三元运算符语法格式: + +```java +关系表达式 ? 表达式1 : 表达式2; +``` + +解释:问号前面的位置是判断的条件,判断结果为boolean型,为true时调用表达式1,为false时调用表达式2。其逻辑为:如果条件表达式成立或者满足则执行表达式1,否则执行第二个 + +举例: + +```java +int a = 10; +int b = 20; +int c = a > b ? a : b; // 判断 a>b 是否为真,如果为真取a的值,如果为假,取b的值 +``` + +需求: + +​ 一座寺庙里住着三个和尚,已知他们的身高分别为150cm、210cm、165cm,请用程序实现获取这三个和尚的最高身高。 + +```java +public class OperatorTest02 { + public static void main(String[] args) { + //1:定义三个变量用于保存和尚的身高,单位为cm,这里仅仅体现数值即可。 + int height1 = 150; + int height2 = 210; + int height3 = 165; + //2:用三元运算符获取前两个和尚的较高身高值,并用临时身高变量保存起来。 + int tempHeight = height1 > height2 ? height1 : height2; + //3:用三元运算符获取临时身高值和第三个和尚身高较高值,并用最大身高变量保存。 + int maxHeight = tempHeight > height3 ? tempHeight : height3; + //4:输出结果 + System.out.println("maxHeight:" + maxHeight); + } +} +``` + +# 5.if 语句 + +```java +格式: +if (关系表达式) { + 语句体; +} +``` + +示例 + +public class IfDemo { + public static void main(String[] args) { + System.out.println("开始"); + +```java + // 如果年龄大于18岁, 就可以上网吧 + int age = 17; + + if(age >= 18){ + // int a = 10; + System.out.println("可以上网吧"); + } + + System.out.println("结束"); +} +``` + + +```java +格式: +if (关系表达式) { + 语句体1; +} else { + 语句体2; +} +``` + +示例:奇偶数 + +```java +public class Demo2If { + public static void main(String[] args) { + // 程序判断一个数, 是奇数还是偶数 + int num = 9; + + if(num % 2 == 0){ + System.out.println("偶数"); + }else{ + System.out.println("奇数"); + } + } +} +``` + + + +```java +格式: +if (关系表达式1) { + 语句体1; +} else if (关系表达式2) { + 语句体2; +} +… +else { + 语句体n+1; +} +``` + + + +示例: + +​ 定义一个在0~100之间的变量a, 90~100优秀,80~89良好,70~79中等,60~69及格,0~59请努力加油! + +````java +public class Demo3If { + public static void main(String[] args){ + int score = 65; + if(score >= 90 && score <= 100){ + System.out.println("优秀"); + }else if (score >= 80 && score <= 89){ + System.out.println("良好"); + }else if (score >= 70 && score <= 79){ + System.out.println("中等"); + }else if (score >= 60 && score <= 69){ + System.out.println("及格"); + }else if (score >= 0 && score <= 59){ + System.out.println("请努力加油"); + }else{ + System.out.println("成绩有误!"); + } + } +} +```` + + + + + +#### if语句格式3案例(应用) + +需求:小明快要期末考试了,小明爸爸对他说,会根据他不同的考试成绩,送他不同的礼物,假如你可以控制小明的得分,请用程序实现小明到底该获得什么样的礼物,并在控制台输出。 + +分析: + +​ ①小明的考试成绩未知,可以使用键盘录入的方式获取值 + +​ ②由于奖励种类较多,属于多种判断,采用if...else...if格式实现 + +​ ③为每种判断设置对应的条件 + +​ ④为每种判断设置对应的奖励 + + + +```java +import java.util.Scanner; +public class IfTest02 { + public static void main(String[] args){ + // 1. 使用Scanner录入考试成绩 + Scanner sc = new Scanner(System.in); + System.out.println("请输入您的成绩:"); + int score = sc.nextInt(); + // 2. 判断成绩是否在合法范围内 0~100 + if(score >=0 && score <= 100){ + // 合法成绩 + // 3. 在合法的语句块中判断成绩范围符合哪一个奖励 + if(score >= 95 && score <= 100){ + System.out.println("自行车一辆"); + }else if(score >= 90 && score <= 94){ + System.out.println("游乐场一次"); + }else if(score >= 80 && score <= 89){ + System.out.println("变形金刚一个"); + }else { + System.out.println("挨顿揍, 这座城市又多了一个伤心的人~"); + } + }else{ + // 非法的话, 给出错误提示 + System.out.println("您的成绩输入有误!"); + } + } +} +``` + + + + + +# 作业 + +```java +public class Ammm3 { + public static void main(String[] args) { + //两个值求最大 + int num1=78; + int num2=56; + int max=(num1>num2)?num1:num2; + System.out.println("max="+max); + + //三个值求最大 + int x=89; + int y=55; + int z=23; + int a=(x>=y)?x:y; + int b=(a>=z)?a:z; + System.out.println("max="+b); + + + } +} + +``` + + + +```java +public class Bmmm { + public static void main(String[] args) { + double father =177; + double mother =165; + double nh = ((father+mother)*1.08/2); + double nv =((father*0.923+mother)/2); + System.out.println("男孩身高"+nh+"厘米"); + System.out.println("女孩身高"+nv+"厘米"); + } + + +} +``` + + + +```java +public class Cmmm { + public static void main(String[] args) { + //定义红茶妹妹原来的钱 + int red = 21; + //定义绿茶妹妹原来的钱 + int green = 24; + //求红茶妹妹现有的钱 + red = red * 2 + 3; + //求绿茶妹妹现有的钱 + green *= 2; + //判断并输出两个人的钱是否相等 + System.out.println(red == green); + + } +} +``` + + + +```java +import java.util.Scanner; + +public class Fmmm { + public static void main(String[] args) { + System.out.println("请输入鱼香肉丝价格"); + double a = new Scanner(System.in).nextDouble(); + System.out.println("请输入油炸花生米价格"); + double b = new Scanner(System.in).nextDouble(); + System.out.println("请输入米饭价格"); + double c = new Scanner(System.in).nextDouble(); + best(a,b,c); + } + static void best(double a,double b,double c) { + //优惠一 + double zj = a + b + c; + double one = (zj >= 30 ? zj * 0.8 : zj); + //优惠二 + a = 16.00; + double two = a + b + c; + System.out.println("第一种优惠核算" + one + "第二种优惠核算" + two); + if (one > two) { + System.out.println("第二种优惠划算"); + } else if (one == two) { + System.out.println("两种优惠花一样多的钱"); + } else { + System.out.println("第一种优惠划算"); + + } + } +} +``` + + + +```java +import java.util.Scanner; + +public class Emmm { + public static void main(String[] args) { + Scanner in = new Scanner(System.in); + int x , y; + boolean result; + System.out.println("请输入第一个数字:"); + x = in.nextInt(); + System.out.println("请输入第二个数字:"); + y = in.nextInt(); + result = ((x + y) % 3 == 0) || x == 3 || y == 3; + System.out.println(result); + + } +} + +``` + +```java +import java.util.Scanner; + +public class Gmmm { + public static void main(String[] args) { + Scanner input = new Scanner(System.in); + System.out.println("中国队进球数"); + int a = input.nextInt(); + System.out.println("日本队进球数"); + int b= input.nextInt(); + + if(a>b){ + System.out.println("中国队赢"); + } + System.out.println("日本队赢"); + } +} + +``` + diff --git "a/13\345\217\267\350\224\241\345\230\211\344\271\220/\347\254\224\350\256\260.md" "b/13\345\217\267\350\224\241\345\230\211\344\271\220/\347\254\224\350\256\260.md" deleted file mode 100644 index b0fe1d2727385ce43a74fe755d16a50643e4b8bc..0000000000000000000000000000000000000000 --- "a/13\345\217\267\350\224\241\345\230\211\344\271\220/\347\254\224\350\256\260.md" +++ /dev/null @@ -1,42 +0,0 @@ -数值类型分为 - -整数(byte,short,int,long) - -浮点数(float,double) - -字符(char) - - - -非数值型 布尔(boolean) - - - -默认值 - -整数:int - -浮点型:double - -字符串 string - - - -变量的定义格式 - -# 格式:数据类型 变量名=变量值 - -示范;int price =998 - -#变量使用 1.输出使用 2.修改值 - -1.什么是变量 - -就是内存中的存储空间 - -空间中存储的数据是可以发生改变 - - - - -