Java SE入门及基础(15)

Java 中的标号(标签 label

1. 语法规则

标号名称 : 循环结构

2. 作用

        标号的作用就是给代码添加一个标记,方便后面使用。通常应用在循环结构中,与break 语句配合使用

3. 应用场景

有如下菜单:

实现其中返回主菜单的功能

4. 代码实现

public static void main ( String [] args ) {
        Scanner sc = new Scanner ( System . in );
        while ( true ){
                System . out . println ( "=========================" );
                System . out . println ( "1. 学生成绩管理 " );
                System . out . println ( "2. 学生选课管理 " );
                System . out . println ( "3. 退出系统 " );
                System . out . println ( "=========================" );
                System . out . println ( " 请选择菜单编号: " );
                int menuNo = sc . nextInt ();
                if ( menuNo == 1 ){
                        childMenu : while ( true ){
                                System . out . println ( "**************************" );
                                System . out . println ( "1. 添加成绩 " );
                                System . out . println ( "2. 查看成绩 " );
                                System . out . println ( "3. 修改成绩 " );
                                System . out . println ( "4. 删除成绩 " );
                                System . out . println ( "5. 返回主菜单 " );
                                System . out . println ( "**************************" );
                                System . out . println ( " 请选择菜单编号: " );
                                int number = sc . nextInt ();
                                switch ( number ){
                                        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 childMenu ; //java 中的标号,可以理解为一个代码的标记
                                        }
                                }
                        } else if ( menuNo == 2 ){
                        } else {
                                System . out . println ( " 感谢使用本人开发的系统 " );
                                break ; // 终止 break 所在的循环
                        }
                }
        }

更多Java SE文章参考:Java SE入门及基础知识合集-CSDN博客

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值