Swift语言基础笔记(四)

前端之家收集整理的这篇文章主要介绍了Swift语言基础笔记(四)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

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

一、运算符

//: Playground - noun: a place where people can play

import UIKit

//运算符Swift学习,+ - * / % > < += -= || && | & 注意不同类型需要显示强制转换

var x = 10;
var y = 3;
var z = 0;

x/y
Double(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 play

import UIKit

//循环语句有for、while、do-while
for 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 play

import UIKit

//条件语句
var count = 15
if 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");
}
原文链接:https://www.f2er.com/swift/322820.html

猜你在找的Swift相关文章