我的作业要求我们输入我们最喜欢的餐厅和地址。我无法将地址打印在同一行。
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 :(得分:2)
Scanner.next
用于一个单词,没有任何空格
之后的任何字词都会保存,以便下次拨打Scanner.next
相反,请使用Scanner.nextLine
:
street = scanner.nextLine();