输入正确的号码后,如何让该程序停止运行?

时间:2013-11-09 03:37:08

标签: objective-c

输入正确的号码后,如何让该程序停止运行? “目标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;

2 个答案:

答案 0 :(得分:0)

number区块内将0设置为if

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

    for ...
} else {
}

答案 1 :(得分:0)

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