Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
计算账单(Java的数组和Do循环)_Java_Arrays_While Loop - Fatal编程技术网

计算账单(Java的数组和Do循环)

计算账单(Java的数组和Do循环),java,arrays,while-loop,Java,Arrays,While Loop,我的在线实验室有问题,下面给了我这个代码,我只能修改上面写着“//修复我”的地方。我已经在空白处添加了以下答案,但我仍然没有完全正确的答案。我在考虑在上面写另一个代码,询问我想输入多少项,然后围绕这个问题创建一个DO循环,但这不是问题想要我做的。有可能我只是看错了,任何帮助都将不胜感激 这是实验室 以下程序应输入项目清单,包括项目说明、购买的项目数量和项目单价;然后计算账单总额。当为描述输入“Finish”时,列表的输入完成。完成程序,使其正常工作 import java.util.Scanne

我的在线实验室有问题,下面给了我这个代码,我只能修改上面写着“//修复我”的地方。我已经在空白处添加了以下答案,但我仍然没有完全正确的答案。我在考虑在上面写另一个代码,询问我想输入多少项,然后围绕这个问题创建一个DO循环,但这不是问题想要我做的。有可能我只是看错了,任何帮助都将不胜感激

这是实验室

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

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
}

}

这就是我为让它工作所做的:

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
}

}

关于这项任务,你还不了解什么?你如何手工计算这张账单?这与代码所做的有何不同?(这项作业的重点是让你分析代码,而不是我们。)回答这类问题就是为了这样。当@keshlam总结得很好时,我会特别传递它。我对DO-While循环感到困惑,在这么多输入之后,我如何使它停止。未设置输入,它可以是任意数量的变量。对于@keshlam,正如我已经说过的;我已经分析过了,我被卡住了。至于@Orel Eraki;我感谢你的帮助!谢谢,这回答了我的问题。非常感谢!没问题。我刚刚编辑了第三个FIX-ME:它做同样的事情,但使用了一条捷径。