C语言——猜数字小游戏
#include<stdio.h> #include<stdlib.h> #include<time.h> //猜数字游戏(1~100) //1.游戏生成一个随机数,玩家来猜测 //2.如果猜大了,屏幕输出猜大了 //3.如果猜小了,屏幕输出猜小了 //4.如果猜对了,屏幕输出猜对了 //5.游戏可以重复玩 void menu() { printf("************************************** "); printf("**Do you want to play a number game?** "); printf("**************1.YES******************* "); printf("**************2.NO******************** "); printf("************************************** "); } void game() { //使生成的随机数在1~100之间 int rand_num = rand() % 100 + 1; while (1) { //输入猜测的数字 int guess = 0; printf("Please make a guess: "); scanf("%d", &guess); if (guess > rand_num) { printf("Guess big "); } else if (guess < rand_num) { printf("Guess its little "); } else { printf("You guessed right "); } } } int main() { srand((unsigned int)time(NULL)); do { //进入游戏界面 menu(); //做出选择, 1 or 2 int choice = 0; printf("Please make a choice: "); scanf("%d", &choice); switch (choice) { case 1: game(); break; case 2: printf("exit a game "); break; default: printf("input error "); break; } if (choice == 2) break; } while (1); return 0; }
下一篇:
亲测:三个值得练手的Java实战项目