2017-02-21 109 views
0

我有一項任務,要求我們輸入我們最喜歡的餐廳和地址。我無法在同一行上獲得打印出來的地址。java餐廳地址顯示不正確

import java.util.Scanner; 

public class Restaurant { 
    public static void main(String[] args) { 
    Scanner user_input = new Scanner(System.in); 

String name; 
System.out.print("Enter your Favorite Restaurant: "); 
name = user_input.next(); 

String street; 
System.out.print("Enter the Street Address: "); 
street = user_input.next(); 

String city; 
System.out.print("Enter the City: "); 
city = user_input.next(); 

String state; 
System.out.print("Enter the State: "); 
state = user_input.next(); 

String zip; 
System.out.print("Enter the Zip Code: "); 
zip = user_input.next(); 


String restaurant = name + "\n" + city + ", " + state + ", " + " " " + zip;" 
+ ""; 


} 
} 

我得到一個輸出,不讓我輸入正確的整個地址。必須有一個更簡單的方法來做到這一點。

Enter your Favorite Restaurant: Uncle Bubs 
Enter the Street Address: Enter the City: 444 Spender St 
Enter the State: Enter the Zip Code: BUILD SUCCESSFUL (total time: 19  seconds) 
+0

舉一個例子輸入和輸出,所以我們可以明白你的意思 – jgr208

+0

逃亡'「'爲」 \那麼它可能工作。如果你想在同一行上,那麼刪除'\ n'。使用'System.out.println' – Nilesh

+0

@Nilesh:我想你的意思是'\「' – sinclair

回答

2

Scanner.next用於一個單詞沒有任何空格。
之後的任何單詞將保存下一個電話Scanner.next
相反,使用Scanner.nextLine

street = scanner.nextLine();