Swift语言基础笔记(四)

Swift语言基础笔记(四),第1张

概述通过前面几篇的学习,Swift的基础类型学习的差不多了,接下来就学习流程控制与条件语句和运算符 一、运算符 //: Playground - noun: a place where people can playimport UIKit//运算符Swift学习,+ - * / % > < += -= || && | & 注意不同类型需要显示强制转换var x = 10;var y =

通过前面几篇的学习,Swift的基础类型学习的差不多了,接下来就学习流程控制与条件语句和运算符

一、运算符

@H_403_4@//: Playground - noun: a place where people can playimport UIKit//运算符Swift学习,+ - * / % > < += -= || && | & 注意不同类型需要显示强制转换var x = 10;var y = 3;var z = 0;x/yDouble(x)/Double(y);var xx = +x;var yy = -y;x += 9;if x > y{ print("10 > 3");}var capacity = 30;var volume = 50;if x > y && capacity < volume{ print("Buy it");}if x < y || capacity > volume{ print("Buy it");}else{ print("not buy it");}

二、循环语句
循环语句有for、while、do-while

//: Playground - noun: a place where people can playimport UIKit//循环语句有for、while、do-whilefor index in -99...99{    index;    index*index;}var i = -99;for ; i <= 99 ; {    i*i;    i++;}for var a = -6.28; a <= 6.28; a += 0.1 {    sin(a);}var ii = -99;var step = 1;for ; ii <= 99; ii += step{    ii * ii;    step *= 2;}var a = 10;while a > 0{    a--;}var aWin = 0;var bWin = 0;var game = 0;while aWin < 3 && bWin < 3{    game++;    let a = arc4random_uniform(6) + 1;    let b = arc4random_uniform(6) + 1;    print("a is \(a),b is \(b).",terminator:" ");    if a > b{        print("A win");        bWin = 0;        aWin++;    }else if a < b{        print("B win");        aWin = 0;        bWin++;    }else{        print("a == b");        aWin = 0;        bWin = 0;    }}let winner = aWin == 3 ? "A" : "B";print("After \(game) games \(winner) Win")print("");var aaWin = false;var bbWin = false;var agame = 0;repeat{    game++;    let a = arc4random_uniform(6) + 1;    let b = arc4random_uniform(6) + 1;    print("a is \(a),terminator:" ");    if a > b{        aaWin = true;    }else if a < b{        bbWin = true;    }else{        print("a == b");    }    print("");}while !aaWin && !bbWin;let awinner = aaWin ? "A" : "B";print("After \(awinner) Win")print("");while true{    let a = arc4random_uniform(6) + 1;    let b = arc4random_uniform(6) + 1;    print("a is \(a),b is \(b).");    if a == b{        print("a == b");        continue;    }        let winner = a > b ? "A" : "B";    print("\(winner) win!");    break;}

三、条件语句

switch if-else

//: Playground - noun: a place where people can playimport UIKit//条件语句var count = 15if a > 20{	print(\(a) > 20)}else{	print(\(a) <= 20)}let rating = "A";switch rating{case "a","A":    print("Great");case "b","B":    print("Just so-so");case "c","C":    print("It's Bad");default:    print("Error")}let score = 100;switch score{case 0:    print("You got a egg")case 1..<60:    print("You faild")case 60:    print("Just passed")case 61..<80:    print("Just so-so")case 80..<90:    print("Good")case 90..<100:    print("Great")case 100:    print("Perfect!")default:    print("Error score")}print("")//x^4 - y^2 = 15* x * y;findAnswer:for m in 1...300{    for n in i...300{        if m*m*m*m - n*n == 15*m*n{            print("m = \(m),n = \(n),left = \(m*m*m*m - n*n),right = \(15*m*n)");            break findAnswer;        }    }}//where语句的使用let point = (3,5);switch point{case let(x,y) where x == y:    print("It's on the line x == y!");case let(x,y) where x == -y:    print("It's on the line x == -y!");case let(x,y):    print("It's just an ordinary point.");    print("The point is \(x),\(y)");}let age = 19;switch age{case 10...19:    print("You're a teenager.");default:    print("You're not a teenager.");}if case 10...19 = age{    print("You're a teenager.");}if case 10...19 = age where age >= 18{    print("You're a teenager and in a college.");}let vector = (4,0);if case (let x,0) = vector where x > 2 && x < 5{    print("It's the vector");}
总结

以上是内存溢出为你收集整理的Swift语言基础笔记(四)全部内容,希望文章能够帮你解决Swift语言基础笔记(四)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1070908.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-26
下一篇2022-05-26

发表评论

登录后才能评论

评论列表(0条)

    保存