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)

Scanner.next用于一个没有任何空格的单词。
之后的任何单词都将保存为下一个呼叫Scanner.next
而是使用Scanner.nextLine

street = scanner.nextLine();

最新更新