雷火电竞-中国电竞赛事及体育赛事平台

歡迎來到入門教程網(wǎng)!

C語言

當前位置:主頁 > 軟件編程 > C語言 >

C語言實現(xiàn)猜數(shù)字小游戲

來源:本站原創(chuàng)|時間:2020-01-10|欄目:C語言|點擊:

本文實例為大家分享了C語言猜數(shù)字的具體代碼,供大家參考,具體內(nèi)容如下

一、描述

猜數(shù)字游戲。

二、 程序

使用srand((unsigned)time(NULL)),產(chǎn)生隨機數(shù)種子。
int random = rand() % 100 + 1,產(chǎn)生0~100之間的隨機數(shù)。
應(yīng)加上頭文件#include<time.h>

#include<stdio.h>
#include<windows.h>
#include<time.h>
void menu(){
 printf("#######################\n");
 printf("#    1 Play    #\n");
 printf("#    0 Exit    #\n");
 printf("#######################\n");
}
void Play(){
 int m = 0;
 int random = rand() % 100 + 1;
 while (1)
 {
 printf("請輸入一個數(shù)字:\n");
 scanf_s("%d", &m);
 if (m == random){
  printf("你猜對了\n");
  break;
 }
 else if (m > random){
  printf("你猜大了,請重新輸入!");
 }
 else{
  printf("你猜小了,請重新輸入!");
 }
 }
}
 int main()
 {
 srand((unsigned)time(NULL));
 int select = 0;
 do {
 menu();
 printf("請選擇:");
 scanf_s("%d", &select);
 switch (select)
 {
 case 1:
  Play();
  break;
 case 0:
  printf("ByeBye!\n");
  break;
 default:
  printf("輸入錯誤,請重新輸入!\n");
  break;
 }
 } while (select);
 system("pause");
 return 0;
}

三、運行結(jié)果

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持我們。

上一篇:C++實現(xiàn)推箱子游戲

欄    目:C語言

下一篇:C++貪心算法實現(xiàn)活動安排問題(實例代碼)

本文標題:C語言實現(xiàn)猜數(shù)字小游戲

本文地址:http://www.jygsgssxh.com/a1/Cyuyan/141.html

網(wǎng)頁制作CMS教程網(wǎng)絡(luò)編程軟件編程腳本語言數(shù)據(jù)庫服務(wù)器

如果侵犯了您的權(quán)利,請與我們聯(lián)系,我們將在24小時內(nèi)進行處理、任何非本站因素導(dǎo)致的法律后果,本站均不負任何責任。

聯(lián)系QQ:835971066 | 郵箱:835971066#qq.com(#換成@)

Copyright © 2002-2020 腳本教程網(wǎng) 版權(quán)所有