爲什麼不是這個代碼工作,當我第一次addA工作,但它然後到2.66,並繼續下去,當它應該保持在4.0。爲什麼我的GPA Double值在Swift中無法正常工作?
import UIKit
//Quantity of A A- B+ B B- C+ C C- D+ D D- F
var gradesQuantity = [0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
var gradesAdded = 0.0
var gpa = 0.0
func addGrades(grades: [Double]) -> Double {
for grade in grades {
gradesAdded += grade
if gradesQuantity[0] <= 0 {
gradesQuantity[0] = 0
}
}
return gradesAdded
}
func calcGPA(grades: [Double]) -> Double {
gpa = (grades[0] * 4.0 + grades[1] * 3.7 + grades[2] * 3.3 + grades[3] * 3.0 + grades[4] * 2.7 + grades[5] * 2.3 + grades[6] * 2.0 + grades[7] * 1.7 + grades[8] * 1.3 + grades[9] * 1.0 + grades[10] * 0.7)/gradesAdded
return gpa
}
class ViewController: UIViewController {
@IBOutlet weak var GPALabel: UILabel!
@IBOutlet weak var GPANumber: UILabel!
@IBOutlet weak var AQuantity: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func AddA() {
gradesQuantity[0] += 1.0
addGrades(gradesQuantity)
let x = calcGPA(gradesQuantity)
GPANumber.text = String(x)
AQuantity.text = String(Int(gradesQuantity[0]))
}
@IBAction func SubtractA() {
gradesQuantity[0] -= 1.0
addGrades(gradesQuantity)
let x = calcGPA(gradesQuantity)
GPANumber.text = String(x)
AQuantity.text = String(Int(gradesQuantity[0]))
}
}
可以請人幫我把這個在操場上和其下添加代碼,在按鍵相同,它的工作原理,但在這個項目它不工作。這導致我相信問題出現在addA和subtractA按鈕中。
你能解釋一下更多關於應用程序應該做什麼以及gpa如何加權? –
每次計算時不應該將'gradesAdded'重置爲0嗎? – Paulw11