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

发布于 2024-10-23 00:34:56 字数 544 浏览 5 评论 0原文

例如,

在您的程序中:

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("你还想去哪里?:");'?

For example,

In your program you have:

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;
}

Normally after you input your integer your program will return 0 and the application ends. How do you go about having it "loop" back to the original printf to make a new selection. Or even better, a new printf IE 'printf("Where else would you like to go?:");'?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

姐不稀罕 2024-10-30 00:34:56

为什么不将其保留为单独的方法,并在需要循环时从自身调用它。只需考虑以下代码,

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;
    }
}

Why don't you keep it as a separate method and call it from itself when you want to loop. Just consider the following code,

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;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文