我的程序要求控制檯一次輸入用戶名和年齡。如圖所示,我正在使用BufferedReader。正如你所看到的,addPassenger需要兩個輸入,名字和年齡......但我只能說出名字。如何使用BufferedReader實現?換句話說,我怎樣才能使「screenInput.readLine();」把名字和年齡都作爲輸入字符串?任何幫助讚賞。我該如何讓BufferedReader一次接受多個輸入?
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;
你怎麼分開呢?空間?輸入? – Hackerdarshi
我用Enter將它們分開。 –