猜数字c语言
❶ c语言小游戏:猜数字 随机一个1-100之间的数,根据数据输入进行提示
//小游戏:猜数字 随机一个1-100之间的数,根据数据输入进行提示
#include <stdlib.h>
#include <time.h>
int main(void){
int value=0;
int num=0;
srand((unsigned int) time(NULL));
num=rand()%100+1; //1-100
while(1){
scanf("%d",&value);
if(num>value){
printf("您猜小了\n");
}
else if(num<value){
printf("您猜大了\n");
}
else if(num=value){
printf("恭喜您猜对了\n");
break;
}
}
return 0;
}
❷ c语言编程:猜数字游戏
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define UI unsigned short int
void game()
{
UI answer;
UI input;
UI lower=1;
UI upper=100;
UI count=0;
srand(time(NULL));
do{answer=rand()%101;}
while(answer==0);
puts("Welcome to the number guessing game!");
do
{
puts("Please enter an integer from 1 to 100 (again):");
scanf("%lu",&input);
getchar();
count=count+1;
if(input==answer){puts("You succeeded!");printf("The number of time(s) you entered is %lu.\n",count);}
else
{
puts("You failed!");
if(input<answer){if(input>lower){lower=input;}puts("The answer is greater than your input.");}
else {if(input<upper){upper=input;}puts("The answer is less than your input.");}
printf("The answer is from %lu to %lu.\n",lower,upper);
}
}
while(input!=answer);
}
#undef UI
int main()
{
game();
system("Pause");
return 0;
}
❸ 用c语言编写一个程序,由计算机“想”一个数请人猜,如果猜对了,程序结束,否则输出“错误”。
简单的写了下,供参考:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#define Range 5
int guess(int k){
int input;
printf("请输入数字:\t");
scanf("%d",&input);
if(input==k)return 1;
if(input>k)return -1;
if(input<k)return -2;
}
int main(){
int aim;
srand((unsigned)time(NULL));
aim = rand()%Range;
int i,flag;
i=0;
flag=0;
printf("\n猜一个 0 - %d 的数字:",Range);
while(flag!=1){
flag=guess(aim);
if(flag==-1)printf("错误,大了!\n");
if(flag==-2)printf("错误,小了!\n");
if(flag==1)printf("恭喜答对! 此数是:%d\n",aim);
i++;
}
return 0;
}
❹ C璇瑷 缂栧啓涓涓鐚沧暟瀛楁父鎴忥纴瑙勫垯锛氢娇鐢ㄩ殢链哄嚱鏁颁骇鐢熶竴涓5浣嶆暟锛岃緭鍏ョ寽娴嬫暟鎹锛屽垽鏂鍑犱釜姝g‘锛岃︾粏濡备笅锛
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
void main()
{
intA=0,B,i,j,k=0,a[5],g[5];
printf("璇疯緭鍏ヤ綘鐚沧祴镄勪竴涓5浣嶆暟鏁板瓧\n");
scanf("%d",&k);
while(k<12345||k>99999)
{
printf("浣犺緭鍏ヤ简涓涓闱炲父瑙勬暟鎹锛岃烽吨鏂拌緭鍏\n");
scanf("%d",&k);
}
srand((unsignedint)time(NULL));
for(i=0;i<5;i++)
{
a[i]=rand()%9+1;
for(j=0;j<i;j++)
while(a[i]==a[j])
{
a[i]=rand()%9+1;
j=-1;
}
}
printf("闅忔満缁椤嚭镄勬暟瀛椾负\n");
for(i=0;i<5;i++)
printf("%d",a[i]);
printf("\n");
for(i=4;i>=0;i--)
{
g[i]=k%10;
k=(k-g[i])/10;
}
for(i=0;i<5;i++)
for(j=0;j<5;j++)
{
if(g[i]==a[j])
{
A++;
if(i==j)
B=i+1;
}
}
if(A==5)
printf("浣犵寽娴嬬殑鍏ㄩ儴姝g‘\n");
else
{
printf("浣犵寽瀵圭殑涓鏁:%d\n",A);
printf("浣犵寽瀵规暟瀛楃殑浣岖疆:绗%d涓\n",B);
}
}
❺ 猜数字游戏C语言编程
有个几年前编好的猜数字,但是代码找不到了,没有没有记忆成绩的功能,楼主要不?
❻ 猜数字游戏c语言编程一到五
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
intmain(){
srand((unsigned)time(NULL));
intguessNum=0;
while(1)
{
intrandNum=rand()%6;
printf("请输入1-5的数字:");
scanf("%d",&guessNum);
if(guessNum<1||guessNum>5){
printf("输入不合法! ");
continue;
}
if(randNum==guessNum){
printf("恭喜你,猜对了!");
break;
}else
{
printf("很遗憾,差一点,继续努力! ");
}
}
}