前端之家收集整理的这篇文章主要介绍了
swift -控制语句,
前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
//
// main.swift
// 控制语句-03
//
import Foundation
println("Hello,World!")
//定义一个分数
var score = 80;
/**
* 定义一个数组
*/
var scoreArr = [90,99,45,89,67,54];
var minscore = 0;
var maxscore = 0;
var avgscore = 0.0;
var sumscore = 0.0;
var count = scoreArr.count;
//循环所有的yuans
for s in scoreArr
{
sumscore = sumscore + Double(s);
println("s is \(s)");
if(minscore == 0||minscore > s){
minscore = s;
}
if(maxscore == 0||maxscore < s)
{
maxscore = s;
}
}
avgscore = sumscore/Double(count);
println("sumSocre is \(sumscore) avgscore is \(avgscore)");
println("maxscore is \(maxscore) minscore is \(minscore)");
for(var i = 0; i < count; i++){
var s = scoreArr[i];//取得第iyuans
println("for ...i\(i) s = \(s)");
if(minscore == 0||minscore > s){
minscore = s;
}
if(maxscore == 0||maxscore < s)
{
maxscore = s;
}
}
avgscore = sumscore/Double(count);
println("sumSocre is \(sumscore) avgscore is \(avgscore)");
println("maxscore is \(maxscore) minscore is \(minscore)");
var index = 0;
do{
if(index >= count){
break;
}
var s = scoreArr[index];
println("do-while s[\(index)]=\(s)");
}while(++index < count);
index = 0;
while(index < count)
{
index++;
if(index >= count){
break;
}
var s = scoreArr[index];
println("while s[\(index)] = \(s)");
}
//switch 之前的switch会穿透
let appType = "ios";
switch appType
{
case "ios":
println("ios开发");
fallthrough;//让往下走
case "Android":
println("Android开发");
case "Java":
println("Java 开发");
default:
println("没有任何匹配");
}