我有一個程序,我試圖運行。它編譯但沒有按預期運行(這部分只運行):程序沒有完全運行
#include <stdio.h>
int main(int argc, const char * argv[]) {
char play;
char choice;
char answer;
printf("Welcome to Two doors.\n");
printf("Would you like to play? (y/n): ");
scanf("%c", &play);
if (play == 'y') {
printf("\nYou 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");
scanf("%c", &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("%c", answer);
break;
case 2:
printf("%c", answer);
break;
case 3:
printf("%c", answer);
break;
default:
break;
}
}
return 1;
}
好像我無法讓代碼在其整體運行。這裏是我在輸入'y'後提問時得到的結果:
Welcome to Two doors.
Would you like to play? (y/n): y
You are a prisoner in a room with 2 doors and 2 guards.
One of the doors will guide you to freedom and behind the other is a hangman --you don't know which is which.
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.
You have to choose and open one of these doors, but you can only ask a single question to one of the guards.
What do you ask so you can pick the door to freedom?
1.Ask the truth-guard to point to the door of doom.
2.Ask the liar-guard to point to the door of doom.
3.Doesn't matter which one you pick.
(lldb)
有沒有人可以幫助我?
謝謝!
它究竟在哪停止? – Carcigenicate
這是錯誤的。如果你不知道哪個警衛是騙子,你怎麼能問這個騙子後衛?這個問題不能很好地處理多種選擇,因爲一旦你知道解決方案是顯而易見的(向任何一個警衛詢問另一個**後衛會說什麼是自由之門)。 – Olaf
重新提出您的問題:爲什麼忽略編譯器警告?如果您在忽略它們的情況下調用未定義的行爲,那麼遇到問題也就不足爲奇了。 – Olaf