• 程序流程控制


    顺序结构

    Java中定义成员变量时采用合法的前向引用。 从上往下执行


    分支结构

     有if…else和switch-case两种分支语句

    一、if else if else 

    1. /*
    2. 如果:
    3. 成绩为100分时,奖励一辆BMW;
    4. 成绩为(80,99]时,奖励一台iphone xs max;
    5. 当成绩为[60,80]时,奖励一个 iPad;
    6. 其它时,什么奖励也没有。
    7. 请从键盘输入岳小鹏的期末成绩,并加以判断
    8. */
    9. import java.util.Scanner;
    10. class ScoreTest{
    11. public static void main(String[] args){
    12. System.out.println("输入考试分数(0-100)");
    13. Scanner scanner = new Scanner(System.in);
    14. double score=scanner.nextDouble();
    15. if(score==100){
    16. System.out.println("奖励一辆BMW");
    17. }else if(score>80 && score<=99){
    18. System.out.println("奖励一台iphone xs max");
    19. }else if(score>=60 && score<=80){
    20. System.out.println("奖励一个 iPad");
    21. }else{
    22. System.out.println("什么奖励也没有");
    23. }
    24. }
    25. }

    说明:

    else可以没有

    多分支 条件互斥的话 循序没关系

    多分支条件有交集 顺序很关键  认证考虑   谨慎驾驶

    多分支包含的关系 应该先写小的 在写大条件

    if else 可以嵌套

    只有一行执行语句省略可以大括号

    例题

    1. //1)对下列代码,若有输出,指出输出结果。
    2. int x = 4;
    3. int y = 1;
    4. if (x > 2) {
    5. if (y > 2)
    6. System.out.println(x + y);
    7. System.out.println("atguigu");
    8. } else
    9. System.out.println("x is " + x);

    输出结果:  atguigu

    二、switch

    格式: 

    switch(表达式){
    case 常量1:
    语句1;
    // break;
    case 常量2:
    语句2;
    // break;
    … …
    case 常量N:
    语句N;
    // break;
    default:
    语句;
    // break;
    }

    注意点:

    ① 根据switch表达式中的值,依次匹配各个case中的常量。一旦匹配成功,则进入相应case结构中,调用其执行语句。
      当调用完执行语句以后,则仍然继续向下执行其他case结构中的执行语句,直到遇到break关键字或此switch-case结构
      末尾结束为止。

    ② break,可以使用在switch-case结构中,表示一旦执行到此关键字,就跳出switch-case结构

    ③ switch结构中的表达式,只能是如下的6种数据类型之一:
       byte 、short、char、int、枚举类型(JDK5.0新增)、String类型(JDK7.0新增)

    ④ case 之后只能声明常量。不能声明范围。

    ⑤ break关键字是可选的。

    ⑥ default:相当于if-else结构中的else.  
      default结构是可选的,而且位置是灵活的。

    ⑦ 凡是可以使用switch-case的结构,都可以转换为if-else。反之,不成立。
    ⑧ 既可以使用switch-case,(同时,switch中表达式的取值情况不太多),又可以使用if-else时,我们优先选择使用switch-case。原因:switch-case执行效率稍高。

    ⑨ case可以合并

    例子:

    1. /*
    2. 从键盘分别输入年、月、日,判断这一天是当年的第几天
    3. 注:判断一年是否是闰年的标准:
    4. 1)可以被4整除,但不可被100整除
    5. 2)可以被400整除
    6. */import java.util.Scanner;class SwitchTest05{
    7. public static void main(String[] args){
    8. Scanner scan = new Scanner(System.in);
    9. System.out.println("请输入年份");
    10. int year = scan.nextInt();
    11. System.out.println("请输入月份");
    12. int month = scan.nextInt();
    13. System.out.println("请输入日期");
    14. int day = scan.nextInt();
    15. int dayIndex=0;
    16. switch(month){
    17. case 12:
    18. dayIndex+=30;
    19. case 11:
    20. dayIndex+=31;
    21. case 10:
    22. dayIndex+=30;
    23. case 9:
    24. dayIndex+=31;
    25. case 8:
    26. dayIndex+=31;
    27. case 7:
    28. dayIndex+=30;
    29. case 6:
    30. dayIndex+=31;
    31. case 5:
    32. dayIndex+=30;
    33. case 4:
    34. dayIndex+=31;
    35. case 3:
    36. if((year%4==0&&year%100!=0)||year%400==0){
    37. dayIndex+=29;
    38. }else{
    39. dayIndex+=28;
    40. }
    41. case 2:
    42. dayIndex+=31;
    43. case 1:
    44. dayIndex+=day;
    45. System.out.println(month+"月"+day+"日是2022年的第"+dayIndex+"天");
    46. }
    47. }
    48. }
    49. /*
    50. 从键盘上读入一个学生成绩,存放在变量score中,根据score的
    51. 值输出其对应的成绩等级:
    52. score>=90 等级: A
    53. 70<=score<90 等级: B
    54. 60<=score<70 等级: C
    55. score<60 等级: D
    56. */import java.util.Scanner;class SwitchTest07{
    57. public static void main(String[] args){
    58. Scanner scan = new Scanner(System.in);
    59. System.out.println("请输入分数");
    60. int score = scan.nextInt();
    61. // 方式一:
    62. /*
    63. if(score>=90){
    64. System.out.println("等级A");
    65. }else if(score>=70&&score<90){
    66. System.out.println("等级B");
    67. }else if(score>=60&&score<70){
    68. System.out.println("等级C");
    69. }else{
    70. System.out.println("等级D");
    71. }
    72. */
    73. // 方式二:
    74. int level = score/10;
    75. switch(level){
    76. case 10:
    77. case 9:
    78. System.out.println("等级A");
    79. break;
    80. case 8:
    81. case 7:
    82. System.out.println("等级B");
    83. break;
    84. case 6:
    85. System.out.println("等级C");
    86. break;
    87. default:
    88. System.out.println("等级D");
    89. break;
    90. }
    91. }
    92. }

    if和switch语句很像,具体什么场景下,应用哪个语句呢?
    1如果判断的具体数值不多,而且符合byte、short 、char、int、String、枚举等几
    种类型。建议使用swtich语句。因为效率稍高。


    循环结构

     有while、do…while、for三种循环语句。

    for:

    语法格式
    for (①初始化部分; ②循环条件部分; ④迭代部分){
    ③循环体部分;
    } 

    注意:②为Boolean类型

    1. /*
    2. 100以内的偶数
    3. 所有偶数的和
    4. 偶数的个数
    5. */
    6. class ForTest03{
    7. public static void main(String[] args){
    8. int sum = 0;
    9. int count = 0;
    10. for(int i=1;i<=100;i++){
    11. if(i%2==0){
    12. System.out.println("100以内的偶数 是"+i);
    13. sum+=i;
    14. count++;
    15. }
    16. }
    17. System.out.println("所有偶数的和 是"+sum);
    18. System.out.println("偶数的个数是"+count);
    19. }
    20. }
    1. /*
    2. 输入两个正整数m和n,求其最大公约数和最小公倍数。
    3. 比如:12和20的最大公约数是4,最小公倍数是60。
    4. */
    5. import java.util.Scanner;
    6. class ForTest02{
    7. public static void main(String[] args){
    8. Scanner scan = new Scanner(System.in);
    9. System.out.println("请输入一个正整数m");
    10. int m=scan.nextInt();
    11. System.out.println("请输入一个正整数n");
    12. int n=scan.nextInt();
    13. int maxDivisor=0;
    14. int minMultiple=0;
    15. int minNum = m
    16. int maxNum = m>=n?m:n;
    17. for(int i=minNum;i>0;i--){
    18. if(m%i==0&&n%i==0){
    19. System.out.println("最大公约数是"+i);
    20. break;
    21. }
    22. }
    23. // m*i==n*j
    24. for(int i=maxNum;i
    25. if(i%m==0&&i%n==0){
    26. System.out.println("最小公倍数是"+i);
    27. break;
    28. }
    29. }
    30. }
    31. }

    while

    格式 

    ①初始化部分
    while(②循环条件部分){
    ③循环体部分;
    ④迭代部分;
    }

    注意不要忘记声明④迭代部分。否则,循环将不能结束,变成死循环。
    for循环和while循环可以相互转换

    do...while

    格式
    ①初始化部分;
    do{
    ③循环体部分
    ④迭代部分
    }while(②循环条件部分);

    do-while循环至少执行一次循环体。

    1. /*
    2. 100以内的偶数
    3. 所有偶数的和
    4. 偶数的个数
    5. */
    6. class DoWhileTest{
    7. public static void main(String[] args){
    8. int i = 1;
    9. int sum=0;
    10. int count=0;
    11. do{
    12. if(i%2==0){
    13. System.out.println("100以内的偶数"+i);
    14. sum+=i;
    15. count++;
    16. }
    17. i++;
    18. }while(i<=100);
    19. System.out.println("所有偶数的和"+sum);
    20. System.out.println("偶数的个数"+count);
    21. }
    22. }
    1. /*
    2. 从键盘读入个数不确定的整数,并判断读入的正数和负数的个数,输入
    3. 为0时结束程序
    4. */
    5. import java.util.Scanner;
    6. class ForWhileTest{
    7. public static void main(String[] args) {
    8. Scanner scan = new Scanner(System.in);
    9. System.out.println("请输入数字");
    10. int positiveCount=0,negativeCount=0;
    11. do{
    12. int num = scan.nextInt();
    13. if(num>0){
    14. positiveCount++;
    15. }else if(num<0){
    16. negativeCount++;
    17. }else{
    18. break;
    19. }
    20. }while(true);
    21. System.out.println("正数的个数是"+positiveCount+" 负数的个数是"+negativeCount);
    22. }
    23. }

    循环嵌套:

    1. /*
    2. 100以内的所有质数
    3. 2:1 2
    4. 3:1 3
    5. 4:1 2 4
    6. */
    7. class PrimeNum2{
    8. public static void main(String[] args){
    9. //获取当前时间距离1970-01-01 00:00:00 的毫秒数
    10. long start = System.currentTimeMillis();
    11. boolean isFlag = true;
    12. int primeCount = 0;
    13. for(int i=2;i<=10000;i++){
    14. for(int j=2;j<=Math.sqrt(i);j++){
    15. if(i % j == 0){
    16. isFlag = false;
    17. break;// 优化一:对本身非质数的数有效的
    18. }
    19. }
    20. if(isFlag==true){
    21. primeCount++;
    22. }
    23. isFlag = true;
    24. }
    25. //获取当前时间距离1970-01-01 00:00:00 的毫秒数
    26. long end = System.currentTimeMillis();
    27. System.out.print("质数个数是"+primeCount);
    28. System.out.println("所花费的时间为:" + (end - start));
    29. }
    30. }

    break continue

    1. /*
    2. break和continue关键字的使用
    3.                 使用范围            循环中使用的作用(不同点)        相同点
    4. break:            switch-case            
    5.                 循环结构中            结束当前循环                    关键字后面不能声明执行语句    
    6. continue:        循环结构中            结束当次循环                    关键字后面不能声明执行语句
    7. */
    8. class BreakContinueTest {
    9.     public static void main(String[] args) {
    10.         for(int i = 1;i <= 10;i++){
    11.         
    12.             if(i % 4 == 0){
    13.                 break;//123
    14.                 //continue;//123567910
    15.                 //System.out.println("今晚迪丽热巴要约我!!!");
    16.             }
    17.             System.out.print(i);
    18.         }
    19.         System.out.println("\n");
    20.         //******************************
    21.         
    22.         label:for(int i = 1;i <= 4;i++){
    23.         
    24.             for(int j = 1;j <= 10;j++){
    25.                 
    26.                 if(j % 4 == 0){
    27.                     //break;//默认跳出包裹此关键字最近的一层循环。
    28.                     //continue;
    29.                     //break label;//结束指定标识的一层循环结构
    30.                     continue label;//结束指定标识的一层循环结构当次循环
    31.                 }
    32.                 
    33.                 System.out.print(j);
    34.             }
    35.             
    36.             System.out.println();
    37.         }
    38.     }
    39. }


     

     

  • 相关阅读:
    Spring Cloud Netflix之OpenFeign
    docker--知识点提炼
    大数据安全的重要性解读
    【java】3-获取线程引用与线程的属性
    基于Redis实现消息队列的实践
    授权调用: 介绍 Transformers 智能体 2.0
    docker 安装 redis 6.0.8 cluster 实战 (3主3从) 动态缩容
    Flutter(学前必看)基础
    Linux 日志系统
    企业邮箱选择指南:最适合跨境贸易的解决方案推荐
  • 原文地址:https://blog.csdn.net/hongc93/article/details/127616867