for循环之 跳出循环之 标签的灵活使用(Flag)

Flag活用

此数据管理系统使用了标签来实现以下功能:

1.当作if条件实现选择条件

例1

此段代码使用temp当作标签,实现在每次循环过程中,只要有一次数据添加操作得以实现,就不会执行数仓报错提示。

例2

此段代码使用flag当作标签,在 i = 0 ~ datas . length  的遍历过程中,只要触发删除,就不会再执行删除失败操作。

例3

退出功能设置break和标签K1break用于跳出本体所在的for,K1用来跳出上一层的for,以此来实跳转功能。

例4

int flag = 0

if (chose1==1){//登录
    b:for (;;){
        for (;;){
            c:for (;flag!=1;){
            System.out.println("Name:");
            String stuName = sc.next();
            System.out.println("Password:");
            String stupw = sc.next();
            for (int i = 0; i < 10; i++) {
                if (stuName.equals(sna)|stuName.equals(Namebox[i])){//对比用户名
                    System.out.println("Name Right !");
                    if (stupw.equals(spw)|stupw.equals(Pssswordbox[i])){//对比密码
                        System.out.println("Password Right ! Login success!");//进入系统
                        flag=1;
                        System.out.println("Welcome to StuSystem ! Please choise your block ! ( 1.User Block )( 2.Text Block)( 3. Quit )");
                        int chose2= sc.nextInt();
                        if (chose2==1){//用户管理
                            System.out.println("Please choise your Action ! ( 1.Change Password )( 2. Quit )");
                            int chose3= sc.nextInt();
                            if (chose3==1){
                                System.out.println("--修改密码界面--");
                                System.out.println("输入你想修改的密码:");
                                String changepw = sc.next();
                                for (int j = 0; j < Pssswordbox.length; j++) {
                                    if (Pssswordbox[j].equals(changepw)){
                                        System.out.println("输入修改后的密码:");
                                        String alreadychangepw = sc.next();
                                        Pssswordbox[j]=alreadychangepw;
                                        flag=0;
                                        break b;
                                    }
                                }
                            } else if (chose3==2) {
                                System.out.println("Do you wanna to Qqqquit?");
                                break;
                            }

                        } else if (chose2==2) {//考试管理
                            System.out.println("Exam begins!");
                            System.out.println("Exam over! waht do you annna do ? (1.Query grades )(2.Output grades )(3.Quit )");
                            int chose4= sc.nextInt();
                            if (chose4==1){//查询
                                System.out.println("Your grade is **** !");
                            } else if (chose4==2) {//导出
                                System.out.println("Output success !");
                            }else {//退出
                                System.out.println("Do you wanna to Quit ?");
                                break;
                            }

                        }else if (chose2==3){
                            System.out.println(" you wanna to Quit ?");
                            break a;
                        }
                    }else {
                        System.out.println("Password worng ! Try again!");//再次尝试
                        break c;
                    }
                }else if (flag!=1){
                    System.out.println("Name not exit ! 需要注册 ! ");
                    break b;
                }
            }
        }
      }

此处为登录功能,事先定义 flag = 0 ,因为登录时账号或密码不对需要重新输入,因此用到无限循环的 for( ; ;)。但是会导致成功登录后仍输出 "Name not exit ! 需要注册 ! " 顿

所以在此定义flag,只要成功登录即置 1 ,只有没有登陆过才会执行:

else if (flag!=1){

System.out.println("Name not exit ! 需要注册 ! ");

break b; }

此处即用 flag 跳出 for( ; ;)的无限非必要循环。

x:for

待续

  • 25
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值