0

我正在尝试创建一个简单的程序,要求用户输入三个项目、它们的数量和价格。程序必须允许项目名称包含空格。这是我到目前为止编写的代码。

   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() ; 所以项目名称可以有空格。请帮忙。

4

5 回答 5

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);



}
}
于 2013-01-19T09:10:26.790 回答
1

因为您正在使用System.in,所以在您点击“输入”之前,不会向扫描仪发送任何内容。这意味着如果您键入“15”而不按“回车”,则kb.nextDouble();呼叫会阻塞。当你点击“enter”然后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());
于 2013-01-19T09:11:35.650 回答
0
kb.nextDouble();

我需要检查,但它看起来很可能使nextDouble换行符完好无损,然后由后续的 `nextLine.

于 2013-01-19T08:58:23.893 回答
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(); 

这将解决您遇到的第一个问题。对所有其他地方进行相同的操作,您的程序将按预期运行。

于 2013-01-19T09:09:06.417 回答
0

使用不同的扫描仪对象来获取字符串和数字。这解决了你的问题。

于 2013-01-19T09:35:15.857 回答