如何打印随机cout声明?

时间:2018-02-20 01:57:55

标签: c++

基本上,我有五个不同的npm install输出,我想随机打印其中一个。

cout

1 个答案:

答案 0 :(得分:1)

您基本上需要对数组执行相同的操作,只需生成0-4之间的数字,然后使用switch语句选择要执行的数字。

#include <iostream>
#include <string>
#include <cstdlib> 
using namespace std;

int main(){

        string name[2] = {"Nao","Shilla"};
        string behaviour[4] = {"recognise","detect","avoid","replace"};
        string position[4] = {"above","below","left","right"};
        string pronoun[3] = {"I","you","they"};
        string object[4] = {"car","person","dog","diamond"};
        string texture[3] ={"smooth","dimpled","rough"};

        srand(time(NULL));

        switch(rand() % 5)
        {
            case 0:
                cout <<name[rand() % 2] << " " <<behaviour[rand() % 4]<< " " <<object[rand() % 4] <<endl;
            break;
            case 1:
                cout <<name[rand() % 2] << " " <<behaviour[rand() % 4]<< " "<<texture[rand() % 3] << " " <<object[rand() % 4] <<endl;
            break;
            case 2:
                cout <<name[rand() % 2] << " " <<behaviour[rand() % 4]<< " " <<position[rand() % 4] <<endl;
            break;
            case 3:
                cout <<pronoun[rand() % 3] << " " <<behaviour[rand() % 4]<< " " <<object[rand() % 4] <<endl;
            break;
            case 4:
                cout <<pronoun[rand() % 3] << " " <<behaviour[rand() % 4]<< " " <<position[rand() % 4] <<endl;
        }

}