① 怎樣用c語言實現猜字游戲
//電腦隨機生成一個50以內的整數,然後從鍵盤輸入一個整數猜系統生成的數,直到猜對為止,並輸出所猜次數。
#include<stdio.h>
#include
<stdlib.h>
#include<time.h>
void
main()
{
int
count=0;
int
flag=1;
do
{
srand(time(NULL));
int
num1,num=rand()%51;
printf("請輸入您要猜的數(0-50):");
scanf("%d",&num1);
if(num1>50)
{
printf("輸入有誤,請重新輸入\n");
continue;
}
printf("隨機數為:%d\n",num);
count++;
if(num1==num)
{
flag=0;
}
printf("猜錯了!3請繼續\n\n\n");
}while(flag);
printf("%d",count);
}
② C語言用循環猜字游戲:使用隨機數生成一個三位數作為謎底進行猜字游戲,每次猜的時候給出提示,
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int a,b,c=10;
srand(time(NULL));
a=rand()%1000;
printf("系統已經給出了一個0-999的數,請輸入一個數。\n");
while(1)
{ c--;
scanf("%d",&b);
if(b>a)
printf("猜大了,還剩%d次機會,請繼續\n",c);
if(b<a)
printf("猜小了,還剩%d次機會,請繼續\n",c);
if(b==a)
{ printf("恭喜你答對了\n");
printf("系統給的數是:%d",a);
break;}
if(c==0)
{ printf("你已經沒有機會了,請重新開始吧!\n");break;}
}
}
③ 設計一個猜字游戲的C語言程序
把下面的cout是輸出。
用c語言的輸出就行。
#include
<time.h>
#include
<stdlib.h>
int
main()
{
int
x;
cout<<"輸入任意數字開始。"<<endl;
//使用
輸入是否為
數字來判斷是否繼續猜
scanf("%d",&x);
while(x)
{
srand((int)time(0));
int
m
=
rand()%100+1;//生成隨機數m范圍1到100
int
i
=0,n;
for
(;i<10;i++)//判斷是否才對,10次為限,i用來計算才對次數
{
cout<<"input
the
number
you
guess:"<<endl;
scanf("%d",&n);
if(
n
>
m
)
cout<<"wrong,too
high!"<<endl;
if(
n
<
m
)
cout<<"wrong,too
low!"<<endl;
if(
n
==
m)
{cout<<"right,and
the
times
you
have
guessed
is"<<i<<"."<<endl;break;}
}
cout<<"輸入任意數字繼續。"<<endl;
scanf("%d",&x);
}
return
0;
}
④ C語言寫猜字游戲
/*你說的功能基本達到了,但是樣式沒有怎麼調整,你自己看下吧*/
#include<stdio.h>
#include<stdlib.h>
void getNum(int a[]) /*產生隨機數*/
{int i,j;
randomize();
a[0]=random(10);
for(i=1;i<4;i++)
{
a[i]=random(10);
for(j=i-1;j>=0;j--)
if(a[i]==a[j])
{i--;
break;
}
}
}
void inputNum(int b[]) /*輸入數字,並保證沒有重復數字*/
{int i,j;
puts("Input number:");
laber:;
scanf("%d%d%d%d",&b[0],&b[1],&b[2],&b[3]);
for(i=3;i>0;i--)
for(j=0;j<i;j++)
if(b[j]==b[i])
{puts("Input again:");
goto laber;
}
}
main()
{int a[4],b[4],c[10][4],A=0,B=0,correct[10][2],count=0,i,j;
char ch;
laber:;
system("cls");
getNum(a);
while(count<10) /*最多猜10次*/
{puts("Browse the historic document?(Y/N)"); /*是否查看歷史記錄*/
ch=getchar();
if(ch=='Y'||ch=='y')
for(i=0;i<count;i++)
{printf("%d:",i+1);
for(j=0;j<4;j++)
printf("%d\t",c[i][j]);
printf("%dA %dB",correct[i][0],correct[i][1]);
putchar('\n');
}
putchar('\n');
inputNum(b);
for(i=0;i<4;i++)
c[count][i]=b[i];
for(i=0;i<4;i++)
for(j=0;j<4;j++)
{if(a[i]==b[j]&&i==j)
A++;
else if(a[i]==b[j]&&i!=j)
B++;
}
if(A==4)
{puts("Congratulations!!");
getch();
break;
}
else
printf("%dA %dB\n",A,B);
correct[count][0]=A;
correct[count][1]=B;
count++;
A=0;B=0;
getchar();
}
getchar(); /*是否再猜一次*/
puts("Guess again?");
ch=getchar();
if(ch=='Y'||ch=='y')
goto laber;
}
⑤ 用C語言編寫一個猜數字游戲的程序(最好附帶說明)
有償,先程序後cash ,有意加我
⑥ C語言編寫一個猜字游戲,,,遇到了問題· 請教高人
把這句話放循環外。
printf("很遺憾,你沒猜中本次中獎號碼,謝謝你的參與:!\n");
另外,新定義個flag,表示被猜中了。初始化為false。
如果猜中,就在恭喜那個程序塊中設置為true。
然後在很遺憾那句前面加上對這個flag的判斷,如果=false,則輸出很遺憾這句話
⑦ 如何用c語言寫一個猜數字游戲,我輸入正確數字,讓電腦自動去猜
網頁鏈接
與上述引用同一種問題。
//二分法查找數據,1-100內查找一個數據,查找一次需要判斷一下YesorNo.
#include<stdio.h>
intmain()
{
//初始化
intlow=1,high=100,guess,mid=low,k=0;
chara='n';
//主體
printf("Pleaseinputintegerfrom1to100,Iwilltrytoguessit. ");
scanf("%d",&guess);
printf("Ifiguesstrueinput'y',elseinput'n'. ");
while(1)
{
printf("測試循環了%d次 ",k++);
mid=(low+high)/2;
printf("Um...isyournumberis%d? ",mid);
getchar();//作用:「吃掉」『 』,否則下一行的scanf函數會讀入回車符號。
scanf("%c/n",&a);
if(a!='y')
{
if(mid<guess)//猜測數在mid--high之間
{
low=mid;
}
elseif(mid>guess)//猜測數在low--mid之間
{
high=mid;
}
}
elsebreak;
}
return0;
}
⑧ c語言,編寫一個猜數字游戲
源碼如下:
/*File:guess.c*/
#include<stdio.h>/*standardinput&outputsupport*/
#include<stdlib.h>/*srand()rand()*/
#include<time.h>/*time()*/
/*宏定義*/
#defineNUMBER_LENGTH5/*隨機數長度*/
#defineNUMBER_LIMIT10/*隨機數限制,每一位0-9*/
#defineINPUT_LENTH128/*輸入緩沖區大小*/
chargoal[NUMBER_LENGTH]={0};/*保存隨機數*/
charflag[NUMBER_LIMIT]={0};/*保存隨機數標志,保證不重復*/
charinput[INPUT_LENTH]={0};/*保存輸入*/
/*初始化用於保存數據的數組*/
voidinitData()
{
inti=0;
while(i<NUMBER_LENGTH)
goal[i++]=0;
i=0;
while(i<NUMBER_LIMIT)
{
flag[i++]=0;
}
}
/*初始化用於保存緩沖區的數組*/
voidinitBuffer()
{
inti=0;
while(i<INPUT_LENTH)
input[i++]=0;
}
/*顯示猜測結果*/
voiddisplay()
{
intcount=0;
inti=0;
while(i<NUMBER_LENGTH)
{
if(input[i]==goal[i])
{
printf("%c",'o');
count++;
}
else
{
printf("%c",'x');
}
i++;
}
printf(" RIGHT:%dbit(s) ",count);
if(count==NUMBER_LENGTH)
{
printf("Youwin!Thenumberis%s. ",goal);
exit(0);
}
}
/*生成隨機數*/
voidgeneral()
{
/*以時間作為時間種子保證生成的隨機數真正具有隨機性質*/
srand((unsignedint)time(NULL));
inti=0;
while(i<NUMBER_LENGTH)
{
chartmp;
do
{
tmp='0'+((i!=0)?(rand()%10):(1+rand()%9));
}while(flag[tmp]!=0);
flag[tmp]=1;
goal[i++]=tmp;
}
}
/*輸入方法,用於猜測*/
voidguess()
{
printf(": ");
scanf("%s",input);
display();
initBuffer();
}
/*主函數,程序主框架*/
intmain(intargc,constchar*argv[])
{
initData();
initBuffer();
general();
while(1)guess();
return0;
}
==============================================
運行結果見附圖,希望我的回答能夠對你有所幫助。
⑨ C語言編寫猜數字游戲
#include <stdlib.h>
#include <stdio.h>
//#include <iostream>
unsigned char Num[4] = {0},getNum[4] = {0x20,0x20,0x20,0x20};
void help(void)
{
char i =0,j = 0;
for(i =0;i<4;i++)
{
for(j = 0;j<4;j++)
if(Num[j] == getNum[i])
{
if(i==j)
printf("%dth is correct!\r\n",i);
else
{
printf("%d is a correct data\r\n",getNum[i]);
}
}
else
{
if(i == 3&&j==3)
{printf("/********************small game*****************/\
input 4 data gess the random data,input h can get help\
");}
}
}
}
void main( void )
{
int orginal = 0,getdata = 0,i = 0,j = 0;
// { int a;a = 878*101;cout<<a<<endl}
//first = 0,second = 0,third = 0,fourth = 0;
/* Seed the random-number generator with GetTickCount so that
the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
orginal = rand();
Num[3] = orginal%10000/1000;
Num[2] = orginal%1000/100;
Num[1] = orginal%1000%100/10;
Num[0] = orginal%10;
// printf(" %d",orginal);
for(i = 0;i<4;)
{
for(j = i+1;j<4;j++)
{
if(Num[i]==Num[j])
{ Num[i]++;if(Num[i]>9) Num[i] = 0;i=0;break;}
}
if(j == 4)i++;
}
get: for(i = 0;i<4;)
{
printf("input Num %d ",i);
scanf("%s",&getNum[i]);
if(getNum[i]-0x30>9||getNum[i]-0x30<0)
{
if (getNum[i] == 'H'||getNum[i] =='h')
{
help();
}
else
printf("input one num\r\n");
//if(i>0)i--;
}
else
{
getNum[i] -=0x30;
for(j = 0;j<i;j++)
{
if(getNum[i] == getNum[j])
{printf("have a same data,please input another\r\n"); break;}
}
if(j==i)
i++;
}
}
printf("you input num %d%d%d%d Y or N?\r\n",getNum[0],getNum[1],getNum[2],getNum[3]);
do{
scanf("%c",&j);
printf("%c",j);
if((j == 'N') || (j == 'n'))goto get;
}while( !((j == 'Y') || (j == 'y')));
printf("over");
}