【问题标题】:CS50 Lab1 - Not printing anythingCS50 Lab1 - 不打印任何东西
【发布时间】:2021-11-28 23:47:03
【问题描述】:

我正在尝试为初学者做 CS50 的 lab1。我很难理解为什么我的代码不起作用。

向用户询问起始尺寸和结束尺寸可以正常工作。

问题是当我计算达到最终大小所需的年数时,然后显示结果。它不起作用,我看不出它为什么不起作用。

谁能看到我在代码中做错了什么?我看过类似的问题,但我仍然无法理解我做错了什么。

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

// Determine the number of years required for the population to grow from the start size to the end size.


int main(void)
{
    // TODO: Prompt for start size. Has to be greater than 9, otherwise ask again.
    // TODO: Prompt for end size
int start;
int end;
int years = 0;
int n;
do
{
    start=get_int("Give me a start size of the population: ");
    end=get_int("Give me a end size of the population: ");
    
}
while (start<9 || end<start);
return start;
return end;

    // TODO: Calculate number of years until we reach threshold

do
{
    n = start + (start/3)-(start/4);
    start=n;
    years++;
}
 while (start<end);
 return years;

    // TODO: Print number of years

    printf("The number of years to reach your end population is %i/n", years);
}

【问题讨论】:

    标签: c printf cs50


    【解决方案1】:

    行:

    return start;
    

    退出主程序 更一般地说,您在 main 中的所有返回都将退出您的程序

    【讨论】:

    • 效果很好,谢谢!这是否意味着我们应该只在 Main 之外创建函数时才使用 return?
    • Return 的意思是“退出我所在的函数,并最终向调用者发送一个值。在 main 的情况下,这意味着以指定的状态退出程序。我是你的情况,如果你输入start=6,你可以在程序执行后得到6的状态。
    • 好的,谢谢!
    【解决方案2】:

    new 是保留关键字。禁止用作变量名!

    【讨论】:

    • 谢谢。我已将“new”更改为“n”,但它仍然没有显示“years”,这是我正在尝试做的。
    • new 是 C89、C99、C11、C18、C2x 变量、函数、宏等的完美标识符...... 虽然我尝试(不是很努力)不使用它:-)
    猜你喜欢
    • 2016-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-11
    • 2018-12-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多