我設法創建了一個嘗試猜測用戶在想什么的游戲,但問題是猜測的數字根據滿足的條件存盤到 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 game\n 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 genius\n");
}
uj5u.com熱心網友回復:
您沒有正確地重新設定猜測的界限。
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");
}
注意:按原樣定義常量后,我的解決方案中存在極端情況錯誤。你能找到它并修復它嗎?
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/464202.html
標籤:C
上一篇:執行緒專案問題|如何在C中等待/來自多個執行緒的信號
下一篇:平均輸出不正確
