gpt4 book ai didi

c true循环语句在hangman中不断实现

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

kk 我修复了它,但我认为还有一个声明是 if (hit == length) then 在 while 循环之外 printf 你赢了。但我现在的问题是我应该把 if (hit == length)

放在哪里
#include <stdio.h>
#include <conio.h>
#include <string.h>

//function prototypes

void game(char [], char [], int);
int main()
{
char word[20] = {'d', 'u','c','k'};
char guessed[20] ={'*s', '*s','*s','*s' };
int length = strlen(word);

game(word,guessed, length);

getch();
return 0;
}
void game(char answer[], char guess[], int length)
{
int life = 5;
int x = 0;
int y = 0;
char letter;

while (x < length && life > 0){
int hit = 0;
printf("enter letter\n");
scanf(" %c",&letter);
for (y = 0; y < length; ++y) {
if (answer[y] == letter && guess[y] != letter)
{
++hit;
guess[y] = letter;
}
}
if (!hit)
{
x += hit;
printf("try again\n");
life = life - 1;
printf("%d tries remaining \n", life);
}
else {
printf("keep going\n");
}
}
}

最佳答案

您的条件 if (!hit) 检查 hit 是否为 0。

但是,您仅在过程开始时将其初始化为 0 一次 - 而不是在 while(..) 循环的每次迭代中。
因此 - 一旦 hit 设置为非 0(在某些迭代中),它将始终保持该状态(非 0)。

您应该在 while 循环内设置 hit = 0;(可能在其开始时)。

关于c true循环语句在hangman中不断实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22906289/

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