2015-06-28 53 views
-2
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
{ 
    NSInteger n = 5; 
    NSInteger nFactorial = n; 
    while (n > 1) { 
     nFactorial = nFactorial * --n; 
    } 
    NSLog(@"The factorial of %ld is %ld", (long)n, (long)nFactorial); 
    return YES; 
} 

對於此代碼,爲什麼nFactorial必須初始化爲n?爲什麼代碼不能在所有有nFactorial的地方使用n?循環格式化

回答

0
  1. 爲了使第一個循環有正確的結果。
  2. nFactorial = nFactorial * --n,' - '具有更高的優先級。

如果使用n取代nFactorial,第二nFactorial的價值是不是我們的例外。