2016-06-18 4 views
0

내가 처음 작동 할 때 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 버튼에 있다고 믿게합니다.

+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 
} 

2 A- 3 B,의 당신이 1 A 있다고 가정 해 봅시다 :

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

print(calculateGPA(grades)) 
관련 문제