在我输入正确的号码后,如何使这个程序停止运行?

时间:2021-05-01 20:51:46

How can I make this program to stop running after I enter the right number? "Objective-C

在我输入正确的号码后,如何使这个程序停止运行?objective - 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 个解决方案

#1


0  

Set number to 0 inside the if block.

在if块中设置为0。

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

    for ...
} else {
}

#2


0  

To stop your program, use exit(0). It exits the main thread.

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

#1


0  

Set number to 0 inside the if block.

在if块中设置为0。

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

    for ...
} else {
}

#2


0  

To stop your program, use exit(0). It exits the main thread.

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