Kri*_*ian 14 java string java.util.scanner
在我当前的程序中,一种方法要求用户输入产品的描述作为String
输入.但是,当我后来尝试打印出这些信息时,只String
显示了节目的第一个字.可能是什么原因造成的?我的方法如下:
void setDescription(Product aProduct) {
Scanner input = new Scanner(System.in);
System.out.print("Describe the product: ");
String productDescription = input.next();
aProduct.description = productDescription;
}
Run Code Online (Sandbox Code Playgroud)
因此,如果用户输入的是"带有橙味的苏打汽水",System.out.print
则只会产生"闪闪发光".
任何帮助将不胜感激!
Tim*_*per 26
替换next()
为nextLine()
:
String productDescription = input.nextLine();
Run Code Online (Sandbox Code Playgroud)
Scanner 的 javadoc回答了您的问题
扫描器使用分隔符模式将其输入分解为标记,默认情况下匹配空格。
您可以通过执行类似的操作来更改扫描仪使用的默认空白模式
Scanner s = new Scanner();
s.useDelimiter("\n");
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
164402 次 |
最近记录: |