• 4. Java程序控制结构



    程序运行的三大流程控制语句:
    (1)顺序控制
    (2) 分支控制
    (3) 循环控制

    1. 顺序控制

    程序从上到下逐行地执行,中间没有任何判断和跳转。
    注意事项:
    Java中定义变量时采用合法的前向引用。如::

    int num1 = 12;
    int num2 = num1 + 2;
    
    • 1
    • 2

    错误形式:

    int num2 = num1 + 2; //错误
    int num1 = 12;
    
    • 1
    • 2

    2. 分支控制

    2.1 if-else

    让程序有选择的的执行,分支控制有三种:
    (1) 单分支 if
    (2) 双分支 if-else
    (3) 多分支 if-else if -…-else

    2.2 switch

    switch(表达式){
    	case 常量1:
    		语句块1;
    		break;
    	case 常量2;
    		语句块2;
    		break;
    	case 常量n;
    		语句块n;
    		break;
    	default:
    		default 语句块:
    		break;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 表达式数据类型,应和case后的常量类型一致,或者是可以自动转成可以相互比较的类型,比如输入的是字符,而常量是int。
    • 表达式的返回值必须是byte,short,int,char,enum[枚举],String中的一个。
    double c = 1.1;
    switch(c){//错误
    	case 1.1 : //错误
    	System.out.println( "ok3" );
    	break;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • case子句中的值必须是常量,而不能是变量。
    • default子句是可选的,当没有匹配的case时,执行default。
    • break语用来在执行完一个case分支后,使程序跳出switch语句块; 如果没有写break,程序会顺序执行到switch结尾。

    练习:
    (1)使用 switch 把小写类型的 char 型转为大写(键盘输入)。只转换 a, b, c, d, e. 其它的输出"other"

    char ch;
    Scanner input = new Scanner(System.in);
    System.out.print("请输入一个字符:");
    ch = input.next().charAt(0);//从键盘接收一个字符
    switch(ch){
    	case 'a':
    		System.out.print('A');
    		break;
    	case 'b':
    		System.out.print('B');
    		break;
    	case 'c':
    		System.out.print('C');
    		break;
    	case 'd':
    		System.out.print('D');
    		break;
    	default:
    		System.out.print("other");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20

    (2)对学生成绩大于 60 分的,输出"合格"。低于 60 分的,输出"不合格"。
    这里需要进行一个转换: 如果成绩在 [60,100] , (int)(成绩 / 60) = 1;如果成绩在 [0,60) , (int)(成绩 / 60) = 0。

    Scanner myScanner = new Scanner(System.in);
    System.out.println("请输入成绩");
    double score = myScanner.nextInt();
    switch((int)(score / 60)){
    	case 1:
    		System.out.println("及格");
    		break;
    	
    	case 0:
    		System.out.println("不及格");
    		break;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    (3)根据用于指定月份,打印该月份所属的季节。3,4,5 春季; 6,7,8 夏季; 9,10,11 秋季; 12, 1, 2 冬季。(使用穿透)

    Scanner myScanner = new Scanner(System.in);
    System.out.println("输入月份");
    int month = myScanner.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 1:
    	case 2:
    	case 12:
    		System.out.println("这是冬季");
    		break;
    	default :
    		System.out.println("你输入的月份不对(1-12)");
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27

    2.3 if-else 与 switch 的比较

    • 如果判断的具体数值不多,而且符合 byte、 short 、int、 char, enum[枚举], String 这6 种类型。虽然两个语句都可以使用,建议使用 swtich 语句。
    • 其他情况:对区间判断,对结果为 boolean 类型判断,使用 if,if 的使用范围更广。

    3. 循环控制

    3.1 for循环

    3.2 while循环

    3.3 do-while循环

    3.4 多重循环

    3.5 break、continue 和 return

  • 相关阅读:
    有效的单元测试
    List,Set,Map集合总结
    十个最为戳心测试/开程序员笑话,念茫茫人海,该如何寻觅?
    Linux下安装Redis
    d3.js 的使用
    【排序算法】快速排序
    分布式锁:不同实现方式实践测评
    软考 系统架构设计师系列知识点之边缘计算(1)
    东方通中间件弱密码漏洞检测方法
    create-react-app v5 打包配置(部署到非根目录)
  • 原文地址:https://blog.csdn.net/qq_44378854/article/details/125615865