为什么忽略输入行?
Why is an input line ignored?
我在我的主应用程序中调用了一个方法,将一些 String
变量提供给 class 的构造函数,但这是一个问题:当我调用该方法时(虽然 运行 ning 程序),我的第一行输入被忽略了。这是我的代码:
这里调用了main中的方法
Rent r1=new Rent();
RentProcess(r1);
这是有问题的方法
public static void RentProcess(Rent r) {
System.out.println("Name and telephone : \n");
r.setNameAndTel(in.nextLine());
System.out.println("Rental date : \n");
r.setRentDate(in.nextLine());
}
这是我 运行 应用程序时的输入行。
Choose between :
1 --> Rent
2 --> Exit
1
Name and telephone :
Rental date :
20/2
当我打印上面的信息时。
Rent Code : 0
Name and Telephone
Date of Rent : 20/2
Days of Rent : 0
Rent cost is : 0.0
Name : The Wolf Of Wall Street
虽然你没有放完整的代码。但似乎您正在使用相同的扫描仪来读取导致问题的整数和字符串。
这一行尝试读取包含空 int 的行的剩余部分。
r.setNameAndTel(in.nextLine());
在这一行之前再添加一个 in.nextLine() 应该没问题。
我在我的主应用程序中调用了一个方法,将一些 String
变量提供给 class 的构造函数,但这是一个问题:当我调用该方法时(虽然 运行 ning 程序),我的第一行输入被忽略了。这是我的代码:
这里调用了main中的方法
Rent r1=new Rent();
RentProcess(r1);
这是有问题的方法
public static void RentProcess(Rent r) {
System.out.println("Name and telephone : \n");
r.setNameAndTel(in.nextLine());
System.out.println("Rental date : \n");
r.setRentDate(in.nextLine());
}
这是我 运行 应用程序时的输入行。
Choose between :
1 --> Rent
2 --> Exit
1
Name and telephone :
Rental date :
20/2
当我打印上面的信息时。
Rent Code : 0
Name and Telephone
Date of Rent : 20/2
Days of Rent : 0
Rent cost is : 0.0
Name : The Wolf Of Wall Street
虽然你没有放完整的代码。但似乎您正在使用相同的扫描仪来读取导致问题的整数和字符串。
这一行尝试读取包含空 int 的行的剩余部分。
r.setNameAndTel(in.nextLine());
在这一行之前再添加一个 in.nextLine() 应该没问题。