一、goto语句
C语言提供了⼀种非常特别的语法,就是
goto
语句和跳转标号,
goto
语句可以实现在同⼀个函数
内跳转到设置好的标号处。
#include <stdio.h>
int main(){
printf("你好\n");
goto next;
printf("再见\n");
next:
//跳过了再见的打印
printf("又见面了\n");
return 0;
}
打印结果:
你好
又见面了
goto 语句如果使用的不当,就会导致在函数内部随意乱跳转,打乱程序的执行流程,所以我们的建议是能不用尽量不去使用;但是 goto 语句也不是一无是处,在多层循环的代码中,如果想快速跳出使用goto就非常方便了
。
for(...)
{
for(...)
{
for(...)
{
if(disaster)
goto error;
}
}
}
error:
//...
本来
for 循环想提前退出得使用break ,⼀个
break
只能跳出⼀层
for
循环,如果3层循环嵌套
就得使用3个 break 才能跳出循环,所以在这种情况下我们使用goto 语句就会更加的快捷
二、数字游戏
要求:
- 自动生成1~100的随机数
- 玩家猜数字,猜数字的过程中,根据猜测数据的大小给出猜大了或小了的反馈,直到猜对,游戏结束
1.随机数的生成
1.1 rand函数
C语言提供了一个函数叫rand,可以产生随机数,函数原型为:
int rand(void);
rand函数会返回⼀个伪随机数,这个随机数的范围是在0~
RAND_MAX
之间,这个RAND_MAX的大小是依赖编译器上实现的,但是⼤部分编译器上是32767。
rand函数的使⽤需要包含⼀个头⽂件是:stdlib.h
我们可以调用rand函数,产生随机数
#include <stdio.h>
#include <stdlib.h>
int main(){
printf("%d\n",rand());
printf("%d\n",rand());
printf("%d\n",rand());
printf("%d\n",rand());
printf("%d\n",rand());
return 0;
}
可以观察到两次运行结果一样,说明有点问题。
深入了解后,我们可以知道rand函数生成的随机数是伪随机的,伪随机数不是真正的随机数,是通过某种算法产生的随机数。而rand函数是对⼀个叫“种子”的基准值进行运算生成的随机数。
之所以每次运行产生的随机数是一样的,是因为rand函数生成的随机数的默认种子是1。所以想要产生随机数,我们就得让种子变化
1.2 srand函数
C语言提供了一个函数叫srand,用来初始化随机数的生成器,srand的原型如下:
void srand(unsigned int seed);
程序中在调⽤ rand 函数之前先调用 srand 函数,通过 srand 函数的参数seed来设置rand函数生成随机数的时候的种子,只要种子在变化,每次生成的随机数序列也就变化起来了。
那也就是说给srand的种子是如果是随机的,rand就能生成随机数;在生成随机数的时候又需要⼀个随机数,这就矛盾了。
1.3 time函数
在程序中我们⼀般是使用程序运行的时间作为种子的,因为时间时刻在发生变化的。
在C语言中有⼀个函数叫
time
,就可以获得这个时间,time函数原型如下:
time_t time (time_t*timer);
time函数会返回当前的日历时间,其实返回的是1970年1月1日0时0分0秒到选择程序运行之间的差值,单位是秒。返回类型是time_t类型的,time_t 类型本质上其实就是32位或者64位的整型类 型。
time函数的参数 timer 如果是⾮NULL的指针的话,函数也会将这个返回的差值放在timer指向的内存中带回去。
如果 timer 是NULL,就只返回这个时间的差值。time函数返回的这个时间差也被叫做:
时间戳
。
time函数的时候需要包含头文件:
time.h
如果只是让time函数返回时间戳,我们可以这样写:
time(NULL);//调⽤time函数返回时间戳,这⾥没有接收返回值
那我们就可以让⽣成随机数的代码改写成如下:
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main()
{
//使⽤time函数的返回值设置种⼦
//因为srand的参数是unsigned int类型,我们将time函数的返回值强制类型转换
srand((unsigned int)time(NULL));
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
printf("%d\n", rand());
return 0;
}
多运行几次,观察结果:
可以看到生成了不同的随机数
srand函数是不需要频繁调用的,⼀次运⾏的程序中调用⼀次就够了。
1.4 设置随机数的范围
如果我们要生成0~99之间的随机数,方法如下:
rand()%100;//余数的范围是0~99
如果我们要生成0~100之间的随机数,方法如下:
rand()%100+1;
所以如果要生成a~b之间的随机数,方法如下:
a+rand()%(b-a+1);
2.游戏实现
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
int r = rand()%100+1;
int guess= 0;
while(1)
{
printf("请猜数字>:");
scanf("%d", &guess);
if(guess < r)
{
printf("猜⼩了\n");
}
else if(guess > r)
{
printf("猜⼤了\n");
}
else
{
printf("恭喜你,猜对了\n");
break;
}
}
}
void menu()
{
printf("***********************\n");
printf("****** 1. play ******\n");
printf("****** 0. exit ******\n");
printf("***********************\n");
}
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;
}
也可以加上猜数字的次数限制,如果5次猜不出来,就算失败
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void game()
{
int r = rand() % 100 + 1;
int guess = 0;
int count = 5;
while (count)
{
printf("\n你还有%d次机会\n", count);
printf("请猜数字>:");
scanf("%d", &guess);
if (guess < r)
{
printf("猜⼩了\n");
}
else if (guess > r)
{
printf("猜⼤了\n");
}
else
{
printf("恭喜你,猜对了\n");
break;
}
count--;
}
if (count == 0)
{
printf("你失败了,正确值是:%d\n", r);
}
}
void menu()
{
printf("***********************\n");
printf("****** 1. play ******\n");
printf("****** 0. exit ******\n");
printf("***********************\n");
}
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;
}