我需要一些帮助来诊断我的代码的问题

问题描述 投票:0回答:0

我正在学习 CS50 课程,我进行了一次练习,我必须打印一个带有 # 的金字塔,在 C 语言中看起来像这样,使用用户选择的 1 到 8(包括)之间的高度:

    #
   ##
  ###
 ####
#####

但是我的代码不起作用,我不想去chatgpt。

请不要给我答案!我只是想要一些提示!

这是我的代码:

`

int main(void)
{
    // Recebe uma altura do usuário entre 1 e 8;
    int h;
    do
    {
        h = get_int("Altura: ");
    }
    while (h < 1 || h > 8);

    // acrescenta uma quebra de linha até que atinja a altura
    for (int i = 0; i < h; i++)
    {
         // acrescenta uma # de acordo com a quantidade de quebra de linha
         for (int b = 0; b <= i; b++)
         {
             for (int j = h - 1; j > i; j--)
             {
                 printf(" ");
             }

             printf("#");
         }

         printf("\n");
    }
}    

` 我做了第一个金字塔,看起来像这样:

      #
      ##
      ###
      ####
      #####

而且我知道如何做相反的事情,因为要拥有正确的金字塔,我需要将这些 # 替换为空白,然后看起来像:

  *****
   ****
    ***
     **
      *

所以我知道我需要将这两者混合起来才能得到正确的金字塔,但是当我混合它们时,它不起作用。

  ****#
  ***##
  **###
  *####
  #####

Here is the output of my code

c logic
© www.soinside.com 2019 - 2024. All rights reserved.