【发布时间】:2020-06-11 02:31:40
【问题描述】:
我正在为我的大学作业做一个项目,我正在努力准确地找到我的项目的平均歌曲时长。
我输入了 3:33 的持续时间 3 次,并从 arrayList 获取输入,它应该返回 3:33 的结果。但是,我得到的结果是 3:19。
我不太确定我哪里出错了,我已经尝试在这里和那里进行更改,但仍然得到相同的结果。
public void display()
{
String durationTemp = "";
String durationStr = "";
double durationDbl = 0;
double durationTotal = 0;
double durationAvg = 0;
double time = 0;
int minutes = 0;
int seconds = 0;
if (songCount > 0)
{
displayHeading();
for (int i = 0; i < songCount; i++)
{
displayTextArea.append(songsArrayList.get(i).toString());
appendLine();
durationTemp = songsArrayList.get(i).getDuration();
durationStr = durationTemp.replace(':','.');
durationDbl = Double.parseDouble(durationStr);
durationTotal = durationTotal + durationDbl;
durationAvg = durationTotal / songCount;
}
time = (int)(100 * durationAvg);
minutes = (int)(time/100);
seconds = (int)(60 * (time%100));
displayTextArea.append("average song duration: " + minutes + ":" + seconds);
}
else
{
displayError("No songs have been entered");
}
}
【问题讨论】:
-
为什么乘以 100 然后除以 100?
100与时间有什么相关性?