Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 偶数行显示不正确,而奇数行显示不正确_C++_For Loop - Fatal编程技术网

C++ 偶数行显示不正确,而奇数行显示不正确

C++ 偶数行显示不正确,而奇数行显示不正确,c++,for-loop,C++,For Loop,背景和问题: 我已经为课堂创建了一个钻石形状的程序,但是我对钻石的行数有问题。我想我的逻辑可能有问题。似乎每当我为菱形使用偶数行时,它不会显示为偶数行,而是奇数行 我尝试过各种可能的“解决方案”,但都不起作用 例如,我将(int b=0;b

背景和问题:

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

我尝试过各种可能的“解决方案”,但都不起作用

例如,我将(int b=0;b更改为(int b=0;b I; j=(i-1)/2; 对于(intz=0;zcout您可以在从用户获得输入后添加if-else语句。然后您可以根据该输入决定如何显示菱形

伪代码:

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

听起来您可能需要学习如何使用调试器来逐步完成代码。有了一个好的调试器,您可以逐行执行程序,并查看它与预期的偏差。如果您要进行任何编程,这是一个必不可少的工具。进一步阅读:@NathanOliver感谢您提供的有见地的链接,这非常适合您我试着一步一步地使用我的代码。下面是我所做的:步骤1:我们输入4,得到4行菱形。步骤2:j=(4-1)/2=1.5步骤3:space=abs(1.5-0)空格=1.5步骤4:asterisk=4-2*1.5 asterisk=1这意味着它应该在底部添加一个星号,但它似乎没有这样做,因为某些原因是空的?我确实注意到,在奇数行上,它在底部返回一个空行。@Nite
j
不能等于
1.5
j
是一个integer.当你输入
4
偶数
时,你期望得到什么?@MilesBudnek抱歉,1.谢谢你的建议!我之前考虑过这个问题,但决定不去做。我最终做了,而且它工作正常,但我认为我的逻辑对于偶数行仍然有缺陷。我现在需要弄清楚这个问题。我是t更改了的
(int b=0;b@Nite问题是偶数和奇数之间的对齐。因此,我假设偶数的逻辑根本不起作用?我可能需要全部重写吗?我想不出任何可以改变的东西来修复它。我已经摆弄了我能想到的一切。@Nite即使你是在长度为
N
的字符串中处理特定类型的搜索时,同样的问题也会出现。由于奇数和偶数情况,for循环中的条件检查非常重要。如果您有inclusive
,我尝试尽我所能调试,这就是我得到的结果:我注意到在第一次运行偶数侧时,我得到了星号是2而不是1,最后我得到了0。我不想弄清楚如何解决这个问题。这也是for循环:
for(int b=0;b=asterisk;b++)
不是我在前面的评论中所说的如何更改它。
{
    if ( odd ) {
        // do it this way
    } else { // even 
        // do it this way
    }
}
#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!