Simple Objective-C Program Not Working
int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    int n, number, triangularN开发者_StackOverflow社区umber;
    NSLog(@"What Triangular Number Do You Want?");
    scanf(@"%i", &number);
    triangularNumber = 0;
    for (n = 1; n <= number; ++n)  
        triangularNumber += n;
        NSLog(@"Triangular Number %i is %i", number, triangularNumber);
    [pool drain];
    return 0;
}
The output when I write an integer is this:
Triangular Number 0 is 0
Your input number is 0, and your condition in the for loop starts at 1. Therefore, the loop is never executed.
It should be :
for (n = 1; n <= number; ++n)  {
        triangularNumber += n;
        NSLog(@"Triangular Number %i is %i", number, triangularNumber);
}
In the way you wrote it, it is parsed as:
for (n = 1; n <= number; ++n)  {
    triangularNumber += n;
}
NSLog(@"Triangular Number %i is %i", number, triangularNumber);
And since the input (number) is 0 (as indicated by the print) the loop doesn't happen, yet the line is printed.
 
         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论