C#,我的程序在函数运行后停止运行,我做错了什么?

时间:2016-07-15 16:23:22

标签: c++

这是针对cs50 mario金字塔问题集。我有没有功能的工作,但我想尝试不同的东西。由于用户输入数字后的更改符合参数,程序将停止。它不会打印“#”,也不会像以前那样打印“”。

我正在网上学习这门课程,感谢任何帮助。

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

int towerheight(void);

int main(void)
{
    printf("how high would you like the tower? ");
    int height = towerheight();
    int pound;
    int space;
    for (int i = 0; i < height; i++)
    {
        for (space = height - i; space >= 0; space--)
        {
            printf(" ");
        }
        for (pound = 0; pound < i + 2; pound++)
        {
            printf("#");
        }
        printf("\n");
    }
}
int towerheight(void)
{
    int num;
    do
    {
        printf("Your number must be between 1 - 23: ");
        num = GetInt();
    }
    while (num <= 0 || num >= 24);
    return 0;
}

1 个答案:

答案 0 :(得分:3)

问题在于你的功能,替换最后一行:

int towerheight(void)
{
    int num;
    do
    {
        printf("Your number must be between 1 - 23: ");
        num = GetInt();
    }
    while (num <= 0 || num >= 24);
    return num; // replace by this
}

您始终返回0for循环未满足条件,因为height始终为0