Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.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中,如何添加每个循环的结果?_Java_Loops_For Loop - Fatal编程技术网

在Java中,如何添加每个循环的结果?

在Java中,如何添加每个循环的结果?,java,loops,for-loop,Java,Loops,For Loop,我让这个程序工作,除了 if (more == JOptionPane.NO_OPTION) { System.out.print("\nTotal profit/loss: $"); System.out.print(profitandloss); } 在程序结束时,它将只显示最终循环的结果,而不是将所有循环相加。例如,如果每个循环的利润是8,如果有4个循环,那么总数应该是32,但它只显示8。有没有办法解决这个问题 String productcod

我让这个程序工作,除了

if (more == JOptionPane.NO_OPTION)       
{
    System.out.print("\nTotal profit/loss: $");      
    System.out.print(profitandloss);
}
在程序结束时,它将只显示最终循环的结果,而不是将所有循环相加。例如,如果每个循环的利润是8,如果有4个循环,那么总数应该是32,但它只显示8。有没有办法解决这个问题

String productcode = null, purchased, cost, sold, salesprice, numberproducts = null;

double number = 1;
double profitandloss = 0;
int more;

System.out.println("Product code    units purchased  unit cost   units sold   units available   sales price  profit/loss");

double money;

for (int index = 1; index <= number; index++)
{
    productcode = JOptionPane.showInputDialog("Please enter the product code");
    int code = Integer.parseInt(productcode);

    purchased = JOptionPane.showInputDialog("Please enter the amount purchased");
    double unitspurchased = Double.parseDouble(purchased);

    cost = JOptionPane.showInputDialog("Please enter the cost of this item");
    double unitcost = Double.parseDouble(cost);

    sold = JOptionPane.showInputDialog("Please enter how many these items were sold");
    double unitssold = Double.parseDouble(sold);

    salesprice = JOptionPane.showInputDialog("Please enter the sales price for this item");
    double price = Double.parseDouble(salesprice);

    double available = unitspurchased - unitssold;
    profitandloss = unitssold*(price - unitcost);

    System.out.printf("P %2d %18.2f %18.2f %12.2f %12.2f %15.2f %15.2f", code, unitspurchased, unitcost, unitssold, available, price, profitandloss);
    System.out.println("");
    more= JOptionPane.showConfirmDialog(null, "Do you wish to enter any more products?", numberproducts, JOptionPane.YES_NO_OPTION);

    if (more == JOptionPane.YES_OPTION)
    {
        number++;
    }
    if (more == JOptionPane.NO_OPTION)
    {
        System.out.print("\nTotal profit/loss: $");
        System.out.print(profitandloss);
    }
}
改变

或同等地

profitandloss += unitssold*(price - unitcost);
您遇到此问题的原因是,您没有通过每次添加profitandloss来累积最终答案,而是每次都用当前结果覆盖profitandloss,因此最终只打印最新的结果。

更改

profitandloss = unitssold*(price - unitcost);

或同等地

profitandloss += unitssold*(price - unitcost);
您遇到此问题的原因是,您不是通过每次添加profitandloss来累积最终答案,而是每次都用当前结果覆盖profitandloss,因此最后您只能打印最新的结果。

您应该替换

profitandloss = unitssold*(price - unitcost);

您在每次迭代时都会覆盖profitandloss。

您应该替换

profitandloss = unitssold*(price - unitcost);


您在每次迭代时都会覆盖profitandloss。

或者更好地使用StringBuilder:或者更好地使用StringBuilder: