C语言猜数字游戏及关机程序

目录

一、猜数字游戏

二、关机程序

三、cmd弹窗命令


一、猜数字游戏

首先构思基本框架

int main()
{
	int input = 0;
	do
	{
		menu();//创建菜单
		printf("请选择>:");//选择游戏或退出
		scanf("%d", &input);
		switch (input)
		{
		case 1:
			game();//游戏程序
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}

	} while (input);//用case 0:在此处恰好结束循环

然后对菜单和游戏主体进行定义

void menu()
{
	printf("*******************************************\n");
	printf("**********    1.Play  0.Exit    ***********\n");
	printf("*******************************************\n");
}
void game()
{
	int a = 0;
	int guess = 0;
	a = rand() % 100 + 1;//生成1~100内的随机数
	while (1)
	{
		printf("请猜数字>:");
		scanf("%d", &guess);
		if (guess > a)
			printf("猜大了\n");
		else if (guess < a)
			printf("猜小了\n");
		else
		{
			printf("恭喜你,猜对了!\n");
			Sleep(2000);//展示
			system("cls");//清屏
			break;
		}
	}
}

这里的rand()函数生成随机数时要有随机起点,否则每次游戏的随机数都一样,在此加上

srand((unsigned int)time(NULL));//利用时间戳设置随机数生成起点

这里用到了time(),获取时间戳,括号里面应为指针,这里用空指针NULL就行,目的是让随机数生成起点随时间不同而改变。注意:这条代码要写在main函数里,如果在外面,时间间隔只有几ms,效果不明显。

完整源码如下:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<windows.h>

void menu()
{
	printf("*******************************************\n");
	printf("**********    1.Play  0.Exit    ***********\n");
	printf("*******************************************\n");
}
void game()
{
	int a = 0;
	int guess = 0;
	a = rand() % 100 + 1;//生成1~100内的随机数
	while (1)
	{
		printf("请猜数字>:");
		scanf("%d", &guess);
		if (guess > a)
			printf("猜大了\n");
		else if (guess < a)
			printf("猜小了\n");
		else
		{
			printf("恭喜你,猜对了!\n");
			Sleep(2000);//展示
			system("cls");//清屏
			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;
}

二、关机程序

这个主要用到system命令

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

int main()
{
	char input[20] = { 0 };
	system("shutdown -s -t 60");//设置60秒后关机,cmd命令行命令
again:
	printf("请注意,您的电脑将在60秒后关机,如果输入:我是一个大狗熊 就取消关机\n请输入>:");
	scanf("%s", input);
	if (strcmp(input, "我是一个大狗熊") == 0)
		system("shutdown -a");
	else
		goto again;
}

这里的goto语句也可由while循环代替,你也可以设置别的话,还可以设置为启动项,让你的朋友🤭

三、cmd弹窗命令

#include<stdio.h>
#include<string.h>
#define N 100//自行修改,1000个要个5分钟左右才结束
int main()
{
	int i;
	printf("如果你不想死,那么就输入 start 开始游戏:\n");
	char key[10];
	scanf("%s", key);
	if (strcmp(key, "start") == 0)
	{
		for (i = 0; i < N; i++)//会在屏幕上打开N个cmd命令行窗口
			system("start");
	}
	else
	{
		system("start");
		system("shutdown -s -t 10");
	}
}

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

...404 Not Found

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值