找不到我的猜号游戏的问题。(三)

  • 本文关键字:问题 游戏 找不到 c
  • 更新时间 :
  • 英文 :


我已经设法创建了一个游戏,试图猜测用户在想什么,但问题是,猜测的数字是存储到l_guess或h_guess取决于条件满足,我怎么能将它存储到第三个变量,这将是新的猜测使用下一个猜测?我的问题可能不是很清楚,但我希望看看代码会有所帮助。

// Assignment #7
// This program guesses the number that you have picked.
#include <stdio.h>
#define UPPER 100
#define first_Guess 50
char answer;
int h_Guess;
int l_Guess;
int new_Guess;
void game_Start();
void game_Play();
int main(void)
{
h_Guess = (first_Guess + UPPER) / 2;
l_Guess = first_Guess;
game_Start();
game_Play();
return 0;
}
void game_Start()
{
printf("Hello, and welcome to the guessing gamen Think of a number between 0 and 100 and I will try to guess it.n");
printf("... Is it %d ?", first_Guess);
printf("If the answer is correct, press (c) n If your number is higher, press (h)n if your number is lower, press (l)n");
scanf("%c", &answer);
}
void game_Play()
{
while (answer != 'c')
{
if (answer == 'h')
{
printf("Then ... is it %d?n", h_Guess);
h_Guess = (h_Guess + UPPER) / 2;
}
else if (answer == 'l')
{
l_Guess = l_Guess / 2;
printf("Then ... is it %d?n", l_Guess);
}
scanf("%c", &answer);
}
printf(" I knew it, I am a geniusn");
}

你没有正确地重新设置猜测的边界。

void game_Play()
{
int upper = MAX;
int lower = MIN;
int guess = FIRST_GUESS;
while (answer != 'c')
{
if (answer == 'h')
{
lower = guess;
}
else if (answer == 'l')
{
upper = guess;
}
guess = (upper + lower) / 2;
printf("Then is it %d?n", guess);
scanf(" %c", &answer);
}
printf(" I knew it. I am a genius.n");
}

注意:你的常量定义为他们是,有一个边缘情况的错误在我的解决方案。你能找到它并修好它吗?

最新更新