J'ai essayé de créer une calculatrice de valeur x pour la formule quadratique.Formule quadratique dans Swift
code:
var firstXValueVAR = 1.0
var secondXValueVAR = 1.0
var squaredValueVAR = 1.0
var firstOperationValueVAR = "+"
var secondOperationValueVAR = "+"
var constantValueVAR = 0.0
var topFormula = 0.0
var topFormula2 = 0.0
var bottomFormula = 0.0
var TotalSquaredValue = 0
var bSquared = 0
var totalX = 0.0
var totalX2 = 0.0
Ensuite, lorsque je clique sur « Go » pour calculer X, j'ai, et je ne suis pas vraiment sûr de ce que je fait de mal, mais chaque fois que j'imprime X, il dit nan, et il dit aussi que pour topFormula et topFormula2 (= nan).
func updateValues(){
var secondXValueInt = Int(secondXValueVAR)
var firstXValueInt = Int(firstXValueVAR)
var squaredValueInt = Int(squaredValueVAR)
var constantValueInt = Int(constantValueVAR)
bSquared = secondXValueInt << 2
TotalSquaredValue = firstXValueInt << squaredValueInt
firstXValueVAR = Double(firstXValue.text!)!
secondXValueVAR = Double(secondXValue.text!)!
squaredValueVAR = Double(squaredValue.text!)!
constantValueVAR = Double(constantValue.text!)!
if operations.contains(firstOperationValue.text!){
firstOperationValueVAR = firstOperationValue.text!
if secondOperationValueVAR == "*"{
topFormula = -(secondXValueVAR) + sqrt(Double(bSquared) - (4 * firstXValueVAR * constantValueVAR))
bottomFormula = 2 * firstXValueVAR
totalX = topFormula/bottomFormula
print(squaredValue)
topFormula2 = -(secondXValueVAR) - sqrt(Double(bSquared) - (4 * firstXValueVAR * constantValueVAR))
bottomFormula = 2 * firstXValueVAR
totalX2 = topFormula2/bottomFormula
print(topFormula)
print(topFormula2)
print(bottomFormula)
print("X = \(totalX) & \(totalX2)")
}
if secondOperationValueVAR == "/"{
topFormula = -(secondXValueVAR) + sqrt(Double(bSquared) - (4 * firstXValueVAR * constantValueVAR))
bottomFormula = 2 * firstXValueVAR
totalX = topFormula/bottomFormula
print(squaredValue)
topFormula2 = -(secondXValueVAR) - sqrt(Double(bSquared) - (4 * firstXValueVAR * constantValueVAR))
bottomFormula = 2 * firstXValueVAR
totalX2 = topFormula2/bottomFormula
print(topFormula)
print(topFormula2)
print(bottomFormula)
print("X = \(totalX) & \(totalX2)")
}
if secondOperationValueVAR == "+"{
topFormula = -(secondXValueVAR) + sqrt(Double(bSquared) - (4 * firstXValueVAR * constantValueVAR))
bottomFormula = 2 * firstXValueVAR
totalX = topFormula/bottomFormula
print(squaredValue)
topFormula2 = -(secondXValueVAR) - sqrt(Double(bSquared) - (4 * firstXValueVAR * constantValueVAR))
bottomFormula = 2 * firstXValueVAR
totalX2 = topFormula2/bottomFormula
print(topFormula)
print(topFormula2)
print(bottomFormula)
print("X = \(totalX) & \(totalX2)")
}
if secondOperationValueVAR == "-"{
topFormula = -(secondXValueVAR) + sqrt(Double(bSquared) - (4 * firstXValueVAR * constantValueVAR))
bottomFormula = 2 * firstXValueVAR
totalX = topFormula/bottomFormula
print(squaredValue)
topFormula2 = -(secondXValueVAR) - sqrt(Double(bSquared) - (4 * firstXValueVAR * constantValueVAR))
bottomFormula = 2 * firstXValueVAR
totalX2 = topFormula2/bottomFormula
print(topFormula)
print(topFormula2)
print(bottomFormula)
print("X = \(totalX) & \(totalX2)")
}
}
if operations.contains(secondOperationValue.text!){
secondOperationValueVAR = secondOperationValue.text!
}
}
@IBAction func GoAction(sender: AnyObject) {
updateValues()
}
question générale: avez-vous vérifié ce qui se passe dans l'utilisateur entre le texte dans vos champs de texte? – luk2302
Avez-vous essayé un point d'arrêt et vérifié étape par étape le code? – UlyssesR
Salut Andy. Il y a une très bonne réponse ci-dessous qui, malheureusement, ne semble pas s'appliquer. Cela vous a-t-il aidé à ce moment-là? Si vous pouvez y répondre, votez-y ou acceptez-le, ce qui est généralement considéré comme une bonne pratique ici. Mieux vaut tard que jamais? – halfer