为什么我在while循环中使用switch时printn不输出,而去掉while循环之后就能输出

代码如下:

import java.util.*;

public class day730 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int choice = in.nextInt();
        int a = 0;
        switch (choice) {
            case 1:
                System.out.println("999");
                break;
            case 2:
                System.out.println("9999999");
                break;
            default:
                System.out.println("please enter again");
                a = 1;
                break;
        }

        in.close();
    }
}


这是不带循环的,能正常输出999 或9999999


import java.util.*;

public class day730 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        int choice = in.nextInt();
        int a = 0;
        while (a == 1) {
            switch (choice) {
                case 1:
                    System.out.println("999");
                    break;
                case 2:
                    System.out.println("9999999");
                    break;
                default:
                    System.out.println("please enter again");
                    a = 1;
                    break;
            }
        }
        in.close();
    }
}

这是带循环的,运行输入1或2都只是输出一个空行就结束了程序

代码在vscode上用coderunner运行的,本人刚开始学习Java 轻点喷,ballball各位了

int a = 0;
while (a == 1)
不满足while条件,不进入while循环

img
定义变量a = 0 循环条件是a==1的时候才能继续往里进行,循环进不去,换成如下注意代码逻辑:

package Test;

import java.util.*;

public class day730 {
    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);
        while (true) {
            int choice = in.nextInt();
            switch (choice) {
            case 1:
                System.out.println("999");
                break;
            case 2:
                System.out.println("9999999");
                break;
            default:
                System.out.println("please enter again");
                return;
            }
        }
        in.close();
    }
}