我想制作一个向用户提问的程序,然后用户选择一个答案,然后程序返回到问题的原始目录。例如:
你最喜欢什么颜色? 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/