Empire C:遊戲篇(1)

來源:互聯網
上載者:User

標籤:

 

隨機產生1-6的數字,我們來猜是幾

猜小了就提示數字小了,請再猜

猜大了就提示數字大了,請再猜

猜對了就提示恭喜,並提示是否繼續再玩

 1 ///riddle 2 ///Author:JA 3 //2015-1-23 4  5  6  7 #include<stdio.h> 8 #include<stdlib.h> 9 #include<time.h>10 #include<conio.h>11 12 int main()13 {14     int a,n;15     time_t t;16     char ans;  //用於存放Y/y17     puts("猜數字遊戲,請猜1-6中的數字!");18 19     do{20         srand(time(&t));  //每次產生的隨機數都不同21         a = rand()%5+1;  //1-6之間的隨機數22         puts("隨機數已經產生,請猜:");23         do{24             scanf("%d", &n);25             if (n > a) puts("數字太大,少年!");26             else if (n < a) puts("數字太小,孩子!");27             else puts("運氣不錯,點個贊!"); break;28 29         } while (n != a);30 31         puts("繼續遊戲嗎?(Y/N)");32         ans = getch();33         if (toupper(ans) != ‘Y‘)34         {35             puts("遊戲結束");36             break;37         }38         /*printf("%d\n", a);39         puts("繼續隨機一個數嗎?(Y/y) 否則按任意鍵繼續");40         ans = _getch(); */ 41     } while (toupper(ans) == ‘Y‘);/*while (ans == ‘Y‘ || ans == ‘y‘);*/42     getchar();43     return 0;44 }
View Code

 

1.隨機數

  • 添加stdlib.h
  • rand()函數——會一直隨機同一個值

2.隨機不同值

  • srand
  • 添加time.h

3.toupper()把小寫轉換成大寫

 

Empire C:遊戲篇(1)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.