我试图创建一个简单的程序,要求用户输入三个项目,数量和价格。程序必须允许项目名称包含空格。这是我到目前为止编写的代码。
import java.util.Scanner;
public class AssignmentOne {
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
// System.out.printf("$%4.2f for each %s ", price, item);
// System.out.printf("\nThe total is: $%4.2f ", total);
//process for item one
System.out.println("Please enter in your first item");
String item = kb.nextLine();
System.out.println("Please enter the quantity for this item");
int quantity = kb.nextInt();
System.out.println("Please enter in the price of your item");
double price = kb.nextDouble();
//process for item two
System.out.println("\nPlease enter in your second item");
String item2 = kb.nextLine();
System.out.println("\nPlease enter the quantity for this item");
int quantity2 = kb.nextInt();
System.out.print("\nPlease enter in the price of your item");
double price2 = kb.nextDouble();
double total2 = quantity2*price2;
// System.out.printf("$%4.2f for each %s ", price2, item2);
// System.out.printf("\nThe total is: $%4.2f ", total2);
//process for item three
System.out.println("\nPlease enter in your third item");
String item3 = kb.nextLine();
System.out.println("Please enter the quantity for this item");
int quantity3 = kb.nextInt();
System.out.println("Please enter in the price of your item");
double price3 = kb.nextDouble();
double total3 = quantity3*price3;
// System.out.printf("$%4.2f for each %s ", price3, item3);
// System.out.printf("\nThe total is: $%4.2f ", total3);
double total = quantity*price;
double grandTotal = total + total2 + total3;
double salesTax = grandTotal*(.0625);
double grandTotalTaxed = grandTotal + salesTax;
String amount = "Quantity";
String amount1 = "Price";
String amount2 = "Total";
String taxSign = "%";
System.out.printf("\nYour bill: ");
System.out.printf("\n\nItem");
System.out.printf("%30s", amount);
// System.out.printf("\n%s %25d %16.2f %11.2f", item, quantity, price, total);
// System.out.printf("\n%s %25d %16.2f %11.2f", item2,quantity2, price2, total2);
// System.out.printf("\n%s %25d %16.2f %11.2f", item3,quantity3, price3, total3);
System.out.printf("\n%30s", item);
System.out.printf("%30d", quantity);
System.out.printf("\n%30s", item2);
System.out.printf("\n%30s", item3);
System.out.printf("\n\n\nSubtotal %47.2f", grandTotal);
System.out.printf("\n6.25 %s sales tax %39.2f", taxSign, salesTax);
System.out.printf("\nTotal %50.2f", grandTotalTaxed);
}
}
我的问题是,当我使用String item = kb.nextLine()时;这是输入项目时此过程的一个示例
Please enter in your first item
soda
Please enter the quantity for this item
10
Please enter the price for this item
15
Please enter in your second item
Please enter the quantity for this item
此时第一项很好,但接下来是第二项,它会自动输入第二项并直接移动到数量上,我不明白如何解决这个问题,我需要使用nextLine();所以项目名称可以有空格。请帮忙。
答案 0 :(得分:1)
试试此代码
import java.util.Scanner;
class AssignmentOne {
public static void main(String[] args)
{
Scanner kb = new Scanner(System.in);
// System.out.printf("$%4.2f for each %s ", price, item);
// System.out.printf("\nThe total is: $%4.2f ", total);
//process for item one
System.out.println("Please enter in your first item");
String item = kb.nextLine();
System.out.println("Please enter the quantity for this item");
int quantity = Integer.parseInt(kb.nextLine());
System.out.println("Please enter in the price of your item");
double price = Double.parseDouble(kb.nextLine());
//process for item two
System.out.println("\nPlease enter in your second item");
String item2 = kb.nextLine();
System.out.println("\nPlease enter the quantity for this item");
int quantity2 = Integer.parseInt(kb.nextLine());
System.out.print("\nPlease enter in the price of your item");
double price2 =Double.parseDouble( kb.nextLine());
double total2 = quantity2*price2;
// System.out.printf("$%4.2f for each %s ", price2, item2);
// System.out.printf("\nThe total is: $%4.2f ", total2);
//process for item three
System.out.println("\nPlease enter in your third item");
String item3 = kb.nextLine();
System.out.println("Please enter the quantity for this item");
int quantity3 = Integer.parseInt(kb.nextLine());
System.out.println("Please enter in the price of your item");
double price3 = Double.parseDouble(kb.nextLine());
double total3 = quantity3*price3;
// System.out.printf("$%4.2f for each %s ", price3, item3);
// System.out.printf("\nThe total is: $%4.2f ", total3);
double total = quantity*price;
double grandTotal = total + total2 + total3;
double salesTax = grandTotal*(.0625);
double grandTotalTaxed = grandTotal + salesTax;
String amount = "Quantity";
String amount1 = "Price";
String amount2 = "Total";
String taxSign = "%";
System.out.printf("\nYour bill: ");
System.out.printf("\n\nItem");
System.out.printf("%30s", amount);
// System.out.printf("\n%s %25d %16.2f %11.2f", item, quantity, price, total);
// System.out.printf("\n%s %25d %16.2f %11.2f", item2,quantity2, price2, total2);
// System.out.printf("\n%s %25d %16.2f %11.2f", item3,quantity3, price3, total3);
System.out.printf("\n%30s", item);
System.out.printf("%30d", quantity);
System.out.printf("\n%30s", item2);
System.out.printf("\n%30s", item3);
System.out.printf("\n\n\nSubtotal %47.2f", grandTotal);
System.out.printf("\n6.25 %s sales tax %39.2f", taxSign, salesTax);
System.out.printf("\nTotal %50.2f", grandTotalTaxed);
}
}
答案 1 :(得分:1)
因为您使用的是System.in
,所以在您点击"输入"之前,不会向扫描仪发送任何内容。如果你键入" 15"没有按下"输入",kb.nextDouble();
调用块。点击"输入"然后kb.nextDouble();
读取" 15",但扫描仪的缓冲区中仍然有换行符。这意味着代码的这一部分:
//process for item two
System.out.println("\nPlease enter in your second item");
String item2 = kb.nextLine();
立即从您输入" 15"中读取缓冲区中的换行符。然后点击"输入"。因此,它不会尝试读取项目名称。
您可以从以下位置替换所有商品名称扫描:
//process for item two
System.out.println("\nPlease enter in your second item");
String item2 = kb.nextLine();
为:
//process for item two
System.out.println("\nPlease enter in your second item");
String item2 = kb.next();
或者阅读换行符并在扫描价格时解析双线:
System.out.println("Please enter in the price of your item");
double price = Double.parseDouble(kb.nextLine());
答案 2 :(得分:0)
kb.nextDouble();
我需要检查,但看起来nextDouble
可能会保留换行符,然后由后续的`nextLine输入。
答案 3 :(得分:0)
您可以通过两种不同的方式解决此问题:
在代码中将.nextLine()
替换为.next()
。 (但这样做不会让你在字符串中有空格。
当您阅读基元然后阅读文本时,请在其后添加另一个.nextLine();
。
在您的情况下使用第二种方法,您将拥有:
System.out.println("Please enter in the price of your item");
double price = kb.nextDouble();
String item2 = kb.nextLine(); // -------------> Added this line also here
//process for item two
System.out.println("\nPlease enter in your second item");
item2 = kb.nextLine();
这将解决您遇到的第一个问题。为所有其他地方做同样的事情,你的程序将按照预期运行。
答案 4 :(得分:0)
使用不同的扫描仪对象获取字符串和数字。这解决了你的问题。