【问题标题】:Random Number Generator not working with 2D arrays that output - C++ [closed]随机数生成器不适用于输出的二维数组 - C++ [关闭]
【发布时间】:2014-06-15 03:52:18
【问题描述】:

好的,我有一个问题。我试图让代码以如下格式输出到控制台:

123456
789012
345678
901234
567890
123456

但它是这样显示的:

123456

我不知道问题是什么,但这里是代码:

void mazeLayout() {
    srand(time(NULL));
    const int mazeLayoutArraySize = 6;
    int mazeLayoutOne = rand()%6+1;
    int mazeLayoutTwo = rand()%6+1;
    int mazeLayoutThree = rand()%6+1;
    int mazeLayoutFour = rand()%6+1;
    int mazeLayoutFive = rand()%6+1;
    int mazeLayoutSix = rand()%6+1;
    int mazeLayoutSeven = rand()%6+1;
    int mazeLayoutEight = rand()%6+1;
    int mazeLayoutNine = rand()%6+1;
    int mazeLayoutTen = rand()%6+1;
    int mazeLayoutEleven = rand()%6+1;
    int mazeLayoutTwelve = rand()%6+1;
    int mazeLayoutThirteen = rand()%6+1;
    int mazeLayoutFourteen = rand()%6+1;
    int mazeLayoutFifteen = rand()%6+1;
    int mazeLayoutSixteen = rand()%6+1;
    int mazeLayoutSeventeen = rand()%6+1;
    int mazeLayoutEightteen = rand()%6+1;
    int mazeLayoutNineteen = rand()%6+1;
    int mazeLayoutTwenty = rand()%6+1;
    int mazeLayoutTwentyOne = rand()%6+1;
    int mazeLayoutTwentyTwo = rand()%6+1;
    int mazeLayoutTwentyThree = rand()%6+1;
    int mazeLayoutTwentyFour = rand()%6+1;
    int mazeLayoutTwentyFive = rand()%6+1;
    int mazeLayoutTwentySix = rand()%6+1;
    int mazeLayoutTwentySeven = rand()%6+1;
    int mazeLayoutTwentyEight = rand()%6+1;
    int mazeLayoutTwentyNine = rand()%6+1;
    int mazeLayoutThirty = rand()%6+1;
    int mazeLayoutThirtyOne = rand()%6+1;
    int mazeLayoutThirtyTwo = rand()%6+1;
    int mazeLayoutThirtyThree = rand()%6+1;
    int mazeLayoutThirtyFour = rand()%6+1;
    int mazeLayoutThirtyFive = rand()%6+1;
    int mazeLayoutThirtySix = rand()%6+1;
    int mazeLayoutArray[mazeLayoutArraySize][mazeLayoutArraySize]={{mazeLayoutOne, mazeLayoutTwo, mazeLayoutThree, mazeLayoutFour, mazeLayoutFive, mazeLayoutSix},
                               {mazeLayoutSeven, mazeLayoutEight, mazeLayoutNine, mazeLayoutTen, mazeLayoutEleven, mazeLayoutTwelve},
                               {mazeLayoutThirteen, mazeLayoutFourteen, mazeLayoutFifteen, mazeLayoutSixteen, mazeLayoutSeventeen, mazeLayoutEightteen},
                               {mazeLayoutNineteen, mazeLayoutTwenty, mazeLayoutTwentyOne, mazeLayoutTwentyTwo, mazeLayoutTwentyThree, mazeLayoutTwentyFour},
                               {mazeLayoutTwentyFive, mazeLayoutTwentySix, mazeLayoutTwentySeven, mazeLayoutTwentyEight, mazeLayoutTwentyNine, mazeLayoutThirty},
                               {mazeLayoutThirtyOne, mazeLayoutThirtyTwo, mazeLayoutThirtyThree, mazeLayoutThirtyFour, mazeLayoutThirtyFive, mazeLayoutThirtySix}};

//for loop to read rows
for(int index1=0; index1<6; index1++){
        for(int index2=0; index2<6; index2++) {
            cout << mazeLayoutArray[index1][index2];
        }
        cout << endl;
        exit(0);
    }

}

如果您有任何问题,尽管问我!

【问题讨论】:

  • 第一个问题:为什么不使用循环来填充数组?
  • 在 C++ 中,您应该使用 &lt;random&gt; 标头中的随机数生成器,而不是 rand(),后者是众所周知的糟糕的随机数生成器。
  • 第三个问题(这就是关键):为什么要将exit放入循环中?
  • 我很惊讶有人坐下来输入这些变量名 - 两次
  • 也许发帖人写了一个 C++ 应用程序来生成所有变量名:)

标签: c++ random


【解决方案1】:
for(int index1=0; index1<6; index1++){
    for(int index2=0; index2<6; index2++) {
        cout << mazeLayoutArray[index1][index2];
    }
    cout << endl;
    exit(0);
}

在内循环打印出第一行后,您只需 exit()'d 程序。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 1970-01-01
    • 1970-01-01
    • 2015-04-09
    相关资源
    最近更新 更多