Flag活用
此数据管理系统使用了标签来实现以下功能:
1.当作if条件实现选择条件
例1
此段代码使用temp当作标签,实现在每次循环过程中,只要有一次数据添加操作得以实现,就不会执行数仓报错提示。
例2
此段代码使用flag当作标签,在 i = 0 ~ datas . length 的遍历过程中,只要触发删除,就不会再执行删除失败操作。
例3
退出功能设置break和标签K1,break用于跳出本体所在的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
待续