首页 > 编程语言 > 详细

swift基础_控制语句

时间:2016-01-24 13:00:04      阅读:151      评论:0      收藏:0      [点我收藏+]

怎么着,swift会创建了吧,生成了我们的main.swift文件:

技术分享

 

看到hello world了吧,那么在swift里控制语句有事长什么样子的呢,且看:

import Foundation

print("Hello, World!")

//定义一个分数
var score = 80

//定义一个数字
var scoreArr = [90,100,79,70,50,30]

var minScore = 0;
var maxScore = 0;
var avgScore = 0.0
var sumScore = 0.0
var count = scoreArr.count;
//循环所有的元素
for s in scoreArr{
    sumScore = sumScore + Double(s)
    print("s is \(s)")
    if(minScore == 0 || minScore > s){
        minScore = s
    }else if(maxScore == 0 || maxScore < s){
        maxScore = s
    }
}

avgScore = sumScore/Double(count);
print("sumScore is \(sumScore) avgScore is \(avgScore)")
print("max score is \(maxScore) min score is\(minScore)")



for(var i=0;i<count;i++){
    var s = scoreArr[i];
    print("for..i \(i) s = \(s)")
    if(minScore == 0 || minScore > s){
        minScore = s
    }else if(maxScore == 0 || maxScore < s){
        maxScore = s
    }
}
avgScore = sumScore/Double(count);
print("sumScore is \(sumScore) avgScore is \(avgScore)")
print("max score is \(maxScore) min score is\(minScore)")


var index = 0;
repeat{
    if(index >= count){
        break;//退出
    }
    var s = scoreArr[index];
    print("do while s[\(index)]=\(s)");
}while(++index < count);


index = 0;
while(index < count){
    var s = scoreArr[index];//取得第i个元素
    print("do while s[\(index++)]=\(s)");
}


//switch 之前的switch 会穿透下面执行完
let appType = "iOS"
switch appType{
    case "iOS":
        print("iOS")
        fallthrough;//往下走,穿透一层
    case "AD":
        print("AD")
    case "WP":
        print("WP")
    default:
        print("没 有匹配值")
}

 

总结:稍后完善。。。

swift基础_控制语句

原文:http://www.cnblogs.com/xuanzhangran123/p/5154942.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!