0

输入正确的数字后,如何使该程序停止运行?“目标-C

int number;        
        NSLog(@"Enter number greater then 1 and less than 50 : ");
        scant ("%i", &number);   
        int exponent;            
        exponent = number;       
        do
        {
            if ((exponent > 0)&&(exponent < 50))  
        {
                for(int i = 1; i <= exponent;i++)   //Using for 
        {                    
                NSLog(@"N      %i \n",  (int)((pow(i, 1) * 100) / 100.0));
                NSLog(@"N^2 %4d \n", (int)((pow(i, 2) * 100) / 100.0));
                NSLog(@"N^3 %4d \n", (int)((pow(i, 3) * 100) / 100.0));
                NSLog(@"N^4 %4d \n", (int)((pow(i, 4) * 100) / 100.0));
                                NSLog(@"N^5 %4d \n", (int)((pow(i, 5) * 100) / 100.0));
                NSLog(@"  \n");                 
                }
            }
            else
            {
                NSLog(@"Number you entered is out of range");
                exponent = number;
            }
        }while (number !=0);        
    }
    return 0;
4

2 回答 2

0

设置number在块0内。if

if ((exponent > 0)&&(exponent < 50)) {
    number = 0; // this will make the "while" check fail and end the loop

    for ...
} else {
}
于 2013-11-09T05:04:18.703 回答
0

要停止您的程序,请使用 exit(0)。它退出主线程。

于 2013-11-09T06:32:12.893 回答