2016-01-19 162 views
0

我的問題是,我試圖做一個計算器,但我沒有這樣做,因爲預期的聲明問題。我審查了有關預期申報的其他問題,但我沒有解決這個問題。預期宣言錯誤

import UIKit 

class ViewController: UIViewController { 

    @IBOutlet weak var Screen: UILabel! 
    var firstNumber = Int() 
    var secondNumber = Int() 
    var isTypingNumber = false 
    var result = Int() 
    var operation = "" 


    @IBAction func number(sender: AnyObject) { 
     let number = sender.currentTitle 
     if isTypingNumber == true { 
      Screen.text = Screen.text! + number!! 
     } else { 
      Screen.text = number; 
     } 
     isTypingNumber = true 


    } 


    @IBAction func operation(sender: AnyObject) { 
     isTypingNumber = false 
     firstNumber = Int(Screen.text!)! 
     operation = sender.currentTitle!! 

    } 

    @IBAction func equals(sender: AnyObject) { 
     secondNumber = Int(Screen.text!)! 
     if operation == "+" { 
      result = firstNumber + secondNumber 
     } else if operation == "-" { 
      result = firstNumber - secondNumber 
     } else if operation == "x" { 
      result = firstNumber * secondNumber 

     }else { 
      result = firstNumber/secondNumber 

     } 
     Screen.text = "\(result)" 

     func clear(sender: AnyObject) { 
      firstNumber = 0 
      secondNumber = 0 
      isTypingNumber = false 
      result = 0 
      Screen.text = "\(result)" 

     } 





     func viewDidLoad() { 
      super.viewDidLoad() 
      // Do any additional setup after loading the view, typically from a nib. 
     } 

     func didReceiveMemoryWarning() { 
      super.didReceiveMemoryWarning() 
      // Dispose of any resources that can be recreated. 
     } 

} 
+0

'變種X = INT()'是錯誤的....它看起來像這樣:'VAR firstNumber:詮釋?' The?使var可選,所以它可以爲零。如果你不把它設置爲可選的,你必須在聲明它的同時設置變量。 'var no可選:Int = 0' – Godlike

+1

@實際上''var x = Int()''會編譯,它會將x初始化爲0。 – JAL

+0

@JAL真的嗎?哦,我測試了它,你是對的。所以它必須是。否則 – Godlike

回答

1

你缺少一個右括號在equals功能:

@IBAction func equals(sender: AnyObject) { 
    secondNumber = Int(Screen.text!)! 
    if operation == "+" { 
     result = firstNumber + secondNumber 
    } else if operation == "-" { 
     result = firstNumber - secondNumber 
    } else if operation == "x" { 
     result = firstNumber * secondNumber 

    }else { 
     result = firstNumber/secondNumber 

    } 
    Screen.text = "\(result)" 
} // missing this