2016-07-15 64 views
-6

这是用于cs50马里奥金字塔问题集。我让它在没有功能的情况下工作,但我想尝试一些不同的东西。由于用户输入数字后的更改符合参数,程序将停止。它不会打印“#”,或者像过去那样的“”。C#,函数运行后我的程序停止运行,我做错了什么?

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

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

这不是C#。 – crashmstr

+0

函数返回应该是'return num'而不是'return 0' –

+0

学习如何调试,而不是在SO上发布最小的问题。如果你在调试器中检查代码,你会发现'height'为零,所以你的'for'循环甚至不会运行,因为它没有使用'i Quantic

回答

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

+0

他在使用之前进行初始化,do会先运行一次,在使用之前对其进行初始化。 – Dispersia

+0

是的,你是对的。我很抱歉,我编辑了我的答案 –

相关问题