Java SE入门及基础(15)

Java 中的标号(标签 label

1. 语法规则

标号名称 : 循环结构

2. 作用

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

3. 应用场景

有如下菜单:

Java SE入门及基础(15)_第1张图片

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

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博客

 

你可能感兴趣的:(算法,java,开发语言)