【问题标题】:How to jump back to the top of the code after if statement C languageC语言if语句后如何跳转回代码顶部
【发布时间】:2020-05-29 07:45:29
【问题描述】:

在用户从菜单中选择一个选项后,我正试图让我的程序从头开始。 当用户选择 1 然后输入捐赠金额时,我希望程序重新启动并显示菜单 What would you like to do? 而不仅仅是重新启动 if 语句。

我应该在 if 语句中添加一个 for 循环来完成这个吗?感谢您的帮助。

 printf("What would you like to do?\n");
    printf("      1 - Donate\n");
    printf("      2 - Invest\n");
    printf("      3 - Print balance\n");
    printf("      4 - Exit\n");
    printf("\n");

    //scans menu choice
    scanf("%d", &menu_option);

    if(menu_option==1)
    {
    printf("How much do you want to donate?\n");
    scanf("%lf", &donation);
    donations_made++;
    current_Balance = initial_Balance + donation;
    }

【问题讨论】:

  • 将所有内容放入for(;;) { 并执行break; 以在除案例1 之外的所有情况下退出循环
  • 把它放在一个循环中。

标签: c for-loop if-statement


【解决方案1】:

当用户选择1然后输入捐款金额时,我希望程序重新启动并显示菜单

只是做

for(;;) {
  printf("      1 - Donate\n");
  printf("      2 - Invest\n");
  printf("      3 - Print balance\n");
  printf("      4 - Exit\n");
  printf("\n");

  //scans menu choice
  scanf("%d", &menu_option);

  if(menu_option==1)
  {
    printf("How much do you want to donate?\n");
    scanf("%lf", &donation);
    donations_made++;
    current_Balance = initial_Balance + donation;
    // NO BREAK
  }
  else {
    .... management of other cases
    break;
  }
}

或者如果你喜欢

do {
  printf("      1 - Donate\n");
  printf("      2 - Invest\n");
  printf("      3 - Print balance\n");
  printf("      4 - Exit\n");
  printf("\n");

  //scans menu choice
  scanf("%d", &menu_option);

  if(menu_option==1)
  {
    printf("How much do you want to donate?\n");
    scanf("%lf", &donation);
    donations_made++;
    current_Balance = initial_Balance + donation;
  }
  // ... management of other cases
} while (menu_option==1);

但是您确定在情况 2 和 3 中也不想重做吗?在这种情况下,将while (menu_option==1); 替换为while (menu_option != 4); 或在第一个提案中,仅当menu_option 为4 时才使用break

我还鼓励您检查 scanf("%d", &menu_option); 的返回值,以确保在输入中给出了一个有效整数并设置了 menu_option

【讨论】:

    【解决方案2】:
    #include <stdio.h>
    
    int main()
    
    {
     int menu_option;
     double donation;
     int donations_made = 0;
     int current_Balance = 0;
     int initial_Balance = 0;
    
     for (;;)
     {
    
        printf("What would you like to do?\n");
        printf("      1 - Donate\n");
        printf("      2 - Invest\n");
        printf("      3 - Print balance\n");
        printf("      4 - Exit\n");
        printf("\n");
    
        //scans menu choice
        scanf("%d", &menu_option);
    
                if (menu_option==1)
                {
                       printf("How much do you want to donate?\n");
                       scanf("%lf", &donation);
                       donations_made++;
                       current_Balance = initial_Balance + donation;
                 }
            else if (menu_option == 4)
                     break;
    
       }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多