【发布时间】:2011-03-09 23:58:28
【问题描述】:
我正在尝试将原始双精度值数组转换为对象。结果我收到“类型不匹配错误”
private double[]purchases;
到
private CreditCard[]purchases;
然后当我尝试向数组添加一个值时
public void purchase(double amount)throws Exception
{
if (numPurchases<purchases.length)
if (amount >0 )
if(amount+balance<=creditLimit)
if( GregorianCalendar.getInstance().getTimeInMillis()<=expDate.getTimeInMillis())
{
balance+=amount;
purchases[numPurchases]= amount;
numPurchases++;
}
else
{
throw new Exception("card expired");
}
else{
throw new Exception("insufficient credit");
}
else{
throw new Exception("invalid amount");
}
else{
throw new Exception("exceeded number of allowed purchases");
}
}
错误消息显示 amount 的类型不匹配“无法从 double 转换为 CreditCard 如何更正代码以允许我将购买金额添加到数组中?
【问题讨论】:
-
@Neil - 我希望我不知道。