【问题标题】:Why am I not getting the desired output of my C program? [duplicate]为什么我的 C 程序没有得到想要的输出? [复制]
【发布时间】:2018-06-21 06:03:24
【问题描述】:
//Aim of this program is to print a hash pyramid

#include <stdio.h>
#include <cs50.h>

int main(void)
{
    int height, spaces, hash;
    do
    {
        printf("Enter the height of the wall:\n");  // Prompt the user for the height
        height = get_int();
    }
    while(height<0 || height>23);

    for (int i=0; i<height; i++)
    {
        for (spaces= height-i; spaces>1; spaces--)
        {
            printf(" ");                //print spaces
            for (hash= 0; hash<=height+1; hash++)
            {
                printf("#");                //print hashes
            }
            printf("\n");                    //move to the next line
        }
    }
}

这是一个打印散列金字塔的程序。 我这样做是我在 edx 上的 CS50x 课程的一部分,所以我包含了 CS50 库。现在,关于程序,我知道我在第三个“for 循环”中搞砸了一些东西,但我没有知道它是什么。

有人可以帮我解决这个问题吗?

【问题讨论】:

  • while(h&lt;0 || h&gt;23); 中的h 是什么?
  • 如果用户输入例如数字 3,预期的输出是什么?
  • 你的第三个 for 循环应该在第二个 for 循环之后,而不是在里面。
  • @Adit 总是发布真实代码
  • 这很好奇;你已经接受了我说你应该做的答案,所以除非你的循环限制是关闭的,否则我建议的是你认为必要和可接受的。

标签: c for-loop cs50


【解决方案1】:

使用 1 个循环。
您不需要 3 个循环!

#include <stdio.h>

int main(void) {
    int height;

    do
    {
        printf("Enter the height of the wall:\n");  // Prompt the user for the height
        scanf("%d", &height);
    }
    while(height<0 || height>23);

    for(int i = 0; i < height; ++i)
    {
        printf("%d: %*.*s\n", i+1, height+i, 2*i+1, "########################################");
    }

    return 0;
}

高度为 6 的样本输出:

Success #stdin #stdout 0s 4284KB
1:      #
2:     ###
3:    #####
4:   #######
5:  #########
6: ###########

【讨论】:

  • 你是在故意设陷阱吗?
【解决方案2】:

我在您的代码中发现了三个问题

1) 打印# 的循环不应在循环打印空间内

2) 打印# 的循环有不正确的停止条件

3) 看来你搞砸了hheight。我只是假设它们应该是一样的

试试看:

#include <stdio.h>

int main()
{
    int h, spaces, hash;

    h = 5;  // Fixed input to simplify code example

    for (int i=0; i<h; i++)
    {
        for (spaces= h-i; spaces>1; spaces--)
        {
            printf(" ");
        }
        for (hash= 0; hash<=2*i; hash++)  // Notice: <= 2*i
        {
          printf("#");
        }
        printf("\n");
    }

  return 0;
}

输出:

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

【讨论】:

  • 我不太明白 2*i 是什么意思...这是什么意思?
  • 2*i(连同&lt;=的使用)是要在当前行打印的#的数量。编写该行的另一种方法是:for (hash= 0; hash &lt; (2*i + 1); hash++)
  • 哦,好的!非常感谢!
【解决方案3】:

以下代码应该适合您

#include <stdio.h>
#include <cs50.h>

int main(void)
    {
        int i, height, spaces, hash;
        do
        {
        printf("Enter the height of the wall:\n");   
                                    // Prompt the user for the height
        scanf("%d", &height);// = get_int();
        }
        while(height<0 || height >= 23);

  for ( i=0; i<height; i++)
  {
    for (spaces= height - i; spaces>1; spaces--)
    {
        printf(" ");                //print spaces
    }
    for (hash= 0; hash<=i; hash++)
    {
        printf("# ");                //print hashes
    }
    printf("\n");                    //move to the next line

   // }
  }

}

【讨论】:

    【解决方案4】:

    问题很简单,您在名为“height”的变量中输入变量,在 for 循环中您使用了未初始化的变量 h。 第三个 for 循环 将在第二个 for 循环之外

    这将是你的循环:

    for (int i=0; i<h; i++)
    {
        for (spaces= h-i; spaces>1; spaces--)
        {
            printf(" ");  
        }
    
        for (hash= 0; hash<=h+1; hash++)
        {
            printf("#");                //print hashes
        }
        printf("\n");                    //move to the next line
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多