c - 如何重复执行一组操作?

标签 c loops return repeat

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

你最喜欢什么颜色? 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;
}

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

最佳答案

伪代码:

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

关于c - 如何重复执行一组操作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20553088/

相关文章:

c - 为什么我的程序输出在 Windows 中闪烁并关闭?

c - 如何使用代码中已定义的变量为变量设置值?

c - 如何从 Objective-C 方法返回 C 指针引用?

javascript - 返回键不适用于许多文本区域

c - 解决错误 "return makes pointer from integer without a cast"

c - 我如何开始在 UBUNTU 9.04 上进行 CUDA 开发?

c++ - 使用 Ninja 构建系统,我可以清理中间构建产品吗?

c - 查找输入的素数之后的第一个素数

javascript - 为什么这个简单的 for 循环没有按预期工作?

Delphi:循环记录中的字节