这是我的代码 - 目标是为 x 名患者输入一些基本信息(年龄、姓名、性别)。
public static void main(String[] args) {
int numPatients = 2;
int[] age = new int[numPatients];
String[] gender = new String[numPatients];
String[] name = new String[numPatients];
Scanner in = new Scanner(System.in);
/*
* Obtaining patients details: name, gender, age
* First create a Scanner input variable to read the data
*/
for (int i = 0; i < numPatients; i++)
{
System.out.println("Enter name of patient " + (i+1));
name[i] = in.nextLine();
System.out.println("Enter gender (male/female) of patient " + (i+1));
gender[i] = in.nextLine();
System.out.println("Enter age of patient " + (i+1));
age[i] = in.nextInt();
}
我遇到的问题是当循环转到第二个变量时,即我无法输入患者姓名的值。它似乎跳过了那里的输入并直接进入下一个输入,即性别。
Enter name of patient 1
Mark
Enter gender (male/female) of patient 1
Male
Enter age of patient 1
34
Enter name of patient 2 //Skipped. Could not enter input here
Enter gender (male/female) of patient 2
Jenna
知道为什么会这样吗?改用 BufferedReader 会更好吗?