为什么 System.out.println 无法打印出字符串值(在我的特定情况下)?

Why System.out.println can't print out a string value (in my particular case)?

我的程序应该要求用户输入一些值,最终这些值应该根据选择的菜单选项打印出来(在我的例子中是 3)。我已经使用 System.out.println("Your name: " + name); 打印出用户输入的名称,但不幸的是它无法打印出该名称。该行只是空着。为什么这样?我该如何解决它。

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);

    int userChoose;
    String name = null;
    int accNum = 0;
    double initiateAmount = 0;
    double newAmm = 0;

    double depOrWith = 0;
    System.out.println("WELCOME TO OUR BANK!\n\n"
            + "...................\n"
            + "...................\n\n"
            + "Choose your optiin:\n"
            + "1. Create new account\n"
            + "2. Deposit/withdraw\n"
            + "3. View details\n"
            + "4. Deleting an account\n"//not used yet
            + "5. View all the accounts\n"//not used yet
            + "6. Quit\n\n");
    System.out.println("*************\n"
            + "************");
    while (true) {
        userChoose = sc.nextInt();

        if (userChoose == 1) {

            System.out.println("Enter your full name:");

            name = sc.nextLine();
            sc.nextLine();

            System.out.println("Choose an account number:");

            accNum = sc.nextInt();

            System.out.println("Enter an initiating amount:");

            initiateAmount = sc.nextDouble();
            System.out.println("\n-----------\n"
                    + "------------");
        } else if (userChoose == 2) {

            System.out.println("Enter negative value to withdraw and positive to deposit");
            depOrWith = sc.nextInt();
            if (depOrWith < 0) {

               initiateAmount = initiateAmount + depOrWith;
            } else {

               initiateAmount = initiateAmount + depOrWith;
            }

            System.out.println("\n-----------\n"
                    + "------------");

        } else if (userChoose == 3) {

            System.out.println("Your name: " + name);
            System.out.println("Your account number: " + accNum);
            System.out.println("Your current balance: " + initiateAmount);
            System.out.println("\n-----------\n"
                    + "------------");
        } else if (userChoose == 6) {

            System.exit(0);

        }

    }

}

名称为空。如果用户选择了三个,那么您实际上还没有为 name 变量赋值,只有当他们选择 1

时才会发生这种情况

选择选项并按 Enter 后,Scanner 未读取换行符。之后当 name = sc.nextLine(); 被调用时,它只会读取所选选项之后的新行,并且 name 将被分配空字符串。要解决这个问题,只需在读取所选选项后添加一个调用nextLine,并在读取名称时删除重复的nextLine

while (true) {
    userChoose = sc.nextInt();
    sc.nextLine();

    if (userChoose == 1) {

        System.out.println("Enter your full name:");

        name = sc.nextLine();

        System.out.println("Choose an account number:");

        ...

当您 select 选项 3 时,您没有设置默认为 nullname 值,因此它正在打印 Your name: null

如果要打印某些内容,您需要读取并分配 name 值。

交换空白nextLine()和指定的。

System.out.println("Enter your full name:");

        sc.nextLine();
        name = sc.nextLine();

        System.out.println("Choose an account number:");

        accNum = sc.nextInt();