//一个猜数字的游戏 | ||
43 | #include<stdio.h> | |
44 | #include<time.h> | |
45 | #include<stdlib.h> | |
46 | ||
47 | void game() | |
48 | { | |
49 | int ret = rand() % 100 + 1; | |
50 | int guess = 0; | |
51 | int count = 10; | |
52 | while (count) | |
53 | { | |
54 | printf("\n你还有%d次机会\n", count); | |
55 | printf("请猜数字>:"); | |
56 | scanf("%d", &guess); | |
57 | if(guess < ret) | |
58 | { | |
59 | printf("猜小了\n"); | |
60 | } | |
61 | else if (guess > ret) | |
62 | { | |
63 | printf("猜大了\n"); | |
64 | } | |
65 | else | |
66 | { | |
67 | printf("恭喜你,猜对了!"); | |
68 | break; | |
69 | } | |
70 | count--; | |
71 | } | |
72 | if (count == 0) | |
73 | { | |
74 | printf("你失败了,正确的值是:%d\n", ret); | |
75 | } | |
76 | ||
77 | } | |
78 | void menu() | |
79 | { | |
80 | printf("****************\n"); | |
81 | printf("**** 1.play ****\n"); | |
82 | printf("**** 0.exit ****\n"); | |
83 | printf("****************\n"); | |
84 | } | |
85 | ||
86 | int main() | |
87 | { | |
88 | int input = 0; | |
89 | srand((unsigned int)time(NULL)); | |
90 | do | |
91 | { | |
92 | menu(); | |
93 | printf("请选择:>"); | |
94 | scanf("%d", &input); | |
95 | switch (input) | |
96 | { | |
97 | case 1: | |
98 | game(); | |
99 | break; | |
100 | case 0: | |
101 | printf("游戏结束\n"); | |
102 | break; | |
103 | default : | |
104 | printf("选择错误,请重新选择\n"); | |
105 | break; | |
106 | } | |
107 | } while (input); | |
108 | return 0; |
猜数字游戏的代码
最新推荐文章于 2024-10-11 15:54:45 发布