深入理解分支和循环语句(3)

文章目录

  • 3.4.6 猜数字游戏
  • 4. goto语句
  • 附:
  • 3.4.6 猜数字游戏

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

    1. 选择玩游戏或退出游戏
    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. 猜数字游戏主体
    //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;
    }
    

    完整代码:

    #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语句

    C语言中提供了可以随意滥用的 goto语句和标记跳转的标号。

    #include <stdio.h>
    
    int main()
    {
    again:
    	printf("hehe\n");
    	printf("hehe\n");
    	printf("hehe\n");
    	printf("hehe\n");
    	
    	goto again;//可以向前跳,也可以向后跳
    
    //end:
    
    	return 0;
    }
    

    注:

    #include <stdio.h>
    
    void test()
    {
    flag:
    	printf("test\n");
    }
    
    int main()
    {
    	printf("hehe\n");
    	
    	goto flag;//goto 语句只能一个函数内部跳转,不能跨函数跳转的
    
    	return 0;
    }
    

    从理论上, goto语句是没有必要的,实践中没有goto语句也可以很容易的写出代码。

    //关机程序 
    //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;
    }
    

    附:关于shutdown命令的扩展


    但是某些场合下goto语句还是用得着的,最常见的用法就是终止程序在某些深度嵌套的结构的处理过程。例如:一次跳出两层或多层循环。多层循环这种情况使用break是达不到目的的,它只能从最内层循环退出到上一层的循环。
    goto语言真正适合的场景

    附:

    分支和循环语句(1)
    分支和循环语句(2)

    物联沃分享整理
    物联沃-IOTWORD物联网 » 深入理解分支和循环语句(3)

    发表评论