在我的当前程序中,一个方法要求用户输入产品描述作为 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;
}
如果用户输入为“带有橙味的苏打水”,那么 System.out.print
只会输出 "Sparkling"。
感谢任何帮助!
nextLine()
调用之前有任何next()
调用,这将失败。正确的答案不是使用nextLine
(你很少需要nextLine
);相反,修复你的扫描器的分隔符:在其创建后立即调用.useDelimiter("\r?\n");
。 - rzwitserloot