猜數字游戲編程
#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語言題目 編程實現數字猜謎游戲!
一個函數用於生成一個隨機數
再用一個函數進行三次循環
如果循環中輸入的數字猜對了就返回1如果三次循環結束則返回0
主數字判斷返回的結果如果是1則表示猜對否則表示猜錯
然後詢問是否繼續進行猜數字游戲
下面是一個簡單的代碼
#include<stdio.h>
#include<time.h>
voidclean_input_buf(void)
{
while(getchar()!=' ');
}
intmake_num(intnum)
{
srand(num);
return1+rand()%20;
}
intguess_num(void)
{
inti=3;
intn;
intnum;
num=make_num(clock());
while(i)
{
printf("請輸入一個你猜的數字:");
scanf("%d",&n);
if(n==num)
return1;
clean_input_buf();
--i;
}
return0;
}
intmain(intargc,char**argv)
{
charc;
printf("猜數字游戲! 游戲要猜測的數字為1-20,每個數字可以猜測3次! ");
while(1)
{
if(guess_num()==1)
printf("恭喜你,你猜中了! ");
else
printf("很遺憾,你猜錯了! ");
redo:
printf("還要繼續游戲嗎?繼續輸入Y否則輸入N! Y/N:");
c=getchar();
clean_input_buf();
if(c=='Y'||c=='y')
continue;
elseif(c=='N'||c=='n')
break;
else
{
printf("輸入錯誤,請重新輸入! ");
gotoredo;
}
}
return0;
}
『叄』 猜數字游戲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("很遺憾,差一點,繼續努力! ");
}
}
}

『肆』 求解使用matlab語言編寫猜數字游戲的程序
num=randi(100,[1 1]);
i=0;
while (i<3)
elseif(a>num)
disp 'High'
i=i+1;
else
end
if (i==3)
disp 'You lose'
end
#include <stdio.h>
int main()
printf("enter the integer you guess:");
scanf("%d", &t);
if (data == t)
{
printf("the data is:%d ", data);
break;
}
else if(data > t)
else
count++;
}while(1);
if (count <= 3)
else if(count <= 7)
return 0;

編程環境
MATLAB由一系列工具組成。這些工具方便用戶使用MATLAB的函數和文件,其中許多工具採用的是圖形用戶界面。包括MATLAB桌面和命令窗口、歷史命令窗口、編輯器和調試器、路徑搜索和用於用戶瀏覽幫助、工作空間、文件的瀏覽器。
隨著MATLAB的商業化以及軟體本身的不斷升級,MATLAB的用戶界面也越來越精緻,更加接近Windows的標准界面,人機交互性更強,操作更簡單。而且新版本的MATLAB提供了完整的聯機查詢、幫助系統,極大的方便了用戶的使用。
『伍』 C++猜數字游戲編程
//從網上找的,順便改了改.....關於選項設定不甚明了....
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
cout<<"輸入設定的數\n";
cin>>r;
int g;
int n=20;
while(n>0)
{
cout<<"請輸入你猜想的數據:";
cin>>g;
n--;
if(g<r)
cout<<"猜測的數值小了,還有"<<n<<"次機會"<<endl;
else if(g>r)
cout<<"猜測的數值大了,還有"<<n<<"次機會"<<endl;
else
{
cout<<"恭喜你答對了,用了"<<20-n<<"次就猜對了"<<endl;
n=0;
break;
}
if(n==0)
cout<<"20次機會用完了,沒有猜對,正確答案是:"<<r<<endl;
}
return 0;
}
『陸』 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;
}
==============================================
運行結果見附圖,希望我的回答能夠對你有所幫助。

