Last active
October 28, 2016 12:02
-
-
Save kiras0518/7e93e9bbb9e52a267716843efbedc357 to your computer and use it in GitHub Desktop.
swift
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
定義function,接受3個參數,起始值,最大值和決定數字倍數的number, 回傳運算結果 比方起始值3, | |
最大值98,決定數字倍數的number為5時,(只包含5的倍數) 運算結果為 5 + 10 + 15 + ….. + 95 | |
*/ | |
func calNumber(starNumber:Int, maxNumber:Int, mulNumber:Int) -> Int //3個參數Int傳入值 回傳一個Int | |
{ | |
var total = 0 | |
for i in starNumber...maxNumber where i%mulNumber == 0 //當5倍數餘數為零0"做相加" | |
{ | |
total += i | |
} | |
return total | |
} | |
calNumber(starNumber: 3, maxNumber: 98, mulNumber: 5) | |
/* | |
定義function,接受3個參數,起始值,最大值和決定數字倍數的number, 回傳運算結果 比方起始值3,最大值11, | |
決定數字倍數的number為5時,(不包含5的倍數) 運算結果為 3 + 4 + 6 + 7 + 8 + 9 + 11 | |
*/ | |
func calNumber1(starNumber:Int, maxNumber:Int, mulNumber:Int) -> Int //3個參數Int傳入值 回傳一個Int | |
{ | |
var total = 0 | |
for i in starNumber...maxNumber where i%mulNumber != 0 //當mul倍數餘數為零"不做" | |
{ | |
total += i | |
} | |
return total | |
} | |
calNumber1(starNumber: 3, maxNumber: 11, mulNumber: 5) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment