猜數字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("很遺憾,差一點,繼續努力! ");
}
}
}