如何用C语言编写一个长尾词的猜数游戏程序?
- 内容介绍
- 文章标签
- 相关推荐
本文共计627个文字,预计阅读时间需要3分钟。
原文示例:本文实例为大家分享了C语言实现猜数字游戏的整体代码,供大家参考学习,具体内容如下:
代码如下:#include #include #include
int main() { int number, guess, attempts=0; srand(time(NULL)); number=rand() % 100 + 1;
printf(Welcome to the Guess the Number Game!\n); printf(I'm thinking of a number between 1 and 100.\n);
do { printf(Enter your guess: ); scanf(%d, &guess); attempts++;
if (guess number) { printf(Too high!\n); } else { printf(Congratulations! You guessed the right number in %d attempts.\n, attempts); } } while (guess !=number);
return 0;}
简化版:C语言实现猜数字游戏,示例代码如下:
#include #include #include
int main() { int number, guess, attempts=0; srand(time(NULL)); number=rand() % 100 + 1;
printf(欢迎玩猜数字游戏!我在想1到100之间的一个数。\n); do { printf(请输入你的猜测:); scanf(%d, &guess); attempts++;
if (guess number) { printf(太高了!\n); } else { printf(恭喜!你猜对了,用了%d次尝试。\n, attempts); } } while (guess !=number);
return 0;}
本文实例为大家分享了C语言实现猜数游戏的具体代码,供大家参考,具体内容如下
代码如下
#include<stdio.h> #include<stdlib.h> #include<time.h> void game() { srand((unsigned int)time(NULL)); //srand((time(NULL))设计一个随机种子,每次运行都能保证随机种子不同 int x = rand() % 100; //100以内的随机值;rand()函数可以用来产生随机数,但这不是真正意义上的随机数,是一个伪随机数 int y; while (1) //判断输入的数和猜的数的大小 { scanf_s("%d", &y); if (x > y) { printf("猜小了\n"); } else if (x < y) { printf("猜大了\n"); } else { printf("恭喜你,猜对了\n"); break; } } } void menu() //目录 { printf("********************************\n"); printf("**********1.继续玩**************\n"); printf("**********0.退出 **************\n"); printf("********************************\n"); } int main() { age2: menu(); age1: printf("请输选择\n"); int a; scanf_s("%d", &a); switch (a) { case 1: printf("游戏开始请输入你猜的数\n"); game(); goto age2; break; case 0: break; default: printf("选择错误请重新输入!\n"); goto age1; } }
运行结果
注:
1.rand()函数可以用来产生随机数,但这不是真正意义上的随机数,是一个伪随机数
2.srand((time(NULL))设计一个随机种子,每次运行都能保证随机种子不同
3.但使用srand((time(NULL))会出现如下警告
将srand((time(NULL))改为srand((unsigned int)time(NULL));即可
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。
本文共计627个文字,预计阅读时间需要3分钟。
原文示例:本文实例为大家分享了C语言实现猜数字游戏的整体代码,供大家参考学习,具体内容如下:
代码如下:#include #include #include
int main() { int number, guess, attempts=0; srand(time(NULL)); number=rand() % 100 + 1;
printf(Welcome to the Guess the Number Game!\n); printf(I'm thinking of a number between 1 and 100.\n);
do { printf(Enter your guess: ); scanf(%d, &guess); attempts++;
if (guess number) { printf(Too high!\n); } else { printf(Congratulations! You guessed the right number in %d attempts.\n, attempts); } } while (guess !=number);
return 0;}
简化版:C语言实现猜数字游戏,示例代码如下:
#include #include #include
int main() { int number, guess, attempts=0; srand(time(NULL)); number=rand() % 100 + 1;
printf(欢迎玩猜数字游戏!我在想1到100之间的一个数。\n); do { printf(请输入你的猜测:); scanf(%d, &guess); attempts++;
if (guess number) { printf(太高了!\n); } else { printf(恭喜!你猜对了,用了%d次尝试。\n, attempts); } } while (guess !=number);
return 0;}
本文实例为大家分享了C语言实现猜数游戏的具体代码,供大家参考,具体内容如下
代码如下
#include<stdio.h> #include<stdlib.h> #include<time.h> void game() { srand((unsigned int)time(NULL)); //srand((time(NULL))设计一个随机种子,每次运行都能保证随机种子不同 int x = rand() % 100; //100以内的随机值;rand()函数可以用来产生随机数,但这不是真正意义上的随机数,是一个伪随机数 int y; while (1) //判断输入的数和猜的数的大小 { scanf_s("%d", &y); if (x > y) { printf("猜小了\n"); } else if (x < y) { printf("猜大了\n"); } else { printf("恭喜你,猜对了\n"); break; } } } void menu() //目录 { printf("********************************\n"); printf("**********1.继续玩**************\n"); printf("**********0.退出 **************\n"); printf("********************************\n"); } int main() { age2: menu(); age1: printf("请输选择\n"); int a; scanf_s("%d", &a); switch (a) { case 1: printf("游戏开始请输入你猜的数\n"); game(); goto age2; break; case 0: break; default: printf("选择错误请重新输入!\n"); goto age1; } }
运行结果
注:
1.rand()函数可以用来产生随机数,但这不是真正意义上的随机数,是一个伪随机数
2.srand((time(NULL))设计一个随机种子,每次运行都能保证随机种子不同
3.但使用srand((time(NULL))会出现如下警告
将srand((time(NULL))改为srand((unsigned int)time(NULL));即可
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持自由互联。

