我有一项作业要求我们输入我们最喜欢的餐厅和地址。我无法在同一行打印出地址。
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();
关于java餐厅地址显示不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42369464/