2016-06-18 40 views
0

爲什麼不是這個代碼工作,當我第一次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按鈕中。

+0

你能解釋一下更多關於應用程序應該做什麼以及gpa如何加權? –

+0

每次計算時不應該將'gradesAdded'重置爲0嗎? – Paulw11

回答

0

我認爲你的問題的根本原因是你做了太多的工作。如果刪除addGrades功能(這是不是真的增加任何功能,只要我可以告訴),並用以下內容替換您的AddASubtractA功能,那麼它會工作打算:

@IBAction func AddA() { 
    gradesQuantity[0] += 1.0 
    gradesAdded += 1.0 
    GPANumber.text = String(calcGPA(gradesQuantity)) 
    AQuantity.text = String(Int(gradesQuantity[0])) 
} 

@IBAction func SubtractA() { 
    gradesQuantity[0] -= 1.0 
    gradesAdded -= 1.0 
    GPANumber.text = String(calcGPA(gradesQuantity)) 
    AQuantity.text = String(Int(gradesQuantity[0])) 
} 

對於進一步發展,我會建議創建一個具有添加和減去成績並返回GPA函數的子類數組。這應該使事情更具可讀性和可重用性。

+0

非常感謝你現在正常工作! – tchristofferson

1

您正在編寫帶副作用的函數(修改外部變量)。儘量避免這種情況。另外,你可能想要在你的程序中有更多的結構。的gradesQuantity以字母等級助理位置值是好的,但你可以做一個解釋更好:

enum GradeLetter: Double { 
    case A = 4.0 
    case AMinus = 3.7 
    case BPlus = 3.3 
    case B = 3 
    case BMinus = 2.7 
    case CPlus = 2.3 
    case C = 2 
    case CMinus = 1.7 
    case DPlus = 1.3 
    case D = 1 
    case DMinus = 0.7 
    case F = 0 
} 

func calculateGPA(grades: [GradeLetter: Int]) -> Double { 
    let courseCount = grades.reduce(0.0) { aggregate, grade in 
     return aggregate + Double(grade.1) 
    } 
    let totalPoint = grades.reduce(0.0) { aggregate, grade in 
     return aggregate + grade.0.rawValue * Double(grade.1) 
    } 

    return totalPoint/courseCount 
} 

假設你有1 A,2 A-和3 B

var grades = [GradeLetter: Int]() 
grades[.A] = 1 
grades[.AMinus] = 2 
grades[.B] = 3 

print(calculateGPA(grades)) 
相關問題