【问题标题】:Nested For Loop Problems in CC 中的嵌套 For 循环问题
【发布时间】:2022-11-21 04:19:30
【问题描述】:

我试图理解并弄清楚如何制作一个从左到右的金字塔。

我有电脑问身高,只有1-8

我试图根据高度使金字塔看起来像这样

我做了一个平方。 这是代码

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

int main(void)
{
    int getHeight;

    do
    {
        //asks height
        getHeight = get_int("Height: ");
    }
    //If the height is greater then 8 then ask for the Height again
    while(getHeight > 8);


    //
    for(int row = 0; row < getHeight; row++)
    {
        for(int colums = 0; colums < getHeight; colums++)
        {
            printf("#");
        }
        printf("\n");
    }


}**

【问题讨论】:

  • 花点时间考虑一下。也许用笔和纸把它画出来,想出一个算法。例如,对于第一行,您应该打印多少列?

标签: c cs50


【解决方案1】:

看起来你的内循环是错误的。它迭代到 getHeight 但应该只迭代到(包括)row

for(int row = 0; row < getHeight; row++)
{
    for(int colums = 0; colums <= row; colums++)
    { //                       ^^^^^^

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-01-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-02
    相关资源
    最近更新 更多