gpt4 book ai didi

c - C语言的猜字游戏

转载 作者:太空宇宙 更新时间:2023-11-04 08:36:17 25 4
gpt4 key购买 nike

我在编写程序时遇到了一些问题。该程序本质上是猜词游戏刽子手。我有一个词,用户有很多机会猜这个词。我无法让游戏正确循环并在猜测次数用完时结束。另外,我制作了第二个数组,与我的话数组的大小相同,但这个数组充满了破折号。如果猜对了一个字母,我需要正确的字母出现在破折号周围。如果有人可以提供帮助,我将不胜感激。

#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);
}
}
}

最佳答案

您的代码有几个问题。我会尽量指出它们

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 ( if word is completed and guessed correctly )
{
printf("\nCongrats! You guessed the secret word!\n");
}

上面的if循环在c语言中没有意义

else if ( guess != myWord )
{
printf("\nSorry. That is not a correct letter. Please guess again:\n");
scanf(" %c", &guess);
}

上面的 elseif 语句将字符变量与数组的基地址进行比较

关于c - C语言的猜字游戏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26127324/

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