C - 编程HI-LO游戏

  • 本文关键字:游戏 HI-LO 编程 c
  • 更新时间 :
  • 英文 :

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <math.h>
#include <time.h>
#include <windows.h>
#include <stdlib.h>
#include <ctype.h>
#define PI 3.14159
#define PLANK_CONSTANT 6.626E-034
#define MASS_OF_ELECTRONS 9.109E-031
#define VELOCITY_OF_LIGHT 299800000
void hilo();
int main()
{
char check;
while (1)
{
hilo();
printf("Would you like to play again (y/n)? ");
scanf('%c', &check);
if (check != 'y' || check != 'y')
{
break;
}
}
system("pause");
return 0;
}
void hilo()
{
srand(time(NULL));
int count = 0;
int guess;
int randomnumber = rand() % 101;
while (1)
{
printf("Guess the number: ");
scanf("%d", &guess);
if (guess == randomnumber)
{
printf("Hooray, you have won!n");
break;
}
if (guess < randomnumber)
printf("Wrong Number, Try again! The number you guessed is too lown");
if (guess > randomnumber)
printf("Wrong Number, Try again! The number you guessed is too highn");
count++;
if (count == 7)
{
printf("Sorry you lose, the number is: %dn", randomnumber);
break;
}
}
}

有人可以帮助我吗?猜了7次数字并"打破"后,C说有错误"Exception thrown at 0x586A4B02 (ucrtbased.dll) in ALLlabs.exe: 0xC0000005: Access violation reading location 0x00002563."

我试图到处搜索休息的问题,但我找不到问题所在。你们能告诉我代码有什么问题吗?您可以复制代码以尝试自己使用它。

是这一行:

scanf('%c', &check);

它使用单引号 - 因此"%c"变成了一个数字,而不是一个字符串。

试试这个:

scanf(" %c", &check);

请记住在启用所有警告的情况下进行编译。 不要忽略编译器警告!

有一些关于在"%c"之前使用空格的注意事项。

最新更新