练习伪随机数的生成
利用比较数字游戏来实现随机数的应用
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>
int display(){
//显示菜单
printf("**************************************\n");
printf("1.begin\n");
printf("2.exit\n");
printf("**************************************\n");
int choice=0;
scanf("%d", &choice);
return choice;
}
void begin(){
//比较函数以及随机数的应用
int input,random;
srand((unsigned int)time(0));
random = rand()%100+1;
while (1){
printf("guess number:\n");
scanf("%d", &input);
if (input < random){
printf("低\n");
}
else if (input>random){
printf("高\n");
}
else{
printf("对\n");
Sleep(1000);//利用Sleep函数进行结果延迟
system("cls");
break;
}
}
}
void game(){
//菜单选项选择实现
while (1){
int y = display();
if (y == 1){
begin();
}
else if (y == 2){
system("cls");
break;
}
el