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中选择的是哪种大学课程,它都会在courseSelected文本区域中显示课程标题和价格,并在total文本字段中显示您的运行总计。问题是,我的runningTotal不会将这些值加在一起,并且似乎只是用新课程的价格替换了总文本字段中的值。
最佳答案
当前,您在循环内重置了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++)
{