在Java中使用scanner方法进行简单计算

在Java中使用scanner方法进行简单计算,java,Java,我试图创建一个简单的程序,要求用户输入三个项目,它们的数量和价格。程序必须允许项目名称包含空格。这是我迄今为止编写的代码 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 ea

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

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

kb.nextDouble();

我需要检查,但看起来,
nextDouble
很可能保留了完整的换行符,然后由后续的“nextLine”输入。

您可以用两种不同的方法解决此问题:

  • 将代码中的
    .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(); 
这将解决您遇到的第一个问题。对所有其他位置进行相同的操作,您的程序将按预期运行。

尝试此代码

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



}
}

因为您使用的是
系统。在
中,在您点击“回车”之前,不会向扫描仪发送任何信息。这意味着如果您在不点击“回车”的情况下键入“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();
致:

或者在扫描价格时,阅读换行符并解析double:

System.out.println("Please enter in the price of your item");
double price = Double.parseDouble(kb.nextLine());

或者使用不同的扫描程序对象获取字符串和数字。这就解决了您的问题。

@marounnaroun否,如果项目名称有空格(扫描程序的默认分隔符),则
next()
将不会解析整行。如果项目名称中有空格,则这将不起作用。
System.out.println("Please enter in the price of your item");
double price = Double.parseDouble(kb.nextLine());