내가 처음 작동 할 때 addA를 할 때이 코드가 작동하지 않는 이유는 무엇입니까? 그렇지만 4.0으로 유지되어야 할 때 2.66으로 내려가 계속됩니다.스위프트에서 GPA Double 값이 올바르게 작동하지 않는 이유는 무엇입니까?
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