【问题标题】:How to execute a set of operations repeatedly?如何重复执行一组操作?
【发布时间】:2013-12-31 10:19:17
【问题描述】:

我想制作一个程序来询问用户问题,然后用户选择一个答案,然后程序返回到原始问题目录。例如:

你最喜欢的颜色是什么? 1. 蓝色 2. 绿色 3. 黄色

然后用户选择说“2”

那我想让它返回

你最喜欢的颜色是什么? 1. 蓝色 2. 绿色 3. 黄色

我已经编写了代码来处理询问、选择问题的答案。我只需要帮助返回到原始问题,以便用户可以根据需要再次回答。谢谢大家!

#include <stdio.h>

int main (void)
{
   printf("\nIntroducing Space\n"); 
   printf("Brought to you in part by Free Time\n\n\n");
   while(1) {   
       int space;
       int subspace1;
       int subspace2;
       printf("What would you like to do in Space?\n");
       printf("\nDIRECTORY\n\n");
       printf("1. What is space?\n");
       printf("2. Tell me how cool I am\n");
       printf("Enter the number to your desired space\n");
       scanf("%i", &space);
       if (space == 2){
           printf("Although I have never met you, anyone in space would be considered    cool to the average eye. I mean lets be real, its space.\n\n");
           printf("Do you have any interesting hobbies or tidbits about yourself?\n\n");
           printf("1. I can play the flute blindfolded\n");
           printf("2. I can eat twelve pounds of salt water taffy in one sitting\n");
           printf("3. My uncle drives an RV\n");
           scanf("%i", &subspace2);
           if (subspace2 == 1) {
              printf("\n The flute huh? You're somethin' else\n");
           }
           if (subspace2 == 2) {
              printf("\n But let's be honest who likes salt water taffy these days anyway?\n");
           }
           if (subspace2 == 3) {
              printf("\n An RV? Now adays they call that 'homeless' but hey whatever floats your boat chief\n");
           }
      }
   }
   return 0;
}

我觉得这是我需要某种返回语句的地方,它让我回到我最初的问题“你想在太空中做什么”,以便用户可以提出另一个问题。

【问题讨论】:

  • 请向我们展示您编写的代码。
  • 这背后的逻辑是什么?您想何时返回上一个通话?每次都想要吗?
  • 对不起,我很难解释自己是我的错。我用我当前的代码编辑了我的原始帖子
  • @Jekyll 我想做的是问用户“你想做什么?”并给用户选项。在用户选择某样东西后,我想让程序说“你想做点别的吗?”并返回到原来的待办事项目录
  • 首先将所有内容放在一个大的 while(1) { } 循环中,然后您可以使用子循环来改进

标签: c loops return repeat


【解决方案1】:

使用 do...while 而不是 while,因为您提到您至少问过一次问题。 这就是你可以做到的方式

int choice;
char ch2;
do{

printf("Option1");
printf("Option2");
printf("Option3");
scanf("%d", choice);

switch(choice){
case 1: //statements
        break;
case 2: //statements
        break;
case 3: //statements
        break;
deafult: //statements
}

printf("Do you want to continue(c) or quit(q)?");
scanf("%s", ch2)
}while(ch2 != "q");

