diff --git "a/01 \350\213\217\346\270\205\345\215\216/20221202 \345\210\206\346\224\257\347\273\223\346\236\204if\345\222\214switch.md" "b/01 \350\213\217\346\270\205\345\215\216/20221202 \345\210\206\346\224\257\347\273\223\346\236\204if\345\222\214switch.md" new file mode 100644 index 0000000000000000000000000000000000000000..994c676e58e3745f3f474280d7fa6692afa92793 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20221202 \345\210\206\346\224\257\347\273\223\346\236\204if\345\222\214switch.md" @@ -0,0 +1,329 @@ +# 分支结构之if和switch语句 + +#### 2022.12.02 + +### 流程控制语句 + +顺序结构 + +分支结构(if,switch) + +循环结构(for, while,do...while) + +### if + +#### if语句1 + +```java +格式: +if (关系表达式){ + 语句体; +} +eg: +public class a1 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入你的年龄:"); + int age = sc.nextInt(); + if (age>=18){ + System.out.println("你已成年,请愉快地玩游戏吧!"); + } + } +} +``` + +#### if语句2 + +```java +格式: +if (关系表达式){ + 语句体1; +}else{ + 语句体2; +} +eg: +public class a1 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入你的年龄:"); + int age = sc.nextInt(); + if (age>=18){ + System.out.println("你已成年,请愉快地玩游戏吧!"); + }else{ + System.out.println("很抱歉,你的年龄未达到要求,不能玩游戏!"); + } + } +} +``` + +#### if语句3 + +```java +格式: +if (关系表达式1){ + 语句体1; +}else if(关系表达式2){ + 语句体2; +} +else{ + 语句体3; +} +eg: +public class a1 { + public static void main(String[] args) { + Scanner sc = new Scanner(System.in); + System.out.println("请输入你的成绩:"); + int score = sc.nextInt(); + if (score >= 0 && score <= 100) { + if (score>=0 && score < 60) { + System.out.println("你的成绩为不合格。"); + } else if (score>=60 && score < 75) { + System.out.println("你的成绩为合格。"); + } else if (score>=75 && score < 90) { + System.out.println("你的成绩为良好。"); + } else { + System.out.println("你的成绩为优秀!"); + } + }else { + System.out.println("请重新输入。"); + } + } +} +``` + +注意:if else这种分支结构是互斥的,众多分支中,只能执行一个,也就是前面已经有一个满足了,后面的就不会执行了。 + +### switch + +```java +switch (表达式1){ + case 1: + 语句体1; + break; + case 2: + 语句体2; + break; + default: + 语句体3; + break; +} +eg: +public class a1 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("今天周几?"); + int week =sc.nextInt(); + switch (week){ + case 1: + System.out.println("今天的活动是:跑步"); + break; + case 2: + System.out.println("今天的活动是:游泳"); + break; + case 3: + System.out.println("今天的活动是:慢走"); + break; + case 4: + System.out.println("今天的活动是:动感单车"); + break; + case 5: + System.out.println("今天的活动是:拳击"); + break; + case 6: + System.out.println("今天的活动是:爬山"); + break; + case 7: + System.out.println("今天的活动是:好好吃一顿"); + break; + } + } +} +``` + +注意:switch能做的事,if也能做,反过来不一定。switch只能判断精准的某一个值,if判断的是一个区间一个范围。 + + + +### 字符串的比较 + +```java +import java.util.Scanner; + +public class a5 { + public static void main(String[] args) { + String password="aaa"; + Scanner sc = new Scanner(System.in); + System.out.println("请输入密码:"); + String newpassword=sc.next(); + if (password.equals(newpassword)){ + System.out.println("密码正确"); + } + else{ + System.out.println("密码错误"); + } + } +} +//因为String是引用类型,用equals方法比较是否相等。 +//char n=sc,next(),charAt(0)(输出第几个); +``` + + + +### 作业 + +某市不同车牌的出租车3千米的起步价和计费分别和计费分别为:夏利3元,3千米以外,2.1元/千米;富康4元,3千米以外,2.4元/千米;桑塔纳5元,3千米以外,2.7元/千米。编程实现从键盘输入乘车的车型及行车千米数,输入应付车费。 + +```java +import java.util.Scanner; + +public class a3 { + public static void main(String[] args) { + double pay=0; + Scanner sc = new Scanner(System.in); + System.out.println("请输入乘坐的车型:"); + String car = sc.next(); + System.out.println("请输入行车千米数:"); + double kilometer = sc.nextDouble(); + double kilo=kilometer-3; + switch (car) { + case "夏利": + if (kilometer > 0 && kilometer <= 3) { + pay+=3; + } else { + pay = 3+(kilo*2.1); + } + break; + case "富康": + if (kilometer > 0 && kilometer <= 3) { + pay += 4; + } else { + pay = 4+(kilo*2.4); + } + break; + case "桑塔纳": + if (kilometer > 0 && kilometer <= 3) { + pay += 5; + } else { + pay = 5+(kilo*2.7); + } + break; + } + System.out.println("需要支付:"+pay); + } +} +``` + + + +使用switch和if语句算出今天是今年的第几天。 + +```java +import java.util.Scanner; + +public class a1 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入今天的日期:"); + int year= sc.nextInt(); + int month=sc.nextInt(); + int day= sc.nextInt(); + int sum=0; + switch (month){ + case 12: + sum+=30; + case 11: + sum+=31; + case 10: + sum+=30; + case 9: + sum+=31; + case 8: + sum+=31; + case 7: + sum+=30; + case 6: + sum+=31; + case 5: + sum+=30; + case 4: + sum+=31; + case 3: + if (year%4==0 && year%100!=0 ||year%400==0){ + sum+=29; + }else{ + sum+=28; + } + case 2: + sum+=31; + case 1: + sum=sum+day; + } + System.out.println(year+"年"+month+"月"+day+"日是本年的第"+sum+"天"); + } +} + +``` + + + +编写一个程序,根据用户输入的一个字符,判断该字符是不是字符,如果是字母,判断该字母是声母还是韵母,是大写字母还是小写字母;如果不是字母,则输出“你输入的字符不是字母”。 + +```java +import java.util.Scanner; + +public class a4 { + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("请输入一个字符:"); + char a=sc.next().charAt(0); + + if (a>='a'&& a<='z' || a>='A'&& a<='Z'){ + switch (a){ + case 'a': + case 'e': + case 'i': + case 'o': + case 'u': + System.out.println(a+"是小写韵母"); + break; + case 'A': + case 'E': + case 'I': + case 'O': + case 'U': + System.out.println(a+"是大写韵母"); + break; + case 'b': + case 'c': + case 'd': + case 'g': + case 'h': + case 'j': + case 'k': + case 'l': + case 'm': + case 'n': + case 'p': + case 'q': + case 'r': + case 's': + case 't': + case 'v': + case 'w': + case 'x': + case 'y': + case 'z': + System.out.println(a+"是小写声母"); + break; + default: + System.out.println(a+"是大写声母"); + } + + }else{ + System.out.println("输入的字符不是字母。"); + } + + } +} +``` + diff --git "a/01 \350\213\217\346\270\205\345\215\216/20221206 \345\276\252\347\216\257\350\257\255\345\217\245.md" "b/01 \350\213\217\346\270\205\345\215\216/20221206 \345\276\252\347\216\257\350\257\255\345\217\245.md" new file mode 100644 index 0000000000000000000000000000000000000000..eecdde89a036e21435ea67ffffd0863f5f694d10 --- /dev/null +++ "b/01 \350\213\217\346\270\205\345\215\216/20221206 \345\276\252\347\216\257\350\257\255\345\217\245.md" @@ -0,0 +1,178 @@ +# 循环语句 + +#### 2022.12.06 + +### for循环 + +```java +格式: +for (初始化语句;条件判断语句:条件控制语句){ + 循环体语句; +} +``` + +```java +//连续输出'aaa' +public class f1 { + public static void main(String[] args) { + for (int i=1;i<=5;){ //i定义初始值为1 + System.out.println("aaa"); + } + } +} //输出结果为连续的aaa +//输出1-5 + for (int i=1;i<=5;i++){ + System.out.println(i); + //System.out.print(i); 横向输出 + } +``` + +```java +//输出1-5的和 +public class f { + public static void main(String[] args) { + int sum=0; + for (int i=1;i<=5;i++){ + sum+=i; + } + System.out.println(sum); + } +} +//输出1-100的偶数和 + int sum=0; //定义总值 + for (int i=1;i<=100;i++){ + if (i%2==0){ //判断偶数 + sum+=i; + } + } + System.out.println(sum); +``` + +```java +//水仙花数-例如153,1^3+5^3+3^3=153 + int count=0; //计数 + for (int i =100;i<=999;i++){ + int a=i%10;//个位 + int b=i/10%10;//十位 + int c=i/100%10;//百位 + if(a*a*a+b*b*b+c*c*c==i) { + System.out.print(i+" "); + count++; + if (count % 2==0){ + System.out.println(); //每两个换行 + } + } + } +``` + +### while循环 + +```java +格式: +初始化语句; +while (条件判断语句){ + 循环体语句; + 条件控制语句; +} +``` + +```java +//输出五次aaa +public class w1 { + public static void main(String[] args) { + int i=1; + while (i<=5){ + System.out.println("aaa"); + i++; + } + } +} +``` + +```java +//纸张折叠几次和珠穆朗玛峰一样高 +public class w1 { + public static void main(String[] args) { + int count=0;//折叠的次数 + double paper=0.1;//纸张的初始厚度 + double feng=8844430;//峰高 + while (paper<=feng){ + paper*=2; //每折叠一次,厚度就会加倍 + count++; //折叠的总次数 + } + System.out.println("需要折叠"+count+"次"); + } +} +``` + +### dowhile循环 + +```java +格式: +do{ + 循环体语句; + 条件控制语句; +} while(条件判断语句); +``` + +```java +//输出五次aaa +public class w1 { + public static void main(String[] args) { + int a=1; + do{ + System.out.println("aaa"); + a++; + }while (a<=5); + } +} +``` + +#### 区别 + +for和while是先判断后循环,do...while是先执行一次,然后判断是否继续执行。 + +### 死循环 + +```java +//for + for(;;){ + System.out.println("aa"); + } +//while + while(true){ + System.out.println("hhh"); + } +//do...while + do{ + System.out.println("~"); + }while(true); +``` + +### 跳转控制语句 + +break:跳出循环,结束循环 + +```java +//模拟20岁工作到80岁, 60岁退休. +for (int a=20;a<=80;a++){ + if (a==60){ + break; + } + System.out.println(a+"岁在上班"); + } + +``` + +continue:跳出本次循环,继续下次循环 + +```java +//模拟电梯上行的过程 1-24层, 4层不停. + for (int a=1;a<=24;a++){ + if (a==4){ + continue; + } + System.out.println(a+"层"); + } +``` +