我想制作一个询问用户问题的程序,然后用户选择答案,然后程序返回原始问题目录。例如:
你最喜欢的颜色是什么? 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;
}
我觉得这就是我需要某种返回声明的地方,它让我回到原来的问题“你想在太空做什么”,这样用户就可以提出另一个问题。
答案 0 :(得分:1)
从你的问题来看,目前尚不清楚你究竟想要实现什么,但我认为一个简单的循环就可以了。
检查此代码
#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;
}
但是,当选项数量增大时,最好使用switch
。试试看。快乐的编码!!
答案 1 :(得分:1)
伪代码:
while(1) {
//ask question
if (answer == 'q') //quit
//exit from the loop, e.g.: break or return
//....
}
答案 2 :(得分:1)
你应该使用一个循环。您还应该查看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);
}
}
}
答案 3 :(得分:0)
循环分开你需要评论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" );
}
将由跳转表实现,它会更快。
答案 4 :(得分:0)
使用do ...而不是因为你提到你问至少一次这个问题。 这就是你如何做到的
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");