【讨论】:

    【解决方案2】:

    循环使用if语句的方式需要注释,你写:

    scanf("%i", &subspace2);
    if (subspace2 == 1) {
        printf("\n The flute huh? You're somethin' else\n");
    }
    if (subspace2 == 2) {
        printf("\n But let's be honest who likes salt water taffy these days anyway?\n");
    }
    if (subspace2 == 3) {
        printf("\n An RV? Now adays they call that 'homeless' but hey whatever floats your boat chief\n");
    }
    

    前面的代码对subspace2 变量进行了三次测试。而不是使用你应该使用:

    scanf("%i", &subspace2);
    if (subspace2 == 1) {
        printf("\n The flute huh? You're somethin' else\n");
    }
    else if (subspace2 == 2) {
        printf("\n But let's be honest who likes salt water taffy these days anyway?\n");
    }
    else if (subspace2 == 3) {
        printf("\n An RV? Now adays they call that 'homeless' but hey whatever floats your boat chief\n");
    }
    

    仅当第一个不正确时才评估第二个分支,仅当第一个和第二个不正确时才评估第三个分支,但最好的选择可能是:

    scanf("%i", &subspace2);
    switch(subspace2) {
        case 1:
            printf("\n The flute huh? You're somethin' else\n");
            break;
        case 2:
            printf("\n But let's be honest who likes salt water taffy these days anyway?\n");
            break;
        case 3:
            printf("\n An RV? Now adays they call that 'homeless' but hey whatever floats your boat chief\n");
            break;
        default:
            printf("\n invalid option\n" );
    }
    

    这将通过一个跳转表来实现,它会更快。

    【讨论】:

      【解决方案3】:

      您应该使用循环。您还应该查看switch 声明。 从长远来看,使用a good book for learning C 可能会更好。

      例子:

      #include <stdio.h>
      int main() {
          char c;
          while(scanf("%c", &c) > 0) {
              switch(c) {
              case 'q':
              case 'Q':
                  /* exit loop */
                  break;
              default:
                  /* do something */
                  printf("%c", c);
              }
          }
      }
      

      【讨论】:

      • 谢谢,我刚刚实现了一个 while(1) 循环,但接下来我会更深入地研究这个
      【解决方案4】:

      从您的问题来看,目前尚不清楚您实际上想要实现什么,但我认为一个简单的循环就可以了。

      检查此代码

      #include <stdio.h>
      #include <stdlib.h>
      
      int main (void)
      {
      
      
          printf("\nIntroducing Space\n");                                
          printf("Brought to you in part by Free Time\n\n\n");
      
      
          int space;
          int subspace1;
          int subspace2;
      
          while (1)
          {
              printf("What would you like to do in Space?\n");                
              printf("\nDIRECTORY\n\n");
              printf("1. What is space?\n");
              printf("2. Tell me how cool I am\n");
              printf("3. no more question. I want to get out\n");
      
      
              printf("Enter the number to your desired space\n");
              scanf("%d", &space);
      
              if (space == 0)
                  exit(0);
              else if (space ==1)
              {
                  printf("First go to wikipedia and read about the space\n\n");
              }
              else if (space == 2){
                  printf("Although I have never met you, anyone in space would be considered    cool to the average eye. I mean lets be real, its space.\n\n");
                  printf("Do you have any interesting hobbies or tidbits about yourself?\n\n");
                  printf("1. I can play the flute blindfolded\n");
                  printf("2. I can eat twelve pounds of salt water taffy in one sitting\n");
                  printf("3. My uncle drives an RV\n");
                  scanf("%i", &subspace2);
                  if (subspace2 == 1) {
                      printf("\n The flute huh? You're somethin' else\n");
                  }
                  if (subspace2 == 2) {
                      printf("\n But let's be honest who likes salt water taffy these days anyway?\n");
                  }
                  if (subspace2 == 3) {
                      printf("\n An RV? Now adays they call that 'homeless' but hey whatever floats your boat chief\n");
                  }
              }
              else
                  printf("Please enter the choice properly\n\n");
          }
          return 0;
      }
      

      However, when the choice number will grow large, its better to use switch.试一试。编码愉快!

      【讨论】:

      • 谢谢!我明白你在说什么。我想我将不得不更深入地研究 switch 语句以整理一下
      • 这就是精神。继续努力吧。 :-)
      【解决方案5】:

      伪代码:

      while(1) {
          //ask question 
          if (answer == 'q') //quit
             //exit from the loop, e.g.: break or return
          //....
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-04-28
        相关资源
        最近更新 更多