无法运行总计工作

时间:2015-12-03 07:54:37

标签: java running-total

if(e.getSource() == register)
  {

     String registerIndex = coursesAvailable.getSelectedValue().toString();
     for(int i = 0; i < cTitle.length; i++)
     {
        double runningTotal = 0;

        if(cTitle[i].equals(registerIndex))
        {
           runningTotal += cPrice[i];

           cSeatsAvail[i] = cSeatsAvail[i] - 1;
           seatsAvailable.setText(String.valueOf(cSeatsAvail[i]));

           courseSelected.replaceSelection(cTitle[i] + " === $" + cPrice[i] + "\n");
           total.setText("Total: $" + runningTotal);
           break;
        }
        else
        {
           continue;
        }
     }
  }

“注册”按钮的代码。当您单击它时,无论您在JList中选择的大学课程,它都会在课程选择的文本区域中显示课程标题和价格,并在总文本字段中显示您的总计课程。问题是,我的runningTotal没有将值一起添加,它似乎只是将总文本字段中的值替换为新课程的价格。

2 个答案:

答案 0 :(得分:6)

目前你在循环中重置 runningTotal - 所以它不起作用:

for(int i = 0; i < cTitle.length; i++)
  {
    double runningTotal = 0; // Resets every iteration!!!

尝试将其移出循环:

double runningTotal = 0; //Init once before the loop !
for(int i = 0; i < cTitle.length; i++)
  {

答案 1 :(得分:2)

替换它,

for(int i = 0; i < cTitle.length; i++)
     {
        double runningTotal = 0;

通过

 double runningTotal = 0;
for(int i = 0; i < cTitle.length; i++)
     {