本文目录一览:
C语言:猜数问题!!
#include stdio.h
int main()
{
int tar, guess, times, again = 1;
char ch;
while (again == 1)
{
scanf("%d", tar);
times = 1;
while (1)
{
scanf("%d", guess);
if (guess == tar)
{
if (times == 1)
printf("Bingo!\n");
else if (times = 3)
printf("Lucky You!\n");
else
printf("Good Guess!\n");
break;
}
if (times = 6)
{
printf("Game Over.\n");
break;
}
if (guess tar)
printf("Too big.\nTry again:");
else
printf("Too small.\nTry again:");
times++;
}
again = 0;
do
{
printf("Do you wanna play again? "
"If yes, press Y or y. Otherwise press N or n.");
scanf(" %c", ch);
if (ch == 'Y' || ch == 'y')
again = 1;
else if (ch == 'N' || ch == 'n')
again = -1;
} while (again == 0);
}
return 0;
}
如何用c语言写一个猜数字游戏,我输入正确数字,让电脑自动去猜
网页链接
与上述引用同一种问题。
//二分法查找数据,1-100内查找一个数据,查找一次需要判断一下Yes or No.
#includestdio.h
int main()
{
//初始化
int low = 1, high = 100, guess, mid = low, k = 0;
char a = 'n';
//主体
printf("Please input integer from 1 to 100,I will try to guess it.\n ");
scanf("%d", guess);
printf("If i guess true input 'y',else input 'n'.\n\n");
while (1)
{
printf("测试循环了 %d 次\n",k++);
mid = (low + high) / 2;
printf("Um...is your number is %d ?\n", mid);
getchar(); //作用:“吃掉”‘\n’,否则下一行的scanf函数会读入回车符号。
scanf("%c/n", a);
if (a != 'y')
{
if (mid guess) //猜测数在mid--high之间
{
low = mid;
}
else if (mid guess) //猜测数在low--mid之间
{
high = mid;
}
}
else break;
}
return 0;
}
C语言中如何编写让计算机“猜数”
#include stdio.h
#include stdlib.h
#include time.h
int main()
{
short i=0;
short sjs;/*随机数*/
short srs=0;/*玩家输入的数*/
srand((unsigned)time(NULL));
sjs=rand()%100;
sjs++;
printf("猜数字游戏\n");
printf("该数字在1到100之间\n");
if(i=10)
{
while(srs!=sjs)
{
printf("请输入你所猜的数字: ");
scanf("%hd",srs);
if(srssjs)
printf("小了\n");
if(srssjs)
printf("大了\n");
}
}
printf("猜对了,就是这个数: %hd\n",sjs);
return 0;
}
C语言编程 猜数游戏
#include
#include
//用到了rand函数,所以要有这个头文件
#include
//用到了time函数,所以要有这个头文件
int
main()
{
int
number;
//number用于存储随机数
int
guess=0;
//guess用于存储玩家猜的数
srand((unsigned)
time(null));//用系统时间作为rand函数使用的种子
number=rand()%100;
//随机除以100,取余数
number++;
//余数加1
printf("猜数字游戏\n");
printf("该数字在1到100之间\n");
while(guess!=number)
{
printf("请输入您所猜的数:");
scanf("%d",guess);
//如果玩家猜的数较小,给予提示
if
(guess
number)
{
printf("大了\n");
}
}
//猜中则循环结束,输出猜中的数字
printf("猜对了,这个数字就是:%d\n",number);
return
0;
}