我输入了一个数,猜错了之后它就一直刷屏,它理论上应该停下来等我再输入一个数然后再运行的。
你需要把生成随机数的语句挪到循环外面,其次scanf中的\n去掉。
源代码存在的问题:
①一次游戏应该只生成一次随机数,所以应该放到while循环外面,而且这个随机数的生成也有问题,最好是利用srand()加一个随机化种子
int a=rand();
②在scanf()输入函数中的%d后加\n是不对的,会出现输入必须换行的情况,无需加入
scanf("%d\n", &c);
解决问题后的代码
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h> //time()函数的头文件
int main()
{
int c = 0, try = 0;
srand((unsigned int)time(NULL)); //使用srand函数和time函数使程序每次生成的随机数不同
int a = (rand() % 100) + 1; //每次生成1 - 100之间的随机数
printf("\n#####猜数字游戏开始#####\n\n");
while (1)
{
printf("请输入一个任意整数:\n");
scanf("%d", &c);
try++;
if (c == a)
{
printf("你猜对了,程序终止。\n");
break;
}
else
{
printf("你猜错了,请再试一次。\n");
}
}
printf("你一共用了%d次才猜出来。\n", try);
char wait_end_char; //在程序最后等待输入,防止程序结束后闪退
scanf("%c", &wait_end_char);
return 0;
希望对您有帮助
很简单的问题,把scanf语句里面的'\n' 去掉就ok
将scanf语句里面的'\n' 去掉
if里不用break 放到if外面
把16行的 int a=rand();移到第11行
#include <iostream>
#include <stdlib.h>
#include <ctime>
using namespace std;
int main()
{
cout << "***********************欢迎来到猜数字小游戏***********************" << endl << endl << endl;
cout << "游戏规则:" << "猜到系统给的随机数字就能获胜、反之失败!一次只有10次机会." << endl;
system("color 70"); //改变颜色
int n,m,j=0;
srand((int)time(NULL)); // 设置一个随机数的种子、以目前时间为变化(默认为五位数)
n = rand() % 100 + 1; //目的是为把数字控制字在1-100之间;
cout <<"请您在1-100之间输入一个数字" << endl;
cin >> m;
while (m != n) //判断您输入的数是否和随机数相等
{
if (m < n)
{
j++;
system("cls");
cout << "您输入的数小了" << endl;
cout << "系统已自动为您缩小范围:" << endl;
cout << "****************还剩下" << 10 - j << "次机会****************" << endl;
cout << m << "-" << 100<< endl;
system("color E0");
}
else
{
j++;
system("cls");
cout << "您输入的数大了" << endl;
cout << "系统已自动为您缩小范围:" << endl;
cout << "****************还剩下" << 10 - j << "次机会****************" << endl;
cout << 0 << "-" << m << endl;
system("color 80");
}
if (j == 10)
{
system("cls");
cout << endl<<endl<<endl<<endl<<"次数已用尽闯关失败!" << endl << endl << endl << endl;
system("color 40");
cout << "**************************游戏结束**************************" << endl;
system("pause");
}
cin >> m;
}
cout << "恭喜您闯关成功" << endl << endl << endl;
cout << "**************************游戏结束**************************" << endl;
return 0;
}
(1)原代码存在的不足
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
int main()
{
int c = 0, try = 0;
while (1)
{
int a = rand(); //一次游戏应该只有一个随机数,因此rand()应该在while外面
printf("请输入一个任意整数\n");
scanf("%d\n", &c); //scanf的输入中加入换行符\n容易出错
try++;
if (c == a)
{
printf("你猜对了,程序终止。\n");
break;
}
else
printf("你猜错了,请再试一次。\n");
}
printf("你一共用了%d次才猜出来。", try);
return 0;
}
(2)修改后的代码
#include <math.h>
#include <stdlib.h>
#include <stdio.h>
#include <time.h> //为了使用time函数初始化srand而引入time.h头文件
int main()
{
int c = 0, try = 0;
srand(time(0)); //使用srand函数和time函数使程序每次生成的随机数不同
int rand_max = 10; //设置最大的随机数,使得可以在合理次数内才出来
int a = (rand() % rand_max) + 1;
printf("\n#####猜数字游戏开始#####\n\n");
while (1)
{
printf("请输入一个任意整数:\n");
scanf("%d", &c);
try++;
if (c == a)
{
printf("你猜对了,程序终止。\n");
break;
}
else
{
printf("你猜错了,请再试一次。\n");
}
}
printf("你一共用了%d次才猜出来。\n", try);
char wait_end_char; //在程序最后等待输入,防止程序结束后闪退
scanf("%c", &wait_end_char);
return 0;
}
(3)代码运行结果截图
random 生成一个随机整数,范围很大,如果不给提示的猜,平均猜中的次数为 2的16或者32次方
每次猜了一次,就重新生成随机数,调用random函数, 代表你猜中的机会只有一次
#include<stdio.h>
#include<stdlib.h>
void text(){
int c=0;
int try1=0;
while(1){
int a=rand();
printf("请输入一个整数:\n", a);
scanf("%d", &c);
try1++;
if(c==a){
printf("你猜对了,程序终止。\n");
break;
}
printf("你猜错了,请再试一次。\n");
}
printf("你一共用了%d次才猜出来\n", try1);
}
int main(int argc, char const *argv[])
{
text();
return 0;
}