diff --git "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/10.BitOperationExercise.java" "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/10.BitOperationExercise.java" index b58c2c4..9e7ac1c 100644 --- "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/10.BitOperationExercise.java" +++ "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/10.BitOperationExercise.java" @@ -1,4 +1,4 @@ -class BitOperationExercise{ +class BitOperationExercise { public static void main(String[] args) { int num1 = 10; int num2 = 20; @@ -24,5 +24,14 @@ public static void main(String[] args) { System.out.println(num1); System.out.println(num2); + //第2题 + int i1 = 60; + int i2 = i1 & 15; + String j = (i2 > 9) ? (char) (i2 - 10 + 'A') + "" : i2 + ""; + int temp = i1 >>> 4; + i2 = temp & 15; + String k = (i2 > 9) ? (char) (i2 - 10 + 'A') + "" : i2 + ""; + System.out.println(k + "" + j); + } } \ No newline at end of file diff --git "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/10.\344\275\215\350\277\220\347\256\227\347\273\203\344\271\240.md" "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/10.\344\275\215\350\277\220\347\256\227\347\273\203\344\271\240.md" index 3c060d9..89b7109 100644 --- "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/10.\344\275\215\350\277\220\347\256\227\347\273\203\344\271\240.md" +++ "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/10.\344\275\215\350\277\220\347\256\227\347\273\203\344\271\240.md" @@ -31,4 +31,25 @@ class BitOperationExercise{ } } -``` \ No newline at end of file +``` + +## 10.2 实现60的二进制到16进制的转换 + +利用无符号位右移实现: >>> + +```java +class BitOperationExercise { + public static void main(String[] args) { + + //第2题 + int i1 = 60; + int i2 = i1 & 15; + String j = (i2 > 9) ? (char) (i2 - 10 + 'A') + "" : i2 + ""; + int temp = i1 >>> 4; + i2 = temp & 15; + String k = (i2 > 9) ? (char) (i2 - 10 + 'A') + "" : i2 + ""; + System.out.println(k + "" + j); + + } +} +``` diff --git "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/13.OperationExercise.java" "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/13.OperationExercise.java" new file mode 100644 index 0000000..ae92818 --- /dev/null +++ "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/13.OperationExercise.java" @@ -0,0 +1,24 @@ +class OperationExercise { + public static void main(String[] args) { + //第3题 + int x = 10, y = 20, z = 30; + int max = ((x > y ? x : y) > z) ? (x > y ? x : y) : z; + System.out.println("x y z中最大值是: " + max); + + //第4题 + double m = 11.0, n = 12.0; + if (m > 10.0 && n < 20.0) { + System.out.println("m+n: " + (m + n)); + } else { + System.out.println("m: " + x + ",n: " + y); + } + + //第5题 + int i = 10; + int j = 20; + i = i ^ j; + j = i ^ j; + i = i ^ j; + System.out.println("i: " + i + ",j: " + j); + } +} \ No newline at end of file diff --git "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/13.\345\244\215\344\271\240\351\242\230.md" "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/13.\345\244\215\344\271\240\351\242\230.md" new file mode 100644 index 0000000..7d5c194 --- /dev/null +++ "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/13.\345\244\215\344\271\240\351\242\230.md" @@ -0,0 +1,65 @@ +# 13. 复习题 + +1.& 和 && 的异同 + +2.程序输出 + +```java +class IfElse{ + public static void main(String[] args) { + boolean x = true; + boolean y = false; + short z = 40; + if ((z++==40) && (y=true)){ + z++; + } + if((x=false) || ++z==43){ + z++; + } + System.out.println("z = " + z); + } +} +``` + +3.定义三个int型变量并赋值,使用三元运算符或者if-else获取这三个数中的较大数的实现。 + +```java +class OperationExercise { + public static void main(String[] args) { + //第3题 + int x = 10, y = 20, z = 30; + int max = ((x > y ? x : y) > z) ? (x > y ? x : y) : z; + System.out.println("x y z中最大值是: " + max); + } +} +``` + +4.编写程序:声明2个double型变量并赋值。判断第一个数大于10.0,且第2个数小于200,打印两数之和,否则,打印两数的乘积。 +```java +class OperationExercise { + public static void main(String[] args) { + //第4题 + double m = 11.0, n = 12.0; + if (m > 10.0 && n < 20.0) { + System.out.println("m+n: " + (m + n)); + } else { + System.out.println("m: " + x + ",n: " + y); + } + } +} +``` + +5.交换两个变量值的代码实现 +```java +class OperationExercise { + public static void main(String[] args) { + //第5题 + int i = 10; + int j = 20; + i = i ^ j; + j = i ^ j; + i = i ^ j; + System.out.println("i: " + i + ",j: " + j); + } +} +``` \ No newline at end of file diff --git "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/5.\346\257\224\350\276\203\350\277\220\347\256\227\347\254\246.md" "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/5.\346\257\224\350\276\203\350\277\220\347\256\227\347\254\246.md" index 9c0957a..07ed4d1 100644 --- "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/5.\346\257\224\350\276\203\350\277\220\347\256\227\347\254\246.md" +++ "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/5.\346\257\224\350\276\203\350\277\220\347\256\227\347\254\246.md" @@ -17,4 +17,6 @@ ## 5.1 比较运算符注意 -* 比较运算“==”不能写成“=”。 \ No newline at end of file +* 比较运算“==”不能写成“=”。 +* \>、 <、 \>=、 <= 只能用在数值型数据之间。 +* ==、!= 不仅可以用在数值型数据之间,还可以使用在其他引用类型变量之间。 \ No newline at end of file diff --git "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/9.\344\275\215\350\277\220\347\256\227\347\254\246.md" "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/9.\344\275\215\350\277\220\347\256\227\347\254\246.md" index 3512783..008fb88 100644 --- "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/9.\344\275\215\350\277\220\347\256\227\347\254\246.md" +++ "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/9.\344\275\215\350\277\220\347\256\227\347\254\246.md" @@ -4,6 +4,9 @@ 位运算符是直接对整数的二进制进行的运算。 +`注意`: 没有<<<。 + + (下表中\代表|) | 运算符 | 运算 | 范例 | diff --git "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/BitOperationExercise.class" "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/BitOperationExercise.class" index b24df04..ab34e38 100644 Binary files "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/BitOperationExercise.class" and "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/BitOperationExercise.class" differ diff --git "a/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/OperationExercise.class" "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/OperationExercise.class" new file mode 100644 index 0000000..4506cd5 Binary files /dev/null and "b/3.Java\345\237\272\346\234\254\350\257\255\346\263\225-\350\277\220\347\256\227\347\254\246/OperationExercise.class" differ diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/ifelse-1.jpg" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/ifelse-1.jpg" new file mode 100644 index 0000000..da25568 Binary files /dev/null and "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/ifelse-1.jpg" differ diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/ifelse-2.jpg" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/ifelse-2.jpg" new file mode 100644 index 0000000..439a8c5 Binary files /dev/null and "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/ifelse-2.jpg" differ diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/ifelse-3.jpg" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/ifelse-3.jpg" new file mode 100644 index 0000000..9a2630c Binary files /dev/null and "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/ifelse-3.jpg" differ diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/switch-case\347\273\223\346\236\204.jpg" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/switch-case\347\273\223\346\236\204.jpg" new file mode 100644 index 0000000..71feb70 Binary files /dev/null and "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266-assets/switch-case\347\273\223\346\236\204.jpg" differ diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/1.\346\265\201\347\250\213\346\216\247\345\210\266.md" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/1.\346\265\201\347\250\213\346\216\247\345\210\266.md" new file mode 100644 index 0000000..c5c949e --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/1.\346\265\201\347\250\213\346\216\247\345\210\266.md" @@ -0,0 +1,28 @@ +# 1. 流程控制 + +## 1.1 流程控制概念 +流程控制语句是用来控制程序中各语句执行顺序的语句,可以把语句组合成能完成一定功能的小逻辑块。 + +## 1.2 流程控制分类 + +流程控制采用结构化程序设计中规定的三种基本流程结构: +* 顺序结构 +* 分支结构 +* 循环结构 + +### 顺序结构 +程序从上到下逐行地执行,中间没有任何判断和跳转。 + +### 分支结构 +根据条件,选择性地执行某段代码。 +* if-else +* switch-case + +### 循环结构 +根据循环条件,重复性地执行某段代码。 +* while +* do...while +* for + +`注意`: JDK1.5提供了foreach循环,方便遍历集合、数组元素。 + diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/2.IfElse.java" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/2.IfElse.java" new file mode 100644 index 0000000..9916de9 --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/2.IfElse.java" @@ -0,0 +1,42 @@ +class IfElse{ + public static void main(String[] args) { + //举例1 + int heartBeats = 78; + if (heartBeats < 60 || heartBeats > 100) { + System.out.println("需要做进一步检查"); + } + System.out.println("检查结束"); + + //举例2 + int age = 23; + if (age < 18) { + System.out.println("还可以多看动画片"); + } else { + System.out.println("可以开始工作了"); + } + + //举例3 + if (age < 0) { + System.out.println("输入数据非法"); + } else if (age < 18) { + System.out.println("青少年时期"); + } else if (age < 35) { + System.out.println("青壮年时期"); + } else if (age < 60) { + System.out.println("中年时期"); + } else { + System.out.println("老年时期"); + } + + boolean x = true; + boolean y = false; + short z = 40; + if ((z++==40) && (y=true)){ + z++; + } + if((x=false) || ++z==43){ + z++; + } + System.out.println("z = " + z); + } +} \ No newline at end of file diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/2.if-else.md" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/2.if-else.md" new file mode 100644 index 0000000..8885b94 --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/2.if-else.md" @@ -0,0 +1,57 @@ +# 2. if-else + +## 2.1 if-else格式 +条件判断结构。 + + +### 格式1: 单独if +``` +if(条件表达式){ + 执行代码块; +} +``` + +![if-else格式1](../4.Java基本语法-流程控制-assets/ifelse-1.jpg) + + +### 格式2: if-else +``` +if(条件表达式){ + 执行代码块1; +} +else{ + 执行代码块2; +} +``` +![if-else格式2](../4.Java基本语法-流程控制-assets/ifelse-2.jpg) + + +### 格式3: if-else if-else + +``` +if(条件表达式1){ + 执行代码块1; +} +else if(条件表达式2){ + 执行代码块2; +} +... +else{ + 执行代码n; +} +``` + +![if-else格式2](../4.Java基本语法-流程控制-assets/ifelse-3.jpg) + + +## 2.2 if-else注意事项 +* else结构是可选的 +* 针对条件表达式: + * 如果多个条件表达式之间是"互斥"关系(没有交集关系),哪个判断和执行语句声明在上面还是下面无所谓。 + * 如果多个条件表达式之间有"交集"关系,需要根据实际情况,考虑清楚应该将哪个结构声明在上面。 + * 如果多个条件表达式之间有"包含"关系,通常情况下,需要将范围小的声明在范围大的上面,否则,范围小的就没机会执行。 +* if-else结构是可以相互嵌套的,但是最多不要超过3层 +* 如果if-else结构的执行语句只有1行时,对应的一对{}是可以省略的,但是不建议省略 +* 如果存在多个if但是只有一个else,else会采用就近原则,和else最近的一个if进行配对 +* 获取随机数: +> 公式: [a, b]: (int)(Math.random()*(b-a+1) + a) \ No newline at end of file diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/3.IfElseExercise.java" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/3.IfElseExercise.java" new file mode 100644 index 0000000..7b7d884 --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/3.IfElseExercise.java" @@ -0,0 +1,150 @@ +import java.util.Scanner; + +class IfElseExercise { + public static void main(String[] args) { + // 第1题 + Scanner scan1 = new Scanner(System.in); + + System.out.print("请输入岳小鹏成绩(0-100):"); + int score = scan1.nextInt(); + + if (score == 100) { + System.out.println("奖励一台BMMW"); + } else if (score > 80) { + System.out.println("奖励一台iphone xs max"); + } else if (score >= 60) { + System.out.println("奖励一台iPad"); + } else { + System.out.println("啥也没有"); + } + + // 第2题 + Scanner scan2 = new Scanner(System.in); + System.out.print("请输入第1个数字:"); + int num1 = scan1.nextInt(); + System.out.print("请输入第2个数字:"); + int num2 = scan1.nextInt(); + System.out.print("请输入第3个数字:"); + int num3 = scan1.nextInt(); + + if (num1 >= num2) { + if (num3 >= num2) { + System.out.println("顺序: " + num3 + "<" + num2 + "<" + num1); + } else if (num3 > num1) { + System.out.println("顺序: " + num1 + "<" + num2 + "<" + num3); + } else { + System.out.println("顺序: " + num1 + "<" + num3 + "<" + num2); + } + } else { + if (num3 >= num2) { + System.out.print("顺序: " + num1 + "<" + num2 + "<" + num3); + } else if (num3 <= num1) { + System.out.println("顺序: " + num3 + "<" + num1 + "<" + num2); + } else { + System.out.println("顺序: " + num1 + "<" + num3 + "<" + num2); + + } + } + + // 第3题 + int x = 4; + int y = 1; + if (x > 2) + if (y > 2) + System.out.println(x + y); + //System.out.println("anti"); + else + System.out.println("x is " + x); + + // 第4题 + boolean b = true; + //如果写成b=false,编译可以通过吗?如果能,结果是? + if (b == false) + System.out.println("a"); + else if (b) + System.out.println("b"); + else if (!b) + System.out.println("c"); + else + System.out.println("d"); + + //执行结果 + // case ==: b + // cse =: c + + //3.4 第1题 + Scanner scan3 = new Scanner(System.in); + System.out.print("请输入第1个数字: "); + int num4 = scan3.nextInt(); + System.out.print("请输入第2个数字: "); + int num5 = scan3.nextInt(); + int sum = num4 + num5; + if (sum >= 50) { + System.out.println("hello world!"); + } else { + System.out.println("sum: " + sum); + } + + //3.4 第2题 + Scanner scan4 = new Scanner(System.in); + System.out.print("请输入第1个数字: "); + double d1 = scan4.nextDouble(); + System.out.print("请输入第2个数字: "); + double d2 = scan4.nextDouble(); + double sum1 = d1 + d2; + double mul = d1 * d2; + if (d1 > 10.0 && d2 < 20.0) { + System.out.println("和是" + sum1); + } else { + System.out.println("乘积是:" + mul); + } + + + //3.4 第3题 + Scanner scan5 = new Scanner(System.in); + System.out.print("您家狗狗多大了: "); + int dogAge = scan5.nextInt(); + if (dogAge > 2) { + int result = (dogAge - 2) * 4 + 21; + System.out.println("您家的狗狗相当于人" + result + "岁"); + } else if (dogAge > 0) { + double result = dogAge * 10.5; + System.out.println("您家的狗狗相当于人" + result + "岁"); + } else { + System.out.println("狗狗的年龄必须大于0岁哦~"); + } + + //3.5 + int luckyNum = (int) (Math.random() * 90 + 10); + int luckyOne = luckyNum / 10; + int luckyTwo = luckyNum % 10; + System.out.println(luckyNum); + + + Scanner scan6 = new Scanner(System.in); + System.out.print("请输入一个两位数: "); + int userNum = scan6.nextInt(); + int userOne = userNum / 10; + int userTwo = userNum % 10; + + if(userNum < 10){ + System.out.println("输入有误,请输入2位数~"); + }else{ + // 正式的逻辑 + if(userOne==luckyOne && userTwo == luckyTwo){ + System.out.println("奖金10000美元"); + }else if(userOne==luckyTwo && userTwo==luckyOne){ + System.out.println("奖金3000美元"); + }else if(userOne==luckyOne || userTwo == luckyTwo){ + System.out.println("奖金1000美元"); + + }else if(userOne==luckyTwo || userTwo == luckyOne){ + System.out.println("奖金500美元"); + + }else{ + System.out.println("感谢您为福利彩票做贡献!"); + + } + } + } +} diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/3.if-else\347\273\203\344\271\240.md" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/3.if-else\347\273\203\344\271\240.md" new file mode 100644 index 0000000..b7ea45b --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/3.if-else\347\273\203\344\271\240.md" @@ -0,0 +1,260 @@ +# 3. if-else练习 + +## 3.1 根据考试成绩奖励 +岳小鹏参加Java考试,他和父亲岳不群达成承诺: +如果: +* 成绩为100分,奖励一台BMW +* 成绩为(80, 99],奖励一台iphone xs max +* 成绩为[60, 80],奖励一个iPad +其他时,什么奖励也没有。 + +请从键盘输入岳小鹏的期末成绩,并加以判断。 + + + +```java +import java.util.Scanner; + +class IfElseExercise { + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + + System.out.print("请输入岳小鹏成绩(0-100):"); + int score = scan.nextInt(); + + if (score == 100) { + System.out.println("奖励一台BMMW"); + } else if (score > 80) { + System.out.println("奖励一台iphone xs max"); + } else if (score >= 60) { + System.out.println("奖励一台iPad"); + } else { + System.out.println("啥也没有"); + } + } +} +``` + +## 3.2 按顺序输出三个数字 +由键盘输入3个整数,分别存入变量num1、num2、num3,对它们进行排序(使用if-else-if-else),并且从小到大输出。 + +```java +import java.util.Scanner; + +class IfElseExercise { + public static void main(String[] args) { + // 第2题 + Scanner scan2 = new Scanner(System.in); + System.out.print("请输入第1个数字:"); + int num1 = scan1.nextInt(); + System.out.print("请输入第2个数字:"); + int num2 = scan1.nextInt(); + System.out.print("请输入第3个数字:"); + int num3 = scan1.nextInt(); + + if (num1 >= num2) { + if (num3 >= num2) { + System.out.println("顺序: " + num3 + "<" + num2 + "<" + num1); + } else if (num3 > num1) { + System.out.println("顺序: " + num1 + "<" + num2 + "<" + num3); + } else { + System.out.println("顺序: " + num1 + "<" + num3 + "<" + num2); + } + } else { + if (num3 >= num2) { + System.out.print("顺序: " + num1 + "<" + num2 + "<" + num3); + } else if (num3 <= num1) { + System.out.println("顺序: " + num3 + "<" + num1 + "<" + num2); + } else { + System.out.println("顺序: " + num1 + "<" + num3 + "<" + num2); + + } + } + + } +} +``` + +## 3.3 写出执行结果 +```java +class IfElseExercise { + public static void main(String[] args) { + // 第3题 + int x = 4; + int y = 1; + if(x>2){ + if(y>2) + System.out.println(x+y); + System.out.println("anti"); + }else + System.out.println("x is " + x); + + } +} + +//执行结果 +//anti + +class IfElseExercise { + public static void main(String[] args) { + // 第3题 + int x = 4; + int y = 1; + if(x>2) + if(y>2) + System.out.println(x+y); + else + System.out.println("x is " + x); + + } +} +//执行结果: else采用就近原则 +// x is 4 +``` + +```java +class IfElseExercise { + public static void main(String[] args) { + // 第4题 + boolean b = true; + //如果写成b=false,编译可以通过吗?如果能,结果是? + if (b == false) + System.out.println("a"); + else if (b) + System.out.println("b"); + else if (!b) + System.out.println("c"); + else + System.out.println("d"); + + //执行结果 + // case ==: b + // cse =: c + } +} +``` + + +## 3.4 编程题 +1.编写程序,声明2个int型变量并赋值。判断2数之和,如过大于等于50,打印"hello world!" +```java +import java.util.Scanner; + + +class IfElseExercise{ + public static void main(String[] args){ + Scanner scan = new Scanner(System.in); + System.out.print("请输入第1个数字: "); + int num1 = scan.nextInt(); + System.out.print("请输入第2个数字: "); + int num2 = scan.nextInt(); + int sum = num1 + num2; + if(sum >= 50){ + System.out.println("hello world!"); + }else{ + System.out.println("sum: " + sum); + } + } +} +``` + +2.编写程序,声明2个double型变量并赋值。判断第一个数大于10.0,且第2个数小于20.0,打印2数之和。否则,打印两数的乘积。 +```java +import java.util.Scanner; + + +class IfElseExercise{ + public static void main(String[] args){ + Scanner scan = new Scanner(System.in); + System.out.print("请输入第1个数字: "); + double d1 = scan.nextDouble(); + System.out.print("请输入第2个数字: "); + double d2 = scan.nextDouble(); + double sum = d1 + d2; + double mul = d1 * d2; + if(d1 > 10.0 && d2 < 20.0){ + System.out.println("和是" + sum); + }else{ + System.out.println("乘积是:" + mul); + } + } +} + +``` + +3.我家的狗5岁了,5岁的狗相当于人类多大呢? 其实,狗的前两年每一年相当于人类的10.5岁,之后每一年增加4岁。编写一个程序,获取用户输入的狗的年龄,通过程序其显示相当于人的年龄,如果用户输入负数,请显示一个提示信息。 +```java +import java.util.Scanner; + + +class IfElseExercise{ + public static void main(String[] args){ + Scanner scan = new Scanner(System.in); + System.out.println("您家狗狗多大了: "); + int dogAge = scan.nextInt(); + if(dogAge > 2){ + int result = (dogAge - 2) * 4 + 10.5 * 2; + System.out.println("您家的狗狗相当于人" + result + "岁"); + }else if (dogAge > 0){ + double result = dogAge * 10.5; + System.out.println("您家的狗狗相当于人" + result + "岁"); + }else{ + System.out.println("狗狗的年龄不能为负数哦~"); + } + } +} +``` + +## 3.5 编程题 +假设你想开发一个玩彩票的游戏,程序随机产生一个2位数,提示用户输入一个2位数,然后按照下面的规则判定用户是否能赢: +* 1.如果用户输入的数匹配彩票的实际顺序,奖金10000美金 +* 2.如果用户输入的所有数字匹配彩票的所有数字,但顺序不一致,奖金3000美元 +* 3.如果用户输入的一个数字仅满足顺序情况下匹配彩票的一个数字,奖金1000美元 +* 4.如果用户输入的一个数字仅满足非顺序情况下匹配彩票的一个数字,奖金500美元 +* 5.如果用户输入的一个数字没有匹配任何一个数字,则彩票作废 + +> 提示: +>使用(int)(Math.random()*90+10)产生随机数。 +>Math.randon(): [0,1) * 90 + 10==>[10, 99) + + +```java +import java.util.Scanner; + +class IfElseExercise { + public static void main(String[] args) { + //3.5 + int luckyNum = (int) (Math.random() * 90 + 10); + int luckyOne = luckyNum / 10; + int luckyTwo = luckyNum % 10; + System.out.println(luckyNum); + + + Scanner scan6 = new Scanner(System.in); + System.out.print("请输入一个两位数: "); + int userNum = scan6.nextInt(); + int userOne = userNum / 10; + int userTwo = userNum % 10; + + if(userNum < 10){ + System.out.println("输入有误,请输入2位数~"); + }else{ + // 正式的逻辑 + if(userOne==luckyOne && userTwo == luckyTwo){ + System.out.println("奖金10000美元"); + }else if(userOne==luckyTwo && userTwo==luckyOne){ + System.out.println("奖金3000美元"); + }else if(userOne==luckyOne || userTwo == luckyTwo){ + System.out.println("奖金1000美元"); + + }else if(userOne==luckyTwo || userTwo == luckyOne){ + System.out.println("奖金500美元"); + + }else{ + System.out.println("感谢您为福利彩票做贡献!"); + + } + } + } +} +``` \ No newline at end of file diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/4.ScannerTest.java" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/4.ScannerTest.java" new file mode 100644 index 0000000..e4a40a2 --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/4.ScannerTest.java" @@ -0,0 +1,29 @@ +import java.util.Scanner; + +class ScannerTest{ + public static void main(String[] args) { + Scanner scan = new Scanner(System.in); + System.out.print("请输入您的名字:"); + String name = scan.next(); + System.out.println(name); + + System.out.print("请输入您的年龄:"); + int num = scan.nextInt(); + System.out.println(num); + + System.out.print("请输入您的体重:"); + double weight = scan.nextDouble(); + System.out.println(weight); + + System.out.print("您是否单身?(true/false)"); + boolean isSingle = scan.nextBoolean(); + System.out.println(isSingle); + + //非要获取char类型的值 + System.out.print("您的性别?(男/女)"); + String gender = scan.next(); + char genderChar = gender.charAt(0);//获取索引为0位置上的字符 + System.out.println(genderChar); + + } +} \ No newline at end of file diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/4.scanner.md" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/4.scanner.md" new file mode 100644 index 0000000..833c2ad --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/4.scanner.md" @@ -0,0 +1,37 @@ +# 4. scanner + +## 4.1 Scanner作用 + +如何从键盘获取不同类型变量?需要使用Scanner类。 + +## 4.2 实现步骤 + +* 1.导包 +```java +import java.util.Scanner; +``` + +* 2.Scanner实例化 + +```java +Scanner scan = new Scanner(System.in); +``` + + +* 3.调用Scanner类的相关方法,来获取指定类型的变量 + * next(): 获取字符串类型的值 + * nextBoolean(): 获取boolean类型的值 + * nextDouble(): 获取double类型的值 + * nextInt(): 获取int类型的值 + +```java +int num = scan.nextInt(); +``` + +## 4.3 Scanner注意事项 +* 对于char型的获取,Scanner没有提供相关的方法,只能获取一个字符串。 + +> 如果非要获取char类型,使用String的charAt(index)方法,获取字符串的索引上的字符。 + +* 需要根据相应的方法,来输入指定类型的值,如果输入的类型与要求的类型不匹配时,会报异常:java.util.InputMismatchException,导致程序终止。 + \ No newline at end of file diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/5.SwitchCaseTest.java" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/5.SwitchCaseTest.java" new file mode 100644 index 0000000..172a743 --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/5.SwitchCaseTest.java" @@ -0,0 +1,56 @@ +class SwitchCaseTest{ + public static void main(String[] args) { + int num = 2; + switch (num){ + case 0: + System.out.println("Zero"); + break; + case 1: + System.out.println("One"); + break; + case 2: + System.out.println("Two"); + break; + case 3: + System.out.println("Three"); + break; + case 4: + System.out.println("Four"); + break; + default: + System.out.println("other"); + } + + //****错误示例: switch表达式值为布尔类型,编译报错 + /* + int isBeauty = true; + switch (isBeauty){ + case true: + System.out.println("Beaty"); + break; + default: + System.out.println("Plain"); + } + */ + + + String season = "summer"; + switch (season){ + case "spring": + System.out.println("春暖花开"); + break; + + case "summer": + System.out.println("夏日炎炎"); + break; + case "autumn": + System.out.println("秋高气爽"); + break; + case "winnter": + System.out.println("白雪皑皑"); + break; + default: + System.out.println("输入季节有误"); + } + } +} \ No newline at end of file diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/5.switch-case.md" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/5.switch-case.md" new file mode 100644 index 0000000..faeaff3 --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/5.switch-case.md" @@ -0,0 +1,48 @@ +# 5. Switch-Case + +## 5.1 Switch-Case结构 + +``` +switch(表达式){ +case 常量1: + 语句1; + //break; +case 常量2: + 语句2; + //break; +... ... +case 常量N: + 语句N; + //break; +default: + 语句; + //break; +} +``` + +![switch-case结构](../4.Java基本语法-流程控制-assets/switch-case结构.jpg) + + +## 2.2 Switch-Case说明 +* 根据switch表达式中的值,依次匹配各个case中的常量,一旦匹配成功,则进入相应case结构中,调用其执行语句。当调用完执行语句后,则仍然继续向下执行其他case中的执行语句,直到遇到break关键字或switch-case结构末尾结束为止。 +* break可以使用在switch-case结构中,表示一旦执行到此关键字就跳出switch-case结构。 +* switch结构中的表达式,只能是如下的6种数据结构之一: + * byte + * short + * char + * int + * Enum(JDK5.0+新增的) + * String(JDK7.0+新增的) +* case后只能声明常量,不能声明范围。 +* break关键字是可选的,要根据实际的switch-case使用情况。 +* default相当于if-else结构中else,default结构是可选的,位置是灵活的。 +* 多个相同输出语句的case,可以合并。 +``` +case 常量1: +case 常量2: +case 常量3: + 语句; +``` + + + diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/6.SwitchCaseExercise.java" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/6.SwitchCaseExercise.java" new file mode 100644 index 0000000..a788411 --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/6.SwitchCaseExercise.java" @@ -0,0 +1,285 @@ +import java.util.Scanner; + +class SwitchCaseExercise { + public static void main(String[] args) { + //6.1.1: 首字母大写 + System.out.println("请输入一个字符串:"); + Scanner scan1 = new Scanner(System.in); + String word = scan1.next(); + char capti = word.charAt(0); + switch (capti) { + case 'a': + System.out.println("A"); + break; + case 'b': + System.out.println("B"); + break; + case 'c': + System.out.println("C"); + break; + case 'd': + System.out.println("D"); + break; + } + + //6.1.2: 打印成绩合格 + System.out.println("请输入学生的分数:"); + Scanner scan2 = new Scanner(System.in); + + int score = scan2.nextInt(); + + switch (score / 10) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + System.out.println("不合格"); + break; + case 6: + case 7: + case 8: + case 9: + case 10: + System.out.println("合格"); + break; + } + + //6.1.3: 打印月份 + System.out.println("请输入月份:"); + Scanner scan3 = new Scanner(System.in); + int month = scan3.nextInt(); + switch (month) { + case 3: + case 4: + case 5: + System.out.println("春天"); + break; + case 6: + case 7: + case 8: + System.out.println("夏天"); + break; + case 9: + case 10: + case 11: + System.out.println("秋天"); + break; + case 12: + case 1: + case 2: + System.out.println("冬天"); + break; + default: + System.out.println("输入参数有误"); + } + + //6.1.4: 打印是2019年哪天 + Scanner scan4 = new Scanner(System.in); + System.out.println("请输入2019年月份:"); + int monthNum = scan4.nextInt(); + System.out.println("请输入2019年日期:"); + int dayNum = scan4.nextInt(); + int totalDayNum = 365; + // 冗余 + /* + switch (monthNum){ + case 1: + if(dayNum > 0 && dayNum <= 31){ + int result = dayNum; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + + case 2: + if(dayNum > 0 && dayNum <= 28){ + int result = dayNum + 30; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + + case 3: + if(dayNum > 0 && dayNum <= 31){ + int result = dayNum + 30 + 28; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + case 4: + if(dayNum > 0 && dayNum <= 30){ + int result = dayNum + 30 + 28 + 31; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + + case 5: + if(dayNum > 0 && dayNum <= 31){ + int result = dayNum + 30 + 28 + 31 + 30; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + + case 6: + if(dayNum > 0 && dayNum <= 30){ + int result = dayNum + 30 + 28 + 31 + 30 + 31; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + + case 7: + if(dayNum > 0 && dayNum <= 31){ + int result = dayNum + 30 + 28 + 31 + 30 + 31 + 30; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + + case 8: + if(dayNum > 0 && dayNum <= 31){ + int result = dayNum + 30 + 28 + 31 + 30 + 31 + 30 + 31; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + case 9: + if(dayNum > 0 && dayNum <= 30){ + int result = dayNum + 30 + 28 + 31 + 30 + 31 + 30 + 31 + 31; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + + case 10: + if(dayNum > 0 && dayNum <= 31){ + int result = dayNum + 30 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + + case 11: + if(dayNum > 0 && dayNum <= 30){ + int result = dayNum + 30 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + + case 12: + if(dayNum > 0 && dayNum <= 31){ + int result = dayNum + 30 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30; + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + result + "天"); + }else{ + System.out.println("输入有误"); + } + break; + + + } + + */ + + //倒着写 + int sumDays = 0; + switch (monthNum) { + case 12: + sumDays += 30; + case 11: + sumDays += 31; + case 10: + sumDays += 30; + case 9: + sumDays += 31; + case 8: + sumDays += 31; + case 7: + sumDays += 30; + case 6: + sumDays += 31; + case 5: + sumDays += 30; + case 4: + sumDays += 31; + case 3: + sumDays += 28; + case 2: + sumDays += 31; + case 1: + sumDays += dayNum; + } + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + sumDays + "天"); + + //6.1.5 判断天数 + Scanner scan5 = new Scanner(System.in); + System.out.println("请输入年份:"); + int yearNum1 = scan5.nextInt(); + System.out.println("请输入月份:"); + int monthNum1 = scan5.nextInt(); + System.out.println("请输入日期:"); + int dayNum1 = scan5.nextInt(); + int totalDayNum1; + boolean isLeapYear; + + //判断是否是闰年 + if ((yearNum1 % 4 == 0 && yearNum1 % 100 != 0) || (yearNum1 % 400 == 0)) { + totalDayNum1 = 366; + isLeapYear = true; + } else { + totalDayNum1 = 365; + isLeapYear = false; + } + + int sumDays1 = 0; + //计算是第几月 + switch (monthNum1) { + case 12: + sumDays1 += 30; + case 11: + sumDays1 += 31; + case 10: + sumDays1 += 30; + case 9: + sumDays1 += 31; + case 8: + sumDays1 += 31; + case 7: + sumDays1 += 30; + case 6: + sumDays1 += 31; + case 5: + sumDays1 += 30; + case 4: + sumDays1 += 31; + case 3: + if (isLeapYear) { + sumDays1 += 29; + } else { + sumDays1 += 28; + } + case 2: + sumDays1 += 31; + case 1: + sumDays1 += dayNum1; + } + + System.out.println(monthNum1 + "月" + dayNum1 + "日是2019年的第" + sumDays1 + "天"); + + } +} \ No newline at end of file diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/6.switch-case\347\273\203\344\271\240.md" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/6.switch-case\347\273\203\344\271\240.md" new file mode 100644 index 0000000..62979eb --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/6.switch-case\347\273\203\344\271\240.md" @@ -0,0 +1,212 @@ +# 6. switch-case练习 + +## 6.1 例题 +1. 使用switch把小写类型的char转为大写。只转换a,b,c,d,e,其他的输出"other"。 +```java +import java.util.Scanner; + +class SwitchCaseExercise { + public static void main(String[] args) { + //6.1: 首字母大写 + System.out.println("请输入一个字符串:"); + Scanner scan1 = new Scanner(System.in); + String word = scan1.next(); + char capti = word.charAt(0); + switch (capti) { + case 'a': + System.out.println("A"); + break; + case 'b': + System.out.println("B"); + break; + case 'c': + System.out.println("C"); + break; + case 'd': + System.out.println("D"); + break; + } + } +} +``` +2.对学生成绩大于60分的,输出"合格",低于60分的,输出"不合格"。 +```java +import java.util.Scanner; + +class SwitchCaseExercise { + public static void main(String[] args) { + //6.2: 打印成绩合格 + System.out.println("请输入学生的分数:"); + Scanner scan2 = new Scanner(System.in); + + int score = scan2.nextInt(); + + switch (score / 10) { + case 0: + case 1: + case 2: + case 3: + case 4: + case 5: + System.out.println("不合格"); + break; + case 6: + case 7: + case 8: + case 9: + case 10: + System.out.println("合格"); + break; + } + } +} +``` +3.根据用于指定月份,打印该月份所属的季节,3-4-5:春季,6-7-8:夏季,9-10-11:秋季,12-1-2:冬季。 + +```java +import java.util.Scanner; + +class SwitchCaseExercise { + public static void main(String[] args) { + //6.3: 打印月份 + System.out.println("请输入月份:"); + Scanner scan3 = new Scanner(System.in); + int month = scan3.nextInt(); + switch (month) { + case 3: + case 4: + case 5: + System.out.println("春天"); + break; + case 6: + case 7: + case 8: + System.out.println("夏天"); + break; + case 9: + case 10: + case 11: + System.out.println("秋天"); + break; + case 12: + case 1: + case 2: + System.out.println("冬天"); + break; + default: + System.out.println("输入参数有误"); + } + } +} +``` + +4.从键盘上输入2019年的month和day,计算出输入日期为2019年的第几天。 + +```java +import java.util.Scanner; + +class SwitchCaseExercise { + public static void main(String[] args) { + //倒着写 + int sumDays = 0; + switch (monthNum) { + case 12: + sumDays += 30; + case 11: + sumDays += 31; + case 10: + sumDays += 30; + case 9: + sumDays += 31; + case 8: + sumDays += 31; + case 7: + sumDays += 30; + case 6: + sumDays += 31; + case 5: + sumDays += 30; + case 4: + sumDays += 31; + case 3: + sumDays += 28; + case 2: + sumDays += 31; + case 1: + sumDays += dayNum; + } + System.out.println(monthNum + "月" + dayNum + "日是2019年的第" + sumDays + "天"); + } +} +``` + +5.从键盘输入年月日,判断这一年是当年的第几天? +注:判断一年是否是闰年标准: +1)可以被4整除,但不可被100整除 +或 +2)可以被400整数 + +```java +import java.util.Scanner; + +class SwitchCaseExercise { + public static void main(String[] args) { + //6.1.5 判断天数 + Scanner scan5 = new Scanner(System.in); + System.out.println("请输入年份:"); + int yearNum1 = scan5.nextInt(); + System.out.println("请输入月份:"); + int monthNum1 = scan5.nextInt(); + System.out.println("请输入日期:"); + int dayNum1 = scan5.nextInt(); + int totalDayNum1; + boolean isLeapYear; + + //判断是否是闰年 + if((yearNum1 % 4 == 0 && yearNum1 % 100 != 0) || (yearNum1 % 400 == 0)){ + totalDayNum1 = 366; + isLeapYear = true; + }else{ + totalDayNum1 = 365; + isLeapYear = false; + } + + int sumDays1 = 0; + //计算是第几月 + switch (monthNum1){ + case 12: + sumDays1 += 30; + case 11: + sumDays1 += 31; + case 10: + sumDays1 += 30; + case 9: + sumDays1 += 31; + case 8: + sumDays1 += 31; + case 7: + sumDays1 += 30; + case 6: + sumDays1 += 31; + case 5: + sumDays1 += 30; + case 4: + sumDays1 += 31; + case 3: + if (isLeapYear){ + sumDays1 += 29; + }else{ + sumDays1 += 28; + } + case 2: + sumDays1 += 31; + case 1: + sumDays1 += dayNum1; + } + + System.out.println(monthNum1 + "月" + dayNum1 + "日是2019年的第" + sumDays1 + "天"); + + } +} +``` + diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/7.IfElseSwitchCase.java" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/7.IfElseSwitchCase.java" new file mode 100644 index 0000000..ac0aac5 --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/7.IfElseSwitchCase.java" @@ -0,0 +1,29 @@ +class IfElseSwitchCase { + public static void main(String[] args) { + // switch-case改写if-else程序 + int a = 3; + int x = 100; + if (a == 1) + x = 5; + else if (a == 2) + x += 10; + else if (a == 3) + x += 16; + else + x += 34; + + switch (a){ + case 1: + x = 5; + break; + case 2: + x += 10; + break; + case 3: + x += 16; + break; + default: + x += 34; + } + } +} \ No newline at end of file diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/7.if-else\345\222\214switch-case\345\205\263\347\263\273.md" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/7.if-else\345\222\214switch-case\345\205\263\347\263\273.md" new file mode 100644 index 0000000..21f8b17 --- /dev/null +++ "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/7.if-else\345\222\214switch-case\345\205\263\347\263\273.md" @@ -0,0 +1,51 @@ +# 7. if-else和switch-case关系 + +## 7.1 if-else和switch-case转换 +* 凡是可以用switch-case的结构,都可以转换成if-else,反之不成立。 +* 当我们写分支结构时,当发现既可以使用switch-case(同时,switch中表达式取值情况不太对),又可以使用if-else时,优先选择使用switch-case。 + + +## 7.2 例题 +使用switch-case改写下面的if-else: + +```java +class IfElseSwitchCase { + public static void main(String[] args) { + // switch-case改写if-else程序 + int a = 3; + int x = 100; + if (a == 1) + x = 5; + else if (a == 2) + x += 10; + else if (a == 3) + x += 16; + else + x += 34; + } +} +``` + +```java +class IfElseSwitchCase { + public static void main(String[] args) { + // switch-case改写if-else程序 + int a = 3; + int x = 100; + + switch (a){ + case 1: + x = 5; + break; + case 2: + x += 10; + break; + case 3: + x += 16; + break; + default: + x += 34; + } + } +} +``` \ No newline at end of file diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/IfElse.class" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/IfElse.class" new file mode 100644 index 0000000..258ba62 Binary files /dev/null and "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/IfElse.class" differ diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/IfElseExercise.class" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/IfElseExercise.class" new file mode 100644 index 0000000..5ff3cab Binary files /dev/null and "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/IfElseExercise.class" differ diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/IfElseSwitchCase.class" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/IfElseSwitchCase.class" new file mode 100644 index 0000000..8d7bff0 Binary files /dev/null and "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/IfElseSwitchCase.class" differ diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/ScannerTest.class" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/ScannerTest.class" new file mode 100644 index 0000000..1bb54fa Binary files /dev/null and "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/ScannerTest.class" differ diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/SwitchCaseExercise.class" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/SwitchCaseExercise.class" new file mode 100644 index 0000000..3c1a3b1 Binary files /dev/null and "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/SwitchCaseExercise.class" differ diff --git "a/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/SwitchCaseTest.class" "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/SwitchCaseTest.class" new file mode 100644 index 0000000..9cb4601 Binary files /dev/null and "b/4.Java\345\237\272\346\234\254\350\257\255\346\263\225-\346\265\201\347\250\213\346\216\247\345\210\266/SwitchCaseTest.class" differ