1、选择语句

  1. /**
  2. * @author Lynn
  3. * @create 2020-11-18-13:42
  4. */
  5. public class A_if {
  6. /**
  7. *if 语句第一种格式:
  8. * if(关系表达式) {
  9. * 语句体
  10. * }
  11. */
  12. public static void main(String[] args) {
  13. int age=12;
  14. if(age<18){
  15. System.out.println("好好学习,天天向上!");
  16. }
  17. }
  18. }
  1. /**
  2. * @author Lynn
  3. * @create 2020-11-18-13:44
  4. */
  5. public class B_if {
  6. /**
  7. *if 语句第二种格式:
  8. * if(关系表达式) {
  9. * 语句体 1;
  10. * }else {
  11. * 语句体 2;
  12. * }
  13. * @param args
  14. */
  15. public static void main(String[] args) {
  16. int age=12;
  17. if(age<25){
  18. System.out.println("好好学习");
  19. }else{
  20. System.out.println("好好工作");
  21. }
  22. }
  23. }
  1. /**
  2. * @author Lynn
  3. * @create 2020-11-18-13:45
  4. */
  5. public class C_if {
  6. /**
  7. * if 语句第三种格式:
  8. * if(关系表达式 1) {
  9. * 语句体 1;
  10. * }else if (关系表达式 2) {
  11. * 语句体 2;
  12. * }
  13. * …
  14. * else {
  15. * 语句体 n+1;
  16. * }
  17. * @param args
  18. */
  19. public static void main(String[] args) {
  20. int score=88;
  21. if(score<60){
  22. System.out.println("不及格");
  23. }else if(score>=60 && score<80){
  24. System.out.println("中等");
  25. }else if(score>=80 && score<90){
  26. System.out.println("良好");
  27. }
  28. }
  29. }

练习

  1. import java.util.Scanner;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-18-13:48
  5. */
  6. public class D_if {
  7. // 键盘录入两个数据,获取这两个数据的较大值
  8. public static void main(String[] args) {
  9. Scanner sc=new Scanner(System.in);
  10. System.out.println("输入第一个数:");
  11. int num=sc.nextInt();
  12. System.out.println("输入第二个数:");
  13. int num2=sc.nextInt();
  14. if(num>num2){
  15. System.out.println(num);
  16. }else{
  17. System.out.println(num2);
  18. }
  19. }
  20. }
  1. import java.util.Scanner;
  2. /**
  3. * @author Lynn
  4. * @create 2020-11-18-13:52
  5. */
  6. public class E_if {
  7. /**
  8. * 键盘录入学生考试成绩,请根据成绩判断该学生属于哪个级别
  9. * * 90-100 优秀
  10. * * 80-90 好
  11. * * 70-80 良
  12. * * 60-70 及格
  13. * * 60以下 不及格
  14. */
  15. public static void main(String[] args) {
  16. Scanner sc=new Scanner(System.in);
  17. System.out.println("录入学生考试成绩:");
  18. int score=sc.nextInt();
  19. if(score>=90 && score<=100){
  20. System.out.println("优秀");
  21. }else if(score>=80 && score<90){
  22. System.out.println("好");
  23. }else if(score>=70 && score<80){
  24. System.out.println("良");
  25. }else if(score>=60 && score<70){
  26. System.out.println("及格");
  27. }else{
  28. System.out.println("不及格");
  29. }
  30. }
  31. }
  1. import com.sun.scenario.effect.impl.sw.sse.SSEBlend_SRC_OUTPeer;
  2. import java.util.Scanner;
  3. /**
  4. * @author Lynn
  5. * @create 2020-11-18-14:00
  6. */
  7. public class F_switch {
  8. //switch 语句实现根据数字输出对应星期
  9. public static void main(String[] args) {
  10. Scanner sc=new Scanner(System.in);
  11. System.out.println("输入数字:");
  12. int day=sc.nextInt();
  13. switch (day){
  14. case 1:
  15. System.out.println("星期一");
  16. break;
  17. case 2:
  18. System.out.println("星期二");
  19. break;
  20. case 3:
  21. System.out.println("星期三");
  22. break;
  23. case 4:
  24. System.out.println("星期四");
  25. break;
  26. case 5:
  27. System.out.println("星期五");
  28. break;
  29. case 6:
  30. System.out.println("星期六");
  31. break;
  32. case 7:
  33. System.out.println("星期日");
  34. break;
  35. }
  36. String s="MON";
  37. switch (s){
  38. case "MON":
  39. System.out.println("星期一");
  40. break;
  41. }
  42. }
  43. }

image.png

2、循环语句

for循环

for循环语句格式:

for(初始化语句;判断条件语句;控制条件语句) {
循环体语句;
}

执行流程

