[英]how to print random cout statement?
基本上,我有五個不同的cout
輸出,我想從五個中隨機打印其中一個。
#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));
cout <<name[rand() % 2] << " " <<behaviour[rand() % 4]<< " " <<object[rand() % 4] <<endl;
cout <<name[rand() % 2] << " " <<behaviour[rand() % 4]<< " "<<texture[rand() % 3] << " " <<object[rand() % 4] <<endl;
cout <<name[rand() % 2] << " " <<behaviour[rand() % 4]<< " " <<position[rand() % 4] <<endl;
cout <<pronoun[rand() % 3] << " " <<behaviour[rand() % 4]<< " " <<object[rand() % 4] <<endl;
cout <<pronoun[rand() % 3] << " " <<behaviour[rand() % 4]<< " " <<position[rand() % 4] <<endl;
}
您基本上需要對數組執行相同的操作,只需生成一個 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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.