所以这是我写的一些代码,我是从 c 开始的大麦,所以请耐心等待,我的代码对我来说很有意义,但它不能正常工作,当我试图拒绝停止播放并显示平均值时,它会重复播放功能。
基本上,我的程序从 1 到 100 猜测用户编号,无论用户想出什么数字。这是一些错误的输出,只是为了给您一个想法。玩游戏功能运行良好,但不是主要功能。
Playing guess my number game
is your number 50 ?=
Great!
Play again? (y for yes n for no): is your number 50 ?=
Great!
Play again? (y for yes n for no): is your number 50 ?n
等等我找不到我出错的地方有什么建议吗?
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <stdbool.h>
int playgame(void);
main() {
int numberofplays = 0;
float average = 0;
int numberguesses = 0;
printf("Playing guess my number game\n");
printf("-------------------------------\n");
bool play = true;
int xtry = 0;
int totaltries = 0;
char answer;
do {
xtry = playgame();
numberofplays++;
totaltries = totaltries + xtry;
printf("Play again? (y for yes n for no): ");
answer = getchar();
if(answer =='n')
play = false;
} while(play==true);
average = (float)(totaltries/numberofplays);
printf("average is %f tries per game\n",average);
return 0;
}
int playgame(void) {
int guessnumber = 50;
int xnumber = 25;
int count=0;
char x;
bool stillguessing = true;
while(stillguessing == true) {
printf("is your number %d ?", guessnumber);
x = getchar();
if(x=='=') {
count++;
printf("Great!\n");
stillguessing=false;
return count;
}
else if(x == '<') {
guessnumber = guessnumber - xnumber;
xnumber = xnumber/2;
if(xnumber<1) xnumber = 1;
count++;
}
else if (x == '>') {
guessnumber = guessnumber +xnumber;
xnumber = xnumber/2;
if(xnumber<1) xnumber = 1;
count++;
}
}
}