Swift语言学习总结——控制语句
//main.swift
//swift控制语句
import Foundation
println("Hello , World");
//定义一个分数
var score=80;
//定义一个数组
var scoreArr=[90,100,23,43,42,67];
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);
println("s is\(s)");
if(minScore==0||minScore>s)
{
minScore=s;
}
if(maxScore==0||maxScore<s)
{
maxScore=s;
}
}
avgScore=sumScore/Double(count);
println("sumScore is \(sumScore) avgScore=\(avgScore)");
println("maxScore is \(maxScore) minScore=\(minScore)");
for(var i=0;i<count;i++)
{
var s=scoreArr;
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("sumScore is \(sumScore) avgScore=\(avgScore)");
println("maxScore is \(maxScore) minScore=\(minScore)");
var index=0;
do{
if(index>=count){
break;
}
var s=scoreArr[index];//取得第i元素
println("do-while s[\(index)=\(s)]");
}while(index++<count);
index=0;
while(index<count)
{
var s=scoreArr[index];//取得第i元素
println("while s[\(index++)=\(s)]");
}
|
|