我在編寫程序時遇到了一些問題。該方案本質上是猜謎遊戲hang子手。我有這個商店一個字,然而用戶有很多機會猜測這個詞。我無法讓遊戲正確循環,並在猜測次數用完時結束。此外,我做了第二個數組,與我的單詞數組的大小相同,但是這個數組填充了破折號。如果信件被正確猜測,我需要正確的字母在破折號周圍出現。如果有人能幫忙,我會很感激。C猜字遊戲
#include <stdio.h> // Input and output operations
#include <stdlib.h> // General purpose functions
int main(void)
{
FILE*fp; // File pointer variable
fp = fopen("Quiz 6", "w");
int numTries, tries, i; // Declaration of variables
char guess;
char myWord [6] = {'a','p','p','l','e','\0'}; // Initialization of variables
char dashedArray [6] = {'-','-','-','-','-','-'};
numTries = 0;
i=0;
tries = 0;
printf("\nLet's play a game! The objective of the game is to guess my secret word. You will have
five chances to guess.\n");
printf("\nLet's get started!\n");
printf("\nPlease enter a letter: ");
scanf("%c", &guess);
for (i = 0; i < 4; i++)
{
numTries = numTries +1;
if (guess == myWord [0])
{
printf("\n %c----", myWord [0]);
printf("\nGood Guess!\n");
printf("\nPlease guess another letter: ");
scanf(" %c", &guess);
}
if (guess == myWord [1] && myWord [1])
{
printf("\n-%c%c--", myWord [1], myWord [1]);
printf("\nGood Guess!\n");
printf("\nPlease guess another letter: ");
scanf(" %c", &guess);
}
if (guess == myWord [3])
{
printf("\n---%c-", myWord [3]);
printf("\nGood Guess!\n");
printf("\nPlease guess another letter: ");
scanf(" %c", &guess);
}
if (guess == myWord [4])
{
printf("\n----%c", myWord [4]);
printf("\nGood Guess!\n");
printf("\nPlease guess another letter: ");
scanf(" %c", &guess);
}
if (if word is completed and guessed correctly)
{
printf("\nCongrats! You guessed the secret word!\n");
}
else if (guess != myWord)
{
printf("\nSorry. That is not a correct letter. Please guess again:\n");
scanf(" %c", &guess);
}
}
}
你忘了提問了。你需要什麼幫助? – 2014-09-30 18:12:24
是的,這個網頁上唯一的問號(?)是@DavidSchwartz's ... – 2014-09-30 18:15:40
@DavidSchwartz:我認爲他問他的代碼的幫助。他提到的工作不正常 – Haris 2014-09-30 18:17:05