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

C#, my program stops running after the function runs, what am i doing wrong?

本文关键字:运行 错了 什么 程序 函数 我的      更新时间:2023-10-16

这是针对 cs50 马里奥金字塔问题集的。我让它在没有功能的情况下工作,但我想尝试一些不同的东西。由于用户输入符合参数的数字后发生了变化,因此程序停止。它不会像以前那样打印"#"或" "。

我正在在线学习这门课程,任何帮助将不胜感激。

#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;
}

问题出在您的函数中,请替换最后一行:

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
}

您总是返回0并且for循环不满足条件height因为始终0