文章目录
- 前言
- 一、猜数字游戏
- 1.1.电脑随机生成一个数(1~100);
- 1.2.猜数字:
- 1.3.玩完一把不过瘾,可以继续玩,不用退出程序。
- 1.4.rand 和 srand 之间的联系
- 5.猜数字游戏源码
- 二、go to 语句
- 2.1.例如:一次跳出两层或多层循环。
- 2.2. goto 语句是不能跨函数的
- 三.关机程序
- 3.1.程序运行起来,1分钟内关机电脑
- 3.2.如果,输入我是猪,就取消关机。
- 3.3字符串比较大小 strcmp 头文件 <string.h>
- 四.例题——打印素数、打印闰年
- 4.1.char 是字符类型
- 4.2.指针变量是用来存放地址的
- 4.3.switch语句知识点
- 4.4.两整形变量的交换
- 4.5.打印素数 --- count++ 用法 -- sqrt 函数
- 4.6.打印闰年
前言
:C语言:结构化的程序设计语言 顺序结构、选择结构、循环结构:
提示:以下是本篇文章正文内容,下面案例可供参考
一、猜数字游戏
1.1.电脑随机生成一个数(1~100);
1.2.猜数字:
a> 猜到了,提醒猜到了,可以继续猜。
b> 猜小了, 提醒猜小了,继续猜。
c>猜对了 , 恭喜,猜对了,结束游戏
1.3.玩完一把不过瘾,可以继续玩,不用退出程序。
如何产生随机数 --- rand 函数 rand 包含的头文件是

1.4.rand 和 srand 之间的联系
- rand 使用之前先调用 srand
- srand 只要调用一次就可以啦,不要每次生成随机数都设置srand
- rand 是生成随机数,而srand是在生成随机数之前先设置随机数的生成起点
5.猜数字游戏源码
#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>void menu()
{// 菜单printf("*********************************\n");printf("********1. play 0. exit*********\n");printf("*********************************\n");}void game()
{RAND_MAX;// 1.生成随机数//rand 函数 可以生成随机数int ret = rand()%100+1; //随机数范围是 0 ~ 32767 // rand()%100 得到的余数只可能是小于100 --- 0~99//rand()%100+1 随机数范围是 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()
{int input = 0;srand((unsigned int)time(NULL)); //要给srand 传递一个变化的值,计算机上的时间是时刻发生变化的//time 函数可以返回一个时间戳do{menu();printf("请选择:>");scanf_s("%d", &input); // 1 0 switch (input){case 1:game();break;case 0:printf("退出游戏\n");break;default:printf("选择错误\n");break;}} while (input);//打印菜单//1.玩游戏//0.退出游戏return 0;
}
二、go to 语句
- C语言中提供了可以随意滥用的 goto语句
- 从理论上goto语句是没有必要的,实践中没有goto语句也可以很容易的写出代码。
- 但是某些场合下goto语句还是用得着的,最常见的用法就是终止程序在某些深度嵌套的结构的处理过程
2.1.例如:一次跳出两层或多层循环。
多层循环这种情况使用break 是达不到目地的,他只能从最内层循环退出到上一层的循环。

2.2. goto 语句是不能跨函数的

三.关机程序
3.1.程序运行起来,1分钟内关机电脑
3.2.如果,输入我是猪,就取消关机。

3.3字符串比较大小 strcmp 头文件 <string.h>

- goto 写法


四.例题——打印素数、打印闰年
4.1.char 是字符类型
字符的本质是字符的ASCII码值,ASCII码值是整型,所以字符类型在归类的时候是可以划分到整型家族的
4.2.指针变量是用来存放地址的
地址大小 32bit / 64bti
变量的大小 32 bit - 4个字节 / 64bit - 8个字节
4.3.switch语句知识点
switch 语句中的default子句可以放在任意位置(√)
switch 语句中的case后的表达式只能是整形常量表达式(√)
switch 语句中case子句必须在default 子句之前(×)
switch 语句中case表达式不要求顺序
4.4.两整形变量的交换


4.5.打印素数 — count++ 用法 – sqrt 函数

4.6.打印闰年
- 判断规则:
1.能被四整除,并且不能被一百整除。
2.能被四百整除是闰年。
