程序代码
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <string.h>
#include <windows.h>
#include <stdlib.h>
#include <time.h>
void menu ( )
{ printf ( "***********************\n" ) ; printf ( "** **\n" ) ; printf ( "** 1.play game **\n" ) ; printf ( "** 0.exit **\n" ) ; printf ( "** **\n" ) ; printf ( "***********************\n" ) ;
}
void playGame ( )
{ int num = 0 ; int r = rand ( ) % 10 + 1 ; while ( 1 ) { printf ( "请输入1-10之间的数字:\n" ) ; scanf ( "%d" , & num) ; if ( num > r) { printf ( "您输入的数字偏大\n" ) ; } else if ( num < r) { printf ( "您输入的数字偏小\n" ) ; } else if ( num == r) { printf ( "恭喜你猜对了\n" ) ; printf ( "即将返回初始化界面,请稍后\n" ) ; Sleep ( 2000 ) ; break ; } }
}
int main ( )
{ srand ( ( unsigned ) time ( NULL ) ) ; int num = 0 ; int input = 0 ; while ( 1 ) { menu ( ) ; printf ( "请输入一个数字决定是否开始游戏:" ) ; scanf ( "%d" , & input) ; if ( input == 0 ) { printf ( "游戏结束,欢迎下次再来" ) ; break ; } else { printf ( "游戏即将开始,请稍后\n" ) ; Sleep ( 2000 ) ; playGame ( ) ; } } return 0 ;
}
运行结果