猜数字小游戏and关机程序

猜数字小游戏

通过算法进行的随机其实是伪随机

game()

menu菜单

input

随机数的生成方法

rand()

时间戳获取的是运行时电脑上的时间

%100的数字是0-100

随机数的生成不能放到循环里,因为每次猜的数不知道大小永远是随机的

//猜数字游戏
//1. 电脑随机生成一个数字(1~100)
//2. 玩家猜数字
//   玩家猜小了,就告知猜小了
//   玩家猜大了,就告知猜大了
//   直到猜对为止
//3. 游戏可以一直玩

#include<stdlib.h>
#include<time.h>

//RAND_MAX
//rand 函数返回随机值数范围(0-32767)



void menu()
{
	printf("*******************\n");
	printf("****** 1.play *****\n");
	printf("****** 0.exit *****\n");
	printf("*******************\n");
}

void game()
{
	int guess = 0;
	//生成一个随机数
	int ret = rand() % 100 + 1;
		//printf("%d\n", ret);

		//猜数字
		while (1)
		{
			printf("猜数字:\n");
			scanf("%d", &guess);
			if (guess < ret)
			{
				printf("猜小了\n");

			}
			else if (guess > ret)
			{
				printf("猜大了\n");
			}
			else
			{
				printf("恭喜你,猜对了\n");
				break;
			}
		}
}


int main()
{
	int input = 0;
	srand((unsigned int)time(NULL));

	do
	{
		menu();
		printf("请选择:");
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();
			break;
		case 0:
				printf("退出游戏\n");
				break;
			default:
				printf("选择错误,重新选择!\n");
				break;
		}
	} while (input);
	return 0;
}

//int main()
//{
// //	time_t t = time(NULL);
//	 time_t t;
//	time(&t);
//	return 0;
//}

关机程序!

//只要程序运行起来,一分钟内电脑就会关机
//如果输入:我是猪,就取消关机

#include<stdlib.h>
#include<string.h>
#include<stdio.h>

int main()
{
    char input[20] = {0};
    system("shutdow -s -t 60");
    //system是一个库函数,用来执行系统命令
    printf("请注意,你的电脑在一分钟内关机,如果输入:我是猪,就取消关机\n");
  scanf("%s",input);
  //判断
  if(strcmp(input,"我是猪")==0)
  {
      system("shutdown -a");//取消关机
  }
  else
  {
      goto again;
  }
  return 0;
}


//while循环
#include<stdlib.h>
#include<string.h>
#include<stdio.h>

int main()
{
    char input[20] = {0};
    system("shutdow -s -t 60");
    
while(1)
{
     printf("请注意,你的电脑在一分钟内关机,如果输入:我是猪,就取消关机\n");
  scanf("%s",input);
  if(strcmp(input,"我是猪")==0)
  {
      system("shutdown -a");//取消关机
      break;
  }
}
return 0;
}

おすすめ

転載: blog.csdn.net/Ll_R_lL/article/details/122524242