C的策划者 [英] Mastermind in C

查看:130
本文介绍了C的策划者的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请告诉我如何随机化C语言中的字符或单词.
示例:
我将输入R(红色)Y(黄色)G(绿色)B(蓝色).
计算机将随机化它,也可以将其复制.
例如它可以是R R B G或G B B Y ..

请告诉我如何..我需要它来制作一个名为MASTERMIND的游戏.

帮助将不胜感激.

please tell me how to randomize characters or words in C.
Example:
I will input R(red) Y(yellow) G(green) B(blue).
The computer will randomize it and it can also be duplicated.
for instance, it could be R R B G or G B B Y..

please tell me how.. I need it to make a game called MASTERMIND.

Help would be much appreciated. Thanks.

推荐答案

您可以将字符存储在数组中,例如
You may store the charachters in an array, e.g.
const char col[]={''R'',''Y'',''G'',''B''};



然后使用rand函数选择字符的索引:



then use the rand function to select the index of the character:

int r = rand() % sizeof(col);


例如:


For instance:

int i;
for (i=0; i<4; i++)
{
  int r = rand() % sizeof(col);
  printf("%c ", col[r]);
}
printf("\n");



:)



:)


在这里尝试:
随机数c [
Try here:
random numbers c[^]


这篇关于C的策划者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