我目前正在开设一个大学数学项目,该项目要求我创建一个与抵押相关的应用程序。我正在尝试编写Swift代码来计算每个月抵押贷款的本金和利息。
我编写的代码如下(值得一提的是我对使用Python的一点经验进行编码非常新,这都是自学的,所以任何批评都是受欢迎的!):
// Declare all variables
// y = periodic interest rate
// n = time left of repayment period (months)
let y = [Float](arrayLiteral: 0.02/12,0.03/12,0.04/12,0.05/12)
let n = [Float](arrayLiteral: 240,180,120,60)
var x = [Float]()
var interest = [Float]()
var T_1 = [Int]()
// Create Array from 0 to 240
for a in 0...240 {
T_1.append(a)
}
// Create Array of all 240 interest values
for a in 0...239 {
if a < 60 {
interest.append(y[0])
var repay = n[0]
x.append(repay)
}
else if a < 120 {
interest.append(y[1])
var repay = n[1]
x.append(repay)
}
else if a < 180 {
interest.append(y[2])
var repay = n[2]
x.append(repay)
}
else {
interest.append(y[3])
var repay = n[3]
x.append(repay)
}
}
print("Interest array:",interest)
// Calculate Each months Principal paid and Interest Paid
func variablerate(P: inout Float) -> [Float] {
var principal = [Float]()
var i_paid = [Float]()
var repayments = [Float]()
var R = Float(0)
let temporaryP = P
i_paid.append(0)
for a in 0...240 {
if (a>0) && (a<240) && (interest[a-1] == interest[a]) {
let ip = P*(1+interest[a-1])-P
i_paid.append(i_paid[a-1] + ip)
P = (1+interest[a-1])*temporaryP
P = P-R
let h = interest[a]
R = (P*h)/(1-pow(1+h,Float(interest.count-a)))
repayments.append(R)
principal.append(round(100*P)/100)
}
else if a==240 || a==241 {
P = P - R
principal.append(round(100*P)/100)
let ip = P*(1+interest[a-1])-P
i_paid.append(i_paid[a-1] + ip)
let h = interest[a-1]
P = P*(1+h)
}
else {
P = P - R
let h = interest[a]
R = (P*h)/(1-pow(1+h,Float(interest.count-a)))
repayments.append(R)
principal.append(round(100*P)/100)
let ip = P*(1+interest[a])-P
i_paid.append(i_paid[a-1] + ip)
P = P*(1+h)
}
}
return i_paid
return principal
}
var z = Float(100000)
print(variablerate(P: &z)) //This is where the error appears
给出的错误是:
错误:执行被中断,原因:EXC_BAD_INSTRUCTION(代码= EXC_I386_INVOP,子代码= 0x0)。
对此问题的任何帮助或指导都将受到大力赞赏!
答案 0 :(得分:1)
崩溃的原因是索引超出范围。
你正在使Index超出范围异常,因为在代码中你运行的循环从0到240,并且从局部变量 a 减去1,所以在第一次迭代中 a 将包含0并从0减1将给出负指数。
这是错误
for a in 0 ... 240 {
如果(a> 0)&amp;&amp; (a&lt; 240)&amp;&amp; (兴趣[a-1] ==兴趣[a])
以下代码带有更正
// Declare all variables
// y = periodic interest rate
// n = time left of repayment period (months)
let y = [Float](arrayLiteral: 0.02/12,0.03/12,0.04/12,0.05/12)
let n = [Float](arrayLiteral: 240,180,120,60)
var x = [Float]()
var interest = [Float]()
var T_1 = [Int]()
// Create Array from 0 to 240
for a in 0...240 {
T_1.append(a)
}
// Create Array of all 240 interest values
for a in 0...239 {
if a < 60 {
interest.append(y[0])
var repay = n[0]
x.append(repay)
}
else if a < 120 {
interest.append(y[1])
var repay = n[1]
x.append(repay)
}
else if a < 180 {
interest.append(y[2])
var repay = n[2]
x.append(repay)
}
else {
interest.append(y[3])
var repay = n[3]
x.append(repay)
}
}
print("Interest array:",interest)
// Calculate Each months Principal paid and Interest Paid
func variablerate(P: inout Float) -> [Float] {
var principal = [Float]()
var i_paid = [Float]()
var repayments = [Float]()
var R = Float(0)
let temporaryP = P
i_paid.append(0)
for a in 1...240 { // Code corrected now loop go from 1 to 240
if (a>0) && (a<240) && (interest[a-1] == interest[a]) {
let ip = P*(1+interest[a-1])-P
i_paid.append(i_paid[a-1] + ip)
P = (1+interest[a-1])*temporaryP
P = P-R
let h = interest[a]
R = (P*h)/(1-pow(1+h,Float(interest.count-a)))
repayments.append(R)
principal.append(round(100*P)/100)
}
else if a == 240 || a == 241 {
P = P - R
principal.append(round(100*P)/100)
let ip = P*(1+interest[a-1])-P
i_paid.append(i_paid[a-1] + ip)
let h = interest[a-1]
P = P*(1+h)
}
else {
P = P - R
let h = interest[a]
R = (P*h)/(1-pow(1+h,Float(interest.count-a)))
repayments.append(R)
principal.append(round(100*P)/100)
let ip = P*(1+interest[a])-P
i_paid.append(i_paid[a-1] + ip)
P = P*(1+h)
}
}
//return i_paid
return principal
}
var z = Float(100000)
print("\(variablerate(P: &z))")