300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > c语言猜数字游戏用while 【C语言基础】有趣的猜数字游戏。(while循环 if语句 switch语句)...

c语言猜数字游戏用while 【C语言基础】有趣的猜数字游戏。(while循环 if语句 switch语句)...

时间:2018-11-07 09:52:25

相关推荐

c语言猜数字游戏用while 【C语言基础】有趣的猜数字游戏。(while循环 if语句 switch语句)...

要求:猜数字游戏的实现。代码如下:#include

#include

#include

void menu()

{

printf("**********************************\n");

printf("*********** 1.play **********\n");

printf("*********** 0.exit **********\n");

printf("**********************************\n");

}

void game()

{

int random_num = rand()%100+1;

int input = 0;

while(1)

{

printf("请输入猜的数字:>");

scanf("%d", &input);

if(input > random_num)

{

printf("猜大了\n");

}

else if(input < random_num)

{

printf("猜小了\n");

}

else

{

printf("猜对了\n");

break;

}

}

}

int main()

{

int input = 0;

srand((unsigned)time(NULL));

do

{

menu();

printf("请选择>:");

scanf("%d", &input);

switch(input)

{

case 1:

game();

break;

case 0:

break;

default:

printf("选择错误,请重新选择!\n");

break;

}

}

while(input);

return 0;

}

运行结果:

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。