0

这是我为 compsci 分配的任务,我被困在如何让星域“*”重复用户想要多少次的最后。

编写一个程序,打印出一系列星域(即“*”)。每个字段将有 n 行和 m 列。允许用户输入行数(至少 1 但不超过 5)和列数(至少 5 但不超过 50)以及字段数(至少 3 但不超过 10) . 每个字段必须由 3 个完整的空白行分隔。

您的程序必须至少包含两个功能:一个从用户那里获取输入数据,另一个用于绘制每个字段。使用 for 循环来构造字段,以及打印出多个字段。不要使用字符串“<em>”。而是打印出单个“</em>”。

代码

#include <iostream>

using namespace std;

void displayField(int numrows, int numcolums);

void getData (int numrows, int numcolumns, int numfields);

const char c = '*';



int main(void)
{
    int numrows, numcolumns, numfields;

    cout << "Welcome to the banner creation program!" << endl;

    cout << "Enter the number of rows (1 - 5) --> ";
    cin >> numrows;

    if(numrows<1 || numrows>5){
        cout << "Your entered value is outside the range!" << endl;
        cout << "Program will now halt..." << endl;
        exit(0);}


    cout << "Enter the number of columns (5 - 50) --> ";
    cin >> numcolumns;

    if(numcolumns<5 || numcolumns>50){
        cout << "Your entered value is outside the range!" << endl;
        cout << "Program will now halt..." << endl;
        exit(0);
    }

    cout << "Enter the number of rows (3 - 10) --> ";
    cin >> numfields;

    if(numfields<3 || numrows>10){
        cout << "Your entered value is outside the range!" << endl;
        cout << "Program will now halt..." << endl;
        exit(0);
    }
for(int i=1; i<=numrows; i++){
    for (int j=1; j<=numcolumns; j++)
            cout << c;
        cout <<endl;

}


}
4

1 回答 1

0

把事情分解...

1)对于每个字段,有许多行。

2)每一行有很多列

3) 每列都有一个字符,'*'

现在这样写,我们知道我们需要嵌套 3 个不同的循环。但也有限制。

在每一行的末尾,我们需要开始一个新行。

在每个字段的末尾,我们需要三个空行。

for (int i = 0; i < numFields; i++) {
  for (int j = 0; j < numRows; j++) {
    for (int k = 0; k < numColumns; k++) {
      cout << c;
    }
    cout << endl;
  }
  cout << endl << endl << endl;
}

此外,您应该仔细检查您的工作。我注意到您在错误的地方使用了一些变量提示提示

于 2012-11-09T16:30:02.520 回答