A:执行初始化语句
B:执行判断条件语句,看其结果是true还是false
如果是false,循环结束。
如果是true,继续执行。
C:执行循环体语句
D:执行控制条件语句
E:回到B继续

  1. /**
  2. * @author Lynn
  3. * @create 2020-11-18-14:10
  4. */
  5. public class A {
  6. //打印0-9
  7. public static void main(String[] args) {
  8. //for 循环语句格式:
  9. //for(初始化语句;判断条件语句;控制条件语句) {
  10. // 循环体语句;
  11. //}
  12. for(int i=0;i<10;i++){
  13. System.out.println(i);
  14. }
  15. }
  16. }

练习

  1. /**
  2. *@author Lynn
  3. *
  4. *@create 2020-11-18-14:15
  5. */
  6. public class B {
  7. //求出1-5之间数据之和
  8. public static void main(String[] args) {
  9. int sum=0;
  10. for(int i=1;i<=5;i++){
  11. sum+=i;
  12. }
  13. System.out.println(sum);
  14. }
  15. }
  1. /**
  2. * @author Lynn
  3. * @create 2020-11-18-14:16
  4. */
  5. public class C {
  6. //求出1-100之间偶数和
  7. public static void main(String[] args) {
  8. int sum=0;
  9. for(int i=2;i<=100;i+=2){
  10. System.out.print(i+"\t");
  11. sum+=i;
  12. }
  13. System.out.println();
  14. System.out.println(sum);
  15. System.out.println("-----------------");
  16. for(int j=1;j<=50;j++){
  17. if(j%3==0 && j%5==0){
  18. System.out.println(j);
  19. }
  20. }
  21. }
  22. }

练习

  1. /**
  2. * @author Lynn
  3. * @create 2020-11-18-14:41
  4. */
  5. public class D {
  6. //for 循环实现在控制台打印水仙花数
  7. /**
  8. * 分析:
  9. * * 什么是水仙花数呢?
  10. * * 所谓的水仙花数是指一个三位数,其各位数字的立方和等于该数本身。
  11. * * 举例:153就是一个水仙花数。
  12. * * 153 = 1*1*1 + 5*5*5 + 3*3*3
  13. * *
  14. * * A:三位数其实就告诉了我们水仙花数的范围
  15. * * 100-999
  16. * * B:如何获取一个数据的每一个位上的数呢?
  17. * * 举例:我有一个数据153,请问如何获取到个位,十位,百位
  18. * * 个位:153%10 = 3;
  19. * * 十位:153/10%10 = 5;
  20. * * 百位:153/100 = 1;
  21. * * 千位:...
  22. * * 万位:...
  23. * * C:让每个位上的立方和相加,并和该数据进行比较,如果相等,就说明该数据是
  24. * 水仙花数,在控制台输出
  25. * @param args
  26. */
  27. public static void main(String[] args) {
  28. int count=0;
  29. for(int i=100;i<=999;i++){
  30. int ge=i%10;
  31. int shi=i/10%10;
  32. int bai=i/10/10%10;
  33. if((ge*ge*ge+shi*shi*shi+bai*bai*bai)==i){
  34. System.out.println(i);
  35. count++;
  36. }
  37. }
  38. System.out.println(count);
  39. }
  40. }

while循环

  1. /**
  2. * @author Lynn
  3. * @create 2020-11-18-14:49
  4. */
  5. public class E {
  6. //while 循环实现 1-100 之间数据求和
  7. /**
  8. * 基本格式
  9. * while(判断条件语句) {
  10. * 循环体语句;
  11. * }
  12. */
  13. public static void main(String[] args) {
  14. int i=1;
  15. int sum=0;
  16. while (i<=100){
  17. sum+=i;
  18. i++;
  19. }
  20. System.out.println(sum);
  21. }
  22. }

do…while循环

  1. /**
  2. * @author Lynn
  3. * @create 2020-11-18-14:53
  4. */
  5. public class F {
  6. //do···while 循环实现 1-100 之间数据求和
  7. /**
  8. * 基本格式
  9. * do {
  10. * 循环体语句;
  11. * }while(判断条件语句);
  12. * @param args
  13. */
  14. public static void main(String[] args) {
  15. int i=1;
  16. int sum=0;
  17. do{
  18. sum+=i;
  19. i++;
  20. } while (i<=100);
  21. System.out.println(sum);
  22. }
  23. }

注意:
三种循环的区别
do…while 循环至少会执行一次循环体。
for 循环和 while 循环只有在条件成立的时候才会去执行循环体
for 循环语句和 while 循环语句的小区别:
使用区别:控制条件语句所控制的那个变量,在 for 循环结束后,就不能再被访问到了,而 while 循环结束还可以继续使用,如果你想继续使用,就用 while,否则推荐使用 for。原 因是 for 循环结束,该变量就从内存中消失,能够提高内存的使用效率。