Branch and Loop Statements (3)

3.4.6 Guess the number game

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

  1. Choose to play the game or quit the game
int main()
{
    
    
	//打印菜单
	//1. 玩游戏
	//0. 退出游戏
	int input = 0;

	do
	{
    
    
		menu();

		printf("请选择:>");
		scanf("%d", &input);//1 0

		switch (input)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);

	return 0;
}

  1. guess the number game
//void srand (unsigned int seed);
void game()
{
    
    
	//RAND_MAX;
	
	//1. 生成随机数
	int ret = rand()%100+1;//可以生成随机数,随机数的范围是:0~32767
	//1~100
	//printf("%d\n", ret);

	//2. 猜数字
	int guess = 0;
	
	while (1)
	{
    
    
		printf("请猜数字:>");
		scanf("%d", &guess);

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

int main()
{
    
    
	//time函数可以返回一个时间戳
	srand((unsigned int)time(NULL));//要给srand传递一个变化的值,计算机上的时间是时刻发生变化的
	
	return 0;
}

Full code:

#include <stdio.h>
#include <stdlib.h>//rand和srand需要一个stdlib.h的头文件
#include <time.h>

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

//void srand (unsigned int seed);
void game()
{
    
    
	//RAND_MAX;
	
	//1. 生成随机数
	int ret = rand()%100+1;//可以生成随机数,随机数的范围是:0~32767
	//1~100
	//printf("%d\n", ret);

	//2. 猜数字
	int guess = 0;
	
	while (1)
	{
    
    
		printf("请猜数字:>");
		scanf("%d", &guess);

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

int main()
{
    
    
	//打印菜单
	//1. 玩游戏
	//0. 退出游戏
	int input = 0;
	//time函数可以返回一个时间戳
	srand((unsigned int)time(NULL));//要给srand传递一个变化的值,计算机上的时间是时刻发生变化的

	do
	{
    
    
		menu();

		printf("请选择:>");
		scanf("%d", &input);//1 0

		switch (input)
		{
    
    
		case 1:
			game();
			break;
		case 0:
			printf("退出游戏\n");
			break;
		default:
			printf("选择错误\n");
			break;
		}
	} while (input);

	return 0;
}

4. goto statement

The C language provides goto statements that can be abused at will and labels that mark jumps.

#include <stdio.h>

int main()
{
    
    
again:
	printf("hehe\n");
	printf("hehe\n");
	printf("hehe\n");
	printf("hehe\n");
	
	goto again;//可以向前跳,也可以向后跳

//end:

	return 0;
}

Note:

#include <stdio.h>

void test()
{
    
    
flag:
	printf("test\n");
}

int main()
{
    
    
	printf("hehe\n");
	
	goto flag;//goto 语句只能一个函数内部跳转,不能跨函数跳转的

	return 0;
}

Theoretically, the goto statement is unnecessary, and in practice, the code can be easily written without the goto statement.

//关机程序 
//1. 程序运行起来后,1分钟内电脑关机
//2. 如果输入:我是猪,就取消关机
#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main()
{
    
    
	char input[20] = {
    
     0 };
	//程序倒计时关机
	system("shutdown -s -t 60");

again:
	printf("请注意,你的电脑在1分钟内关机,如果输入:我是猪,就取消关机\n");
	scanf("%s", input);//输入
	
	if (0 == strcmp(input, "我是猪"))
	{
    
    
		system("shutdown -a");
	}
	else
	{
    
    
		goto again;
	}

	return 0;
}

//不用goto语句

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

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

		if (0 == strcmp(input, "我是猪"))
		{
    
    
			system("shutdown -a");
			break;
		}
	}

	return 0;
}

Attachment: About the extension of the shutdown command


However, the goto statement is still useful in some occasions. The most common usage is to terminate the processing of the program in some deeply nested structures. For example: Jump out of two or more layers of loops at a time. Using break in this case does not achieve the purpose of multi-layer loops, it can only exit from the innermost loop to the previous loop.
The scene where the goto language is really suitable

Attached:

Branch and Loop Statements (1)
Branch and Loop Statements (2)

Guess you like

Origin blog.csdn.net/weixin_73077334/article/details/130448760