【发布时间】:2019-01-12 07:30:50
【问题描述】:
我试图让这个程序打印出一个 10 x 10 的板,其中包含从 4 x 4 数组中随机抽取的值。
所有行的长度应为 10 个条目;出于某种原因,有些行是 7 和 10 个条目。其他时候,他们每个人正好有 10 个,但往往没有一个或多个是短的。只是想弄清楚如何让这个东西每次打印出 10 行和列。编码新手。
iostream, cstdlib, string, math.h, time.h
using namespace std;
int main()
{
srand(time(NULL));
string worldMap[10][10];
string envir[4][4] = { {"S1", "S2", "S3", "S4"}, {"P1", "P2", "P3", "P4"},
{"F1", "F2", "F3", "F4"}, {"D1", "D2", "D3", "D4"} };
for (int i = 0; i < 10; i++)
{
for (int j = 0; j < 10; j++)
{
int cat = rand()%4+1;
int subcat = rand()%4+1;
worldMap[i][j] = envir[cat][subcat];
cout<<worldMap[i][j];
}
cout<<endl;
}
return 0;
}
【问题讨论】:
-
只使用
rand()%4,不使用+1。