0

これは compsci に対する私の課題であり、星のフィールド '*' をユーザーが望む回数繰り返す方法の最後で立ち往生しています。

一連の星のフィールド (つまり、「*」) を出力するプログラムを作成します。各フィールドには n 行 m 列があります。ユーザーが行数 (1 以上 5 以下) と列数 (5 以上 50 以下)、およびフィールド数 (3 以上 10 以下) を入力できるようにします。 . 各フィールドは、3 つの完全な空白行で区切る必要があります。

プログラムには少なくとも 2 つの関数が含まれている必要があります。1 つはユーザーから入力データを取得する関数で、もう 1 つは各フィールドを描画する関数です。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 つの異なるループをすべて入れ子にする必要があることがわかります。しかし、制約があります。

各行の終わりで、新しい行を開始する必要があります。

各フィールドの最後には、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 に答える