例如,
在你的程序中你有:
NSLog(@"Where are you going?");
NSLog(@" 1 = Location1, 2 = Location2");
printf("Make a selection:");
scanf("%i, &value);
switch (value) {
case 1:
NSLog(@"You are going to Location 1.")
break;
case 2:
NSLog(@"You are going to Location 2.");
break;
default:
NSLog(@"That is not a valid location");
break;
}
通常在您输入整数后,您的程序将返回 0 并且应用程序结束。你如何让它“循环”回到原来的 printf 来做出新的选择。或者甚至更好,一个新的 printf IE 'printf("Where else would you like to go?:");'?
最佳答案
你为什么不把它作为一个单独的方法,当你想循环的时候从它自己调用它。只需考虑以下代码,
void takeMeToPlaces() {
NSLog(@"Where are you going?");
NSLog(@"0 = Exit, 1 = Location1, 2 = Location2");
printf("Make a selection:");
scanf("%i, &value);
switch (value) {
case 0:
NSLog(@"You don't like to go anywhere");
break;
case 1:
NSLog(@"You are going to Location 1.");
takeMeToPlaces();
break;
case 2:
NSLog(@"You are going to Location 2.");
takeMeToPlaces();
break;
default:
NSLog(@"That is not a valid location");
takeMeToPlaces();
break;
}
}
关于Objective-C:如何在 switch 语句完成后返回到应用程序的开头,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5335927/