我的程序要求控制台一次输入用户名和年龄。我正在使用BufferedReader,如图所示。正如您所看到的,addPassenger接受两个输入,名称和年龄......但我只能放入名称。如何使用BufferedReader实现?换句话说,我怎样才能制作“screenInput.readLine();”将名称和年龄都作为输入字符串?任何帮助表示赞赏。
public class Console {
public static void main(String[] args) {
// Initialize database
Database prodDB = new Database();
prodDB.bootstrap();
//Initialize console
boolean always = true;
BufferedReader screenInput = new BufferedReader(new InputStreamReader(System.in));
while(always){
//ask for passengerName and age, then add
System.out.println("Enter Passenger Name and Age: ");
String name = screenInput.readLine();
boolean result = prodDB.addPassenger(name, age);
if (result){
System.out.println("Welcome back " + name);
} else
{
System.out.println("Welcome " + name);
}
always = false;
答案 0 :(得分:1)
您可以添加另一行来获取年龄,如下所示:
String name = screenInput.readLine();
int age = Integer.parseInt(screenInput.readLine());