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

时间:2015-04-05 10:02:09

标签: java if-statement java.util.scanner println system.out

我的程序应该要求用户输入一些值,并且根据选择的菜单选项(在我的情况下是 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);

        }

    }

}

4 个答案:

答案 0 :(得分:2)

选择该选项并按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:");

        ...

答案 1 :(得分:1)

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

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

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

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

        accNum = sc.nextInt();

答案 2 :(得分:0)

name为null。如果用户选择了三个,你还没有实际为name变量赋值,那只有在他们选择1

时才会发生

答案 3 :(得分:0)

当您选择3选项时,您没有设置默认为name的{​​{1}}值,因此它正在打印null

如果您想要打印某些内容,则需要阅读并指定Your name: null值。