【问题标题】:Problem displaying array. Everything is placed into a separate row, but not into separate columns?显示数组时出现问题。一切都放在单独的行中,但不放在单独的列中?
【发布时间】:2019-05-06 03:01:42
【问题描述】:

我正在开发一个基于文本的扫雷程序作为家庭作业。我在显示数组时遇到问题,还没有弄清楚我缺少什么。基本上我试图让程序在行和列中显示“5”,以确保我写得正确。但是,它在一长列中显示一串 100 个 5。我相信它是将行数与列数相乘,它应该只是 10 乘 10。是什么导致了这个错误?

感谢您的帮助。

#include <iostream>
#include <iomanip>
#include <time.h>
#include <stdlib.h>
using namespace std;

const int MAX_ROWS = 10;
const int MAX_COLUMNS = 10;
const int EMPTY_SQUARE_DIGIT = 0;
const char EMPTY_SQUARE_SYMBOL = '_';
const int BOMB_DIGIT = -1;
const char BOMB_SYMBOL = '#';

void fillTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS]);
void displayTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS]);

int main(void)
{
int gameBoard[MAX_ROWS][MAX_COLUMNS];

srand(time(NULL));

fillTheGameBoard(gameBoard);
displayTheGameBoard(gameBoard);




system("pause");

return 0;
}


void fillTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS])
{
    for (int row = 0; row < MAX_ROWS; row++)

        for (int column = 0; column < MAX_COLUMNS; column++)
        {
            board[row][column] = 5;
        }
} 


void displayTheGameBoard(int board[MAX_ROWS][MAX_COLUMNS])
{
    for (int row = 0; row < MAX_ROWS; row++)

        for (int column = 0; column < MAX_COLUMNS; column++)
        {
            cout << board[row][column] << " " << endl;
        }
}

【问题讨论】:

  • srand(time(NULL)); - 你应该看rand() Considered Harmful
  • 使用所有 UPPERCASE 编译时常量是反模式,你创造了它最初试图解决的问题。

标签: c++


【解决方案1】:

为每个单元格打印一个换行符。

cout << board[row][column] << " " << endl;

这解释了“所有内容都放在单独的行中”位。您现在可能知道要解决什么问题了。

for (int row = 0; row < MAX_ROWS; row++)
{
    for (int column = 0; column < MAX_COLUMNS; column++)
        cout << board[row][column] << " ";
    cout << endl;
}

【讨论】:

  • 谢谢!!那行得通,我只是看错了问题。再次感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-08-14
相关资源
最近更新 更多