偶数行不能正常显示,而奇数行则不能

时间:2018-02-09 03:19:39

标签: c++ for-loop

背景和问题:

我已经为课程创建了一个钻石形状程序,但是我遇到了钻石偶数行的问题。我在想我的逻辑可能有问题。似乎每当我为钻石形状使用偶数行时,它不会显示为偶数行而是奇数。

Here's an example

我尝试了不同的“解决方案”,但它们没有用。

例如,我将for (int b = 0; b < asterisk; b++)更改为(int b = 0; b <= asterisk; b++)它显示了正确的行数,但它不再是一个合适的钻石形状。它(显然)也影响了奇数行钻石,所以它们看起来也不像是正确的钻石。

Example of the above

我完全陷入困境,肯定会欣赏正确方向的推动。

这是我的代码:

#include <iostream>

using namespace std;

int main() {

    int i, j, space, asterisk;

    do
    {

        cout << "Enter the number of rows desired to make a diamond pattern (0 to quit): ";
        cin >> i;

        j = (i - 1) / 2;

        for (int z = 0; z < i; z++)
        {
            space = abs(j - z);

            asterisk = i - 2 * space;

            for (int a = 0; a < space; a++)
                cout << " ";
            for (int b = 0; b < asterisk; b++)
                cout << "*";
            for (int c = 0; c < space; c++)
                cout << " ";

            cout << endl;
        }
    } while (i > 0);

    cout << "Goodbye!" << endl;

}

非常感谢!

2 个答案:

答案 0 :(得分:1)

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

int main() {

    int i, j, space, asterisk, is_even;

    do
    {

        cout << "Enter the number of rows desired to make a diamond pattern (0 to quit): ";
        cin >> i;

        is_even = (i % 2 == 0) ? 1 : 0;
        //Above line uses ternary operator to assign is_even flag to 1 if the number is even and 0 if it is not.
        j = (i - 1) / 2;

        for (int z = 0; z < i; z++)
        {
            space =  abs(j - z);

            asterisk = (is_even) ? i - 2 * space - 1  : i - 2 * space; //Change 1

            for (int a = 0; a < space; a++)
                cout << " ";

            //Change 2.STARTS
            if(space == 0 && is_even ){
                for (int b = 0; b < asterisk; b++)
                    cout << "*";
                cout<<endl;
            }
            //Change 2.ENDS
            for (int b = 0; b < asterisk; b++)
                cout << "*";

            //for (int c = 0; c < space; c++)
            //    cout << " ";
            //You dont need to add the spaces at the end of each line.

            cout << endl;
        }
    } while (i > 0);

    cout << "Goodbye!" << endl;

}

输出::

Enter the number of rows desired to make a diamond pattern (0 to quit): 1
*
Enter the number of rows desired to make a diamond pattern (0 to quit): 2
*
*

Enter the number of rows desired to make a diamond pattern (0 to quit): 3
 *
***
 *
Enter the number of rows desired to make a diamond pattern (0 to quit): 4
 *
***
***
 *

Enter the number of rows desired to make a diamond pattern (0 to quit): 5
  *
 ***
*****
 ***
  *
Enter the number of rows desired to make a diamond pattern (0 to quit): 6
  *
 ***
*****
*****
 ***
  *

Enter the number of rows desired to make a diamond pattern (0 to quit): 7
   *
  ***
 *****
*******
 *****
  ***
   *
Enter the number of rows desired to make a diamond pattern (0 to quit): 8
   *
  ***
 *****
*******
*******
 *****
  ***
   *

Enter the number of rows desired to make a diamond pattern (0 to quit): 0
Goodbye!

答案 1 :(得分:0)

您可以在从用户获取输入后添加if else语句。然后,您可以根据输入结果决定如何显示钻石。

伪代码:

{
    if ( odd ) {
        // do it this way
    } else { // even 
        // do it this way
    }
}