我有一个方法,“firstMethod”调用4个方法,每个方法返回一个int。在firstMethod结束时,我希望它打印它调用的方法的返回值的总和。所以喜欢这个
firstMethod()
{
var sumOfMinutes = 0 //line A
sumOfMinutes = sumOfMinutes + secondMethod() //Line B
sumOfMinutes = sumOfMinutes + thirdMethod() //Line C
sumOfMinutes = sumOfMinutes + fourthMethod() //Line D
sumOfMinutes = sumOfMinutes + fifthMethod() //Line E
print(sumOfMinutes) //line F
}
secondMethod() -> Int
{
var number = generateRandom()
print(number)
return number
}
thirdMethod() -> Int
{
var number = generateRandom()
print(number)
return number
}
fourthMethod() -> Int
{
var number = generateRandom()
print(number)
return number
}
fifthMethod() -> Int
{
var number = generateRandom()
print(number)
return number
}
generateRandom() -> Int
{
//this method gets a random number out of an array
}
但这种印刷方式不是预期的。它打印的一个例子是:
0 五 2 9 4
**似乎它正在执行A行,然后是行F,然后行B C D和E.为什么会发生这种情况?这对我来说没有任何意义**