总共 运行 无法正常工作
Can't get running total to work
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;
}
}
}
"register" 按钮的代码。当您单击它时,无论您在 JList 中选择了什么大学课程,它都会在 courseSelected 文本区域中显示课程名称和价格,并在总文本字段中显示您的 运行 总数。问题是,我的 运行Total 没有将这些值加在一起,它似乎只是用新课程的价格替换了总文本字段中的值。
目前您在循环内重置 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++)
{
替换这个,
for(int i = 0; i < cTitle.length; i++)
{
double runningTotal = 0;
来自
double runningTotal = 0;
for(int i = 0; i < cTitle.length; i++)
{
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;
}
}
}
"register" 按钮的代码。当您单击它时,无论您在 JList 中选择了什么大学课程,它都会在 courseSelected 文本区域中显示课程名称和价格,并在总文本字段中显示您的 运行 总数。问题是,我的 运行Total 没有将这些值加在一起,它似乎只是用新课程的价格替换了总文本字段中的值。
目前您在循环内重置 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++)
{
替换这个,
for(int i = 0; i < cTitle.length; i++)
{
double runningTotal = 0;
来自
double runningTotal = 0;
for(int i = 0; i < cTitle.length; i++)
{