swift基础_控制语句
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift基础_控制语句相关的知识,希望对你有一定的参考价值。
怎么着,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基础_控制语句的主要内容,如果未能解决你的问题,请参考以下文章