计算Bill(Java的数组和执行循环)

时间:2014-02-09 19:46:54

标签: java arrays while-loop

我的在线实验室遇到了问题,我在下面给出了这个代码,我只能修改“// FIX ME”的地方。我已经在空白处添加了以下答案,但我仍然没有完全正确。我正在考虑在顶部编写另一个代码,询问我想要输入多少项,然后创建一个以此为中心的DO循环,但这不是我想要的问题。我可能只是以错误的方式看待这个问题,任何帮助都会受到赞赏!

这是实验室;

以下程序应输入项目清单,包括项目描述,购买物品数量和项目单价;然后计算总账单。当为描述输入“完成”时,列表的输入完成。完成程序以使其正常工作。

import java.util.Scanner;
    public class CalculateBill {
    public static void main( String[] args ) {
        double sum = 0;
        double cost;
        int items;
        double unitPrice;

        Scanner scan = new Scanner(System.in);
        System.out.println("Please enter the name of the item (Finish to end):");
        String description = scan.next();

        while( items != 0 ) { // FIX-ME
        System.out.println("Please enter the quantity of " + description + ": " );
        items = scan.nextInt();
        System.out.println("Please enter the unit price of " + description + ": ");
        unitPrice = scan.nextDouble();
        cost = Price++   ; // FIX-ME
        System.out.printf("Cost of %d %s(s) is $%.2f%n", items, description, cost);
        sum = sum+1; // FIX-ME
        System.out.println("Please enter the name of the item (Finish to end):");
        description = scan.next();
        }

        System.out.printf("The total bill is $%.2f%n" ???  ); // FIX-ME
}

}

1 个答案:

答案 0 :(得分:0)

这就是我为使其发挥作用所做的事情:

import java.util.Scanner;
public class CalculateBill {
public static void main( String[] args ) {
    double sum = 0;
    double cost = 0;
    int items=0;
    double unitPrice;

    Scanner scan = new Scanner(System.in);
    System.out.println("Please enter the name of the item (f to end):");
    // changed to f as I don't want to have to type Finish every time
    String description = scan.next();

    while( !description.equals("f") ) { // FIXED
    System.out.println("Please enter the quantity of " + description + ": " );
    items = scan.nextInt();
    System.out.println("Please enter the unit price of " + description + ": ");
    unitPrice = scan.nextDouble();
    cost = items*unitPrice  ; // FIXED
    System.out.printf("Cost of %d %s(s) is $%.2f%n", items, description, cost);
    sum += cost; // FIXED
    System.out.println("Please enter the name of the item (F to end):");
    description = scan.next();
    }

    System.out.printf("The total bill is $%.2f%n", sum); // FIXED
}

}
相关问题