Objective-C:如何在 switch 语句完成后返回到应用程序的开头

标签 objective-c xcode ios loops switch-statement

例如,

在你的程序中你有:

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/

相关文章:

ios - 未调用dispatch_async block

objective-c - 动态给出一个相当于 NSButton 的 Key

ios - NSDictionary 查找 keyPath

ios - 使用 ScrollView 和 ImageView 在 iOS 中构建类似 map 的实现

ios - NSNotification 面向对象

objective-c - Xcode 5不导入崩溃文件?

ios - 升级到 Xcode 10 后找不到构建错误 : FBSDKShareKit. h 文件

objective-c - 如何通过索引替换单个字符

ios - 如何在导航栏 Swift 右侧添加自定义 View ?

ios - 我如何呈现 'standalone' View Controller ('forgetting' 前一个)