Swift
1import UIKit 2 3class ViewController: UIViewController { 4 5 @IBOutlet var Label1: UILabel! 6 @IBOutlet weak var Text1: UITextField! 7 8 override func viewDidLoad() { 9 super.viewDidLoad() 10 // Do any additional setup after loading the view, typically from a nib. 11 } 12 13 override func didReceiveMemoryWarning() { 14 super.didReceiveMemoryWarning() 15 // Dispose of any resources that can be recreated. 16 } 17 18 @IBAction func Button1(sender: AnyObject) { 19 let n = Text1.text 20 if Int(n!) < 2 { 21 Label1.text = "素数ではありません" 22 } 23 if Int(n!) == 2{ 24 Label1.text = "素数です" 25 } 26 if Int(n!)! % 2 == 0{ 27 Label1.text = "素数ではありません" 28 } 29 var i = 0 30 for i == 3; i <= Int(n!)! / i; i += 2{ 31 if Int(n!)! % i == 0{ 32 Label1.text = "素数ではありません" 33 } 34 } 35 Label1.text = "素数です" 36 } 37 38 39 } 40
このようにしてみましたがエラーになります。
エラー回避の方法と、またこれが素数を判定するものとして正しいのかということ自体がわからないので教えていただけるとありがたいです。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2015/09/15 12:03
2015/09/15 14:41
2015/09/16 03:56