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
//================================================================================
}
}
我知道我不应该将大量代码放在 Whosebug 上,但我觉得其他部分可能会导致解决方案。所以我的问题是,当我到达第 3 级(检查注释)并到达显示 "Tell me a quote" 的打印语句时,我无法为之后的行输入。因此,即使我没有按下回车键,字符串引号的输入就好像我按下了回车键一样。因此,它在我什至能够键入任何内容之前就已经获取了值。请帮助我...如果您需要更多解释,请告诉我。
在此之后,
boolean isAStudent = input.nextBoolean();
一个换行符保留在缓冲区中。你应该摆脱它:
boolean isAStudent = input.nextBoolean();
input.nextLine();
我正在尝试制作一个简单的 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
//================================================================================
}
}
我知道我不应该将大量代码放在 Whosebug 上,但我觉得其他部分可能会导致解决方案。所以我的问题是,当我到达第 3 级(检查注释)并到达显示 "Tell me a quote" 的打印语句时,我无法为之后的行输入。因此,即使我没有按下回车键,字符串引号的输入就好像我按下了回车键一样。因此,它在我什至能够键入任何内容之前就已经获取了值。请帮助我...如果您需要更多解释,请告诉我。
在此之后,
boolean isAStudent = input.nextBoolean();
一个换行符保留在缓冲区中。你应该摆脱它:
boolean isAStudent = input.nextBoolean();
input.nextLine();