码迷,mamicode.com
首页 > 其他好文 > 详细

完成猜数字游戏

时间:2019-01-23 01:31:36      阅读:216      评论:0      收藏:0      [点我收藏+]

标签:猜数字   开始游戏   div   style   nbsp   ===   ioc   choice   指定   

思路:设置一个随机值,提示用户输入指定范围之内的数字,然后和随机值进行比较,给予用户提示,在最终猜出结果。

其重点在于设置一个随机值,应用rand。

 

 1 #include<stdio.h>
 2 #include<stdlib.h>
 3 
 4 void Game()
 5 {
 6     int num = rand() % 100 - 1;
 7     while (1)
 8     {
 9         int i = 0;
10         printf("请输入[1—100]之间的数字:");
11         scanf("%d", &i);
12         
13         if (i < num)
14         {
15             printf("太低了!\n");
16         }
17         else if (i > num)
18         {
19             printf("太高了!\n");
20         }
21         else
22         {
23             printf("猜对了!\n");
24             break;
25         }
26     }
27 }
28 
29 int meun()
30 {
31     printf("====================!\n");
32     printf("开始游戏请按1\n");
33     printf("====================!\n");
34     printf("退出游戏请按0\n");
35     printf("请输入你的选项:");
36     int choice = 0;
37     scanf("%d",&choice);
38     return choice;
39 }
40 
41 int main()
42 {
43     while (1)
44     {
45         int chioce = meun();
46         if (chioce == 1)
47         {
48             Game();
49         }
50         else
51         {
52             printf("Goodbye!\n");
53             break;
54         }
55     }
56     return 0;
57 }

 

完成猜数字游戏

标签:猜数字   开始游戏   div   style   nbsp   ===   ioc   choice   指定   

原文地址:https://www.cnblogs.com/cuckoo-/p/10306694.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!