本文介绍了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.
推荐答案
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的策划者的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!