#include"stdio.h"
#include"stdlib.h"
void main()
{
int problem,guess=1,a;
char ch;
problem=(int)(rand()%899+1);
scanf("%f%d",&ch,&guess);
for(a=0;ch!='n';a++)
for(a=0;guess==problem;a++)
{if(guess>problem)
printf("too high");
if(guess<problem)
printf("too low");
scanf("%d",&guess);
}
printf("%d",problem);
scanf("%f",&ch);
}为什么运行不起来 scanf("%f%d",&ch,&guess);只输入完这个语句够,界面就不动了
帮你改了一下,大概你要的是这种效果吧。
#include"stdio.h"
#include"stdlib.h"
int main()
{
int problem,guess=1;
char ch;
problem=(int)(rand()%899+1);
scanf("%c%d",&ch,&guess);
for(;ch!='n';){
for(;guess!=problem;)
{if(guess>problem)
printf("too high\n");
if(guess<problem)
printf("too low\n");
scanf("%d",&guess);
}
printf("You are right!The number is %d\n",problem);
scanf("%c",&ch);
}
return 0;
}
第二个for循环的==那里有错误?
for循环中间一个分号前面是判断条件吧,也就是说,如果你的ch=n,根本就不会进入循环了 。我看看rand什么意思。
#include"stdio.h"
int main()
{
int problem,guess=1;
char ch;
problem=(int)(rand()%899+1);
scanf("%c%d",&ch,&guess);
for(;ch!='n';){
for(;1;){
if(guess>problem)
printf("too high\n");
if(guess<problem)
printf("too low\n");
if(guess==problem){
printf("You are right!The number is %d\n",problem);
break;
}
scanf("%d",&guess);
}
getchar();
scanf("%c",&ch);
}
return 0;
}
感觉这样可能好一点点。
scanf("%f%d",&ch,&guess); ch是字符,应该是%c
problem=(int)(rand()%899+1); rand的标准式你找个实例看一下,注意+1的位置
for(a=0;ch!='n';a++);for(a=0;guess==problem;a++) 逻辑乱,建议While
你是要做一个猜数字的简单游戏吧,百度一下,看看别人的,代码,你会进步的
醉卧美人膝先生的建议应该采纳