diff --git "a/42 \346\226\271\345\242\236\345\205\264/20221202 swith\350\257\255\345\217\245.md" "b/42 \346\226\271\345\242\236\345\205\264/20221202 swith\350\257\255\345\217\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..e3f7403e7f01e38873044bdc215ec4bfa497a71f --- /dev/null +++ "b/42 \346\226\271\345\242\236\345\205\264/20221202 swith\350\257\255\345\217\245.md" @@ -0,0 +1,53 @@ +# switch语句 + +## switch语法和执行流程 + +```java +switch(表达式){ + case 值1: + System.out.println(""); + break; + case 值2: + System.out.println(""); + break; + case 值3: + System.out.println(""); + break; + case 值4: + System.out.println(""); + break; + defalut: + System.out.println(""); + break; +} +执行流程: +1、首先计算表达式的值. +2、依次和case后面的值进行比较,如果有对应的值,就会执行相应的语句,在执行的过程中,遇到break就会结束. +3、如果所有的case后面的值和表达式的不匹配,就会执行defalut里面的语句体,然后结束整个switch语句. + + + 1、表达式(将要匹配的值)取值可以为:byte,short,int,char.JDK5以后是可以枚举的,JDK7以后是可以有string的,但是布尔类型是不支持的. + 2、case后面要跟的是和表达式进行比较的值. + 3、break:中断语句,结束switch语句. + 4、case后面只能是字面量不能是变量. + 5、case给出的值不能重复. +``` + +## switch和if的区别 + +```java +1、switch能做的if也能做,但是if能做的switch不一定能做。 +2、if可以判断某个区间,而switch不能,它只能判断某个具体的值。 +3、判断精准的值用switch,粗略的用if。 + +``` + +## switch穿透 + +```java +case后面不加break,就会穿透 + 1、有关字符的比较用equls + 2、字符 char i = sc.next()charAt(0); + 3、swith支持字符串(不支持布尔类型) +``` + diff --git "a/42 \346\226\271\345\242\236\345\205\264/\345\210\244\346\226\255\345\215\203\347\261\263\346\225\260.md" "b/42 \346\226\271\345\242\236\345\205\264/\345\210\244\346\226\255\345\215\203\347\261\263\346\225\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..a87942a4ddc452c62d3289391d8828ae97672bab --- /dev/null +++ "b/42 \346\226\271\345\242\236\345\205\264/\345\210\244\346\226\255\345\215\203\347\261\263\346\225\260.md" @@ -0,0 +1,38 @@ +import java.util.Scanner; + +public class to { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("规则:夏利超出3km,2.1km/元,富康超出3km,2.4km/元,桑塔纳超出3km,2.7km/元."); + System.out.println("请输入要乘坐的车辆所对应的数字:(1、夏利,2、富康.3、桑塔纳)"); + while (true) { + int num = sc.nextInt(); + if (num == 1 || num == 2 || num == 3) { + switch (num) { + case 1: + System.out.println("请输入km数:"); + int km = sc.nextInt(); + System.out.println("你本次需要支付:" + ((km - 3) * 2.1)); + break; + case 2: + System.out.println("请输入千米数:"); + int km1 = sc.nextInt(); + System.out.println("你本次需要支付:" + ((km1 - 3) * 2.4)); + break; + case 3: + System.out.println("请输入千米数:"); + int km2 = sc.nextInt(); + System.out.println("你本次需要支付:" + ((km2 - 3) * 2.7)); + break; + default: + System.out.println("输入无效"); + break; + } + } else { + System.out.println("输入无效"); + } + + + } + } +} diff --git "a/42 \346\226\271\345\242\236\345\205\264/\345\210\244\346\226\255\351\227\260\345\271\264\345\222\214\345\271\263\345\271\264.md" "b/42 \346\226\271\345\242\236\345\205\264/\345\210\244\346\226\255\351\227\260\345\271\264\345\222\214\345\271\263\345\271\264.md" new file mode 100644 index 0000000000000000000000000000000000000000..5eab176ba5072e7fa116a6d48a9f09b08cac933e --- /dev/null +++ "b/42 \346\226\271\345\242\236\345\205\264/\345\210\244\346\226\255\351\227\260\345\271\264\345\222\214\345\271\263\345\271\264.md" @@ -0,0 +1,106 @@ +```java +import java.util.Scanner; + +public class year { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入当前年份例如:(2019)"); + int year = sc.nextInt(); + if ( ( ( year % 4 == 0 && year % 100 != 0) || year % 400 == 0)){ + System.out.println("该年为润年"+"\n"+"闰年1-12月分别为31天,29天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天"); + System.out.println("请输入你想输入月份:例如(5)"); + int month2 = sc.nextInt(); + System.out.println("请输入现在几号例如:(3)"); + int day = sc.nextInt(); + switch (month2){ + case 1: + System.out.println("现在是"+year+"的第"+(day)+"天"); + break; + case 2: + System.out.println("现在是"+year+"的第"+(31+day)+"天"); + break; + case 3: + System.out.println("现在是"+year+"的第"+(60+day)+"天"); + break; + case 4: + System.out.println("现在是"+year+"的第"+(91+day)+"天"); + break; + case 5: + System.out.println("现在是"+year+"的第"+(121+day)+"天"); + break; + case 6: + System.out.println("现在是"+year+"的第"+(152+day)+"天"); + break; + case 7: + System.out.println("现在是"+year+"的第"+(182+day)+"天"); + break; + case 8: + System.out.println("现在是"+year+"的第"+(213+day)+"天"); + break; + case 9: + System.out.println("现在是"+year+"的第"+(244+day)+"天"); + break; + case 10: + System.out.println("现在是"+year+"的第"+(274+day)+"天"); + break; + case 11: + System.out.println("现在是"+year+"的第"+(305+day)+"天"); + break; + case 12: + System.out.println("现在是"+year+"的第"+(335+day)+"天"); + break; + default: + System.out.println("输入无效"); + break; + } + } else { + System.out.println("该年份为平年"+"\n"+"平年1-12月分别为31天,28天,31天,30天,31天,30天,31天,31天,30天,31天,30天,31天,"); + System.out.println("请输入你想输入月份:"); + int month1 = sc.nextInt(); + System.out.println("请输入现在几号例如:(3)"); + int day1 = sc.nextInt(); + switch (month1){ + case 1: + System.out.println("现在是"+year+"的第"+(day1)+"天"); + break; + case 2: + System.out.println("现在是"+year+"的第"+(31+day1)+"天"); + break; + case 3: + System.out.println("现在是"+year+"的第"+(59+day1)+"天"); + break; + case 4: + System.out.println("现在是"+year+"的第"+(90+day1)+"天"); + break; + case 5: + System.out.println("现在是"+year+"的第"+(120+day1)+"天"); + break; + case 6: + System.out.println("现在是"+year+"的第"+(151+day1)+"天"); + break; + case 7: + System.out.println("现在是"+year+"的第"+(181+day1)+"天"); + break; + case 8: + System.out.println("现在是"+year+"的第"+(212+day1)+"天"); + break; + case 9: + System.out.println("现在是"+year+"的第"+(243+day1)+"天"); + break; + case 10: + System.out.println("现在是"+year+"的第"+(273+day1)+"天"); + break; + case 11: + System.out.println("现在是"+year+"的第"+(304+day1)+"天"); + break; + case 12: + System.out.println("现在是"+year+"的第"+(334+day1)+"天"); + break; + default: + System.out.println("输入无效"); + break; + } + } + } +} +``` \ No newline at end of file