繁体   English   中英

Java简单程序不允许接受用户输入

[英]Java simple program not allowing to take user input

我正在尝试制作一个简单的Java程序,并且基本上是在获取一些用户输入值并将其打印在一个整齐有序的输出中。 这一切都在控制台中。 但是当我继续添加更多输入时,该程序只是不允许我接受输入。 这是我的代码:

import java.util.*;

public class output {
    public static void main(String arg[]) {

        //================================================================================
        // Level 1 Start
        //================================================================================
        Scanner input = new Scanner(System.in);

        //Start asking the user questions and store the values
        System.out.println("Hello! What is your name?");
        String name = input.nextLine();
        System.out.println("Hello " + name + "! Nice to meet you!");

        //================================================================================
        // Level 1 End
        //================================================================================


        //================================================================================
        // Level 2 Start
        //================================================================================
        System.out.println("How old are you?");
        String age = input.nextLine();

        System.out.println("Are you male or female?");
        String gender = input.nextLine();

        System.out.println("How much do you weigh?");
        int weight = input.nextInt();

        System.out.println("Are you a student? (true/false)");
        boolean isAStudent = input.nextBoolean();

        //Add a space
        System.out.println("\n");

        //Display the user's data neatly
        System.out.println("Name: " + name);
        System.out.println("Age: " + age);
        System.out.println("Gender: " + gender);
        System.out.println("Weight: " + weight);

        //check to see if the user is a student and print it out
        if(isAStudent) {
            System.out.println("Student?: Yes");        
        } else {
            System.out.println("Student?: No");
        }

        //================================================================================
        // Level 2 End
        //================================================================================

        //================================================================================
        // Level 3 Start
        //================================================================================

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

        //display hello world a bunch of times
        for(int i = 0; i < 5; i++){
            System.out.println("Hello Hello Hello Hello Hello Hello");
        }

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

        System.out.println("Tell me a quote");
        String quote = input.nextLine();

        System.out.print(quote);    

        //================================================================================
        // Level 3 End
        //================================================================================

    }
}

我知道我不应该在stackoverflow上放置大量代码,但是我觉得其他部分可能会导致解决方案。 所以我的问题是,当我到达第3级(检查注释)并到达显示“告诉我报价”的打印语句时,我无法在此之后进行输入。 因此,即使我没有输入,String引号的输入也表现为我按下了Enter键。 因此,在我什至无法键入任何内容之前就已经值了。 请帮助我...如果您需要更多说明,请告诉我。

在这之后,

boolean isAStudent = input.nextBoolean();

换行符保留在缓冲区中。 您应该摆脱它:

boolean isAStudent = input.nextBoolean();
input.nextLine();

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM