case 5:
System.out.println("**************积分抽奖**************");while(true) {
System.out.println("请选择 1.抽奖 2.退出");
int choose2 = input.nextInt();
switch (choose2) {
case 1:
String lottery = userDao.lottery(loginAccount);
System.out.println(lottery);
break;
case 2:
break;
default:
System.out.println("输入有误");
break;
}
}
switch中的break只能终止switch循环,无法终止while循环,如果将break改成return,虽然能终止循环,但是会用力过猛,将整个方法都终止,如何做到精准的终止掉当前while循环,我们可以在外面定义一个boolean变量flag来控制while循环,在case中,通过改变flag的值来控制while循环.
1 case 5:
2 System.out.println("**************积分抽奖**************");
3 boolean flag = true;
4 while(flag) {
5 System.out.println("请选择 1.抽奖 2.退出");
6 int choose2 = input.nextInt();
7 switch (choose2) {
8 case 1:
9 String lottery = userDao.lottery(loginAccount);
10 System.out.println(lottery);
11 break;
12 case 2:
13 flag = false;
14 break;
15 default:
16 System.out.println("输入有误");
17 break;
18 }
19 }