我无法找出为什么抛出上述错误 . 当我只输入2个参数(例如22.5公里)时,它运行正常,使用超过(20.5公里400英尺)给出错误 .
这是代码:
if (args.length > 0)
{
for (int i = 0; i < args.length; i = +2)
{
lengthsCollection.add(new Length(Double.parseDouble(args[i]), args[i+1]));
}
}
class Length在其他地方工作正常
public class Length {
private double valueM;
private String unitM;
public Length(double value, String unit)
{
this.valueM = value;
this.unitM = unit;
}
有人可以帮我吗?如果用户输入正确(长度单位对)我认为我的代码应该工作
1 回答
这个迭代:
args.length > 2
永远执行,因为i
永远是2
. 因此内存错误 .你需要的是: