gpt4 book ai didi

c - 返回代码顶部/重置代码

转载 作者:行者123 更新时间:2023-11-30 18:46:29 29 4
gpt4 key购买 nike

我对编码很陌生。我正在做任务。如果他们回答y再次执行您想要的操作,我希望我的代码基本上再次重新启动。

#include <stdio.h>
#include <stdlib.h>

int main(int argc, const char * argv[]) {
char play;
int choice;

printf("Welcome to Two doors.\n");
printf("Would you like to play? (y/n): ");
scanf("%s", &play);

if (play == 'y')
{
printf("You are a prisoner in a room with 2 doors and 2 guards.\n");
printf("One of the doors will guide you to freedom and behind the other is a hangman --you don't know which is which.\n");
printf("One of the guards always tells the truth and the other always lies. You don't know which one is the truth-teller or the liar either.\n");
printf("You have to choose and open one of these doors, but you can only ask a single question to one of the guards.\n");
printf("What do you ask so you can pick the door to freedom?\n\n");
printf("\t1. Ask the truth-guard to point to the door of doom.\n");
printf("\t2. Ask the liar-guard to point to the door of doom.\n");
printf("\t3. Doesn't matter which one you pick.\n");
printf ("Enter a number between 1 and 3: ");
scanf("%i" , &choice);

char *answer = "No matter which one you choose the guards both tell you which door leads to death, and therefore you can pick the other door.\n";

switch (choice) {
case 1:
printf(answer);
break;
case 2:
printf(answer);
break;
case 3:
printf(answer);
break;
default:
break;
}

printf("Would you like to play again? (y/n): ");
scanf("%s", &play);
if (play == 'y')
printf(main);
}

return 0;
}

最佳答案

一个好主意是将需要重复多次的部分包装在 do {} while () loop 中。像这样的事情:

do {
printf("You are a prisoner in a room with 2 doors and 2 guards.\n");
printf("One of the doors will guide you to freedom and behind the other is a hangman --you don't know which is which.\n");
printf("One of the guards always tells the truth and the other always lies. You don't know which one is the truth-teller or the liar either.\n");
printf("You have to choose and open one of these doors, but you can only ask a single question to one of the guards.\n");
printf("What do you ask so you can pick the door to freedom?\n\n");
printf("\t1. Ask the truth-guard to point to the door of doom.\n");
printf("\t2. Ask the liar-guard to point to the door of doom.\n");
printf("\t3. Doesn't matter which one you pick.\n");
printf ("Enter a number between 1 and 3: ");
scanf("%i" , &choice);

char *answer = "No matter which one you choose the guards both tell you which door leads to death, and therefore you can pick the other door.\n";

switch (choice) {
case 1:
printf(answer);
break;
case 2:
printf(answer);
break;
case 3:
printf(answer);
break;
default:
break;
}

printf("Would you like to play again? (y/n): ");
int result = getchar();
while (result == '\n' || result == EOF)
result = getchar();
}
if (result == EOF) break;
play = (char)result;
while (play == 'y')

UPD:如user3629249指出,问题样本中的输入读数不正确。所以我修复了它。

关于c - 返回代码顶部/重置代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51143339/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com