【问题标题】:How to keep a total with an array?如何用数组保持总数?
【发布时间】:2014-03-21 20:40:30
【问题描述】:

问题:编写一个创建两个并行数组的程序。第一个是一个字符串数组,其中包含杂货清单中的项目名称。第二个是每个项目的价格数组。必须满足以下条件:

  1. 打印带有价格的第一个数组中的商品编号列表。
  2. 要求用户选择一个项目并指定一个数量。
  3. 跟踪他们的购物总金额。
  4. 当他们输入0 退出时,打印到期总金额。

我真的对数组感到困惑,并且无法计算总数。任何帮助是极大的赞赏。

这是我目前所拥有的......

 public static void main(String[] args) {
    Scanner in = new Scanner(System.in);
    System.out.println("Items on the list");
    String[] list = {"eggs", "milk", "chicken", "cereal"};
    double[] prices = {2.00, 2.50, 4.50, 1.00};
    for (int i = 0; i < 4; i++) {
        System.out.println((i + 1) + ". " + list[i] + " " + prices[i]);
    }

    System.out.println("What item do you want");
    int item1 = in.nextInt();
    System.out.println("What quantity");
    int quantity1 = in.nextInt();
    double total1;
    double total2;
    double total = 0;
    while (item1 != 0) {
        System.out.println("Would you like another item");
        int item2 = in.nextInt();
        if (item2 == 0) {
            System.out.println(total);
            break;
        }
        System.out.println("What quantity?");
        int quantity2 = in.nextInt();
        total1 = (prices[item1 - 1] * quantity1);
        total2 = (prices[item2 - 1] * quantity2);
        total = total1 + total2;
    }
}

}

【问题讨论】:

  • 你的具体问题是什么?
  • 要计算总数,请将所有值相加。要保持“运行总计”,您只需在旁边放一个值,然后在插入值时将插入到数组中的每个值添加到该值中。
  • 在上面的代码中,你已经把循环搞砸了。您需要弄清楚如何合并“第一项”和“下一项”路径,以便您只做一次。您不需要,例如数量 1 和数量 2,只需要数量。

标签: java arrays


【解决方案1】:

啊,你好近啊。您不需要跟踪 2 个项目,只需跟踪一个。此外,您希望每次迭代都添加到总数中,而不是完全覆盖它。目前,您的总变量包含第一个项目的价格 * 数量加上输入的 最后一个 项目。

System.out.println( "What do you want?" );
int item = in.nextInt();
double total = 0;

while( item != 0 )
{

    System.out.println( "How many?" );
    int quant = in.nextInt();

    // below is equiv to total += prices[item-1] * quant;
    total = total + (prices[item-1] * quant); // add to the total
                                              // initially it is 0, so after the first
                                              // iteration it is the cost of the first item*quantity.
                                              // and then keeps adding the new item*quantity to it.

    // notice this is done at the end of the loop so that the next statement that
    // gets executed is whether to continue or not.
    System.out.println( "Which item?" );
    item = in.nextInt();
}

【讨论】:

    【解决方案2】:

    开头看起来是正确的,但是您将 while 循环弄乱了一点。我只是做了一个草图来向您展示它的外观(未经测试):

    double total = 0;
    do {
      System.out.println("What item do you want?");
      int item = in.nextInt();
      if (item == 0)
        break;
      System.out.println("How many do you want?");
      int quantity = in.nextInt();
      total += quantity * prices[item - 1];
    } while(true);
    System.out.println("You have to pay " + total);
    

    【讨论】:

      猜你喜欢
      • 2019-05-18
      • 1970-01-01
      • 1970-01-01
      • 2020-03-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-18
      • 2012-07-28
      相关资源
      最近更新 更多