我只是想知道我在这里做错了什么,因为当程序执行时,它运行得很好。我是编程的新手,我很迷茫。任何建议都会很棒!
#include <stdio.h>
#include <cs50.h>
#include <math.h>
int main(void)
{
printf("Days in Month:\n");
int days = GetInt();
for (; days < 28 || days > 31; days = GetInt())
{
printf("No month has that amount of days! Please try again.\n");
}
printf("Pennies on first day:\n");
int pennies = GetInt();
for (; pennies < 0; pennies = GetInt())
{
printf("You can't have negative pennies! Please try again.\n");
}
if (pennies == 1)
printf("$%.2f",pow ((pennies + 1), (days - 1)));
else
printf("$%.2f",pow (pennies, days));
}
Check50收益率:
~/workspace/chapter1/ $ check50 1617.chapter1.pennies pennies.c
:) pennies.c exists
:) pennies.c compiles
:( 28 days, 1 penny on first day
\ expected output, but not "$134217728.00"
:( 31 days, 1 penny on first day
\ expected output, but not "$1073741824.00"
:( 29 days, 2 pennies on first day
\ expected output, but not "$536870912.00"
:( 30 days, 30 pennies on first day
\ expected output, but not "$20589113209464899002378237447530552256..."
:) rejects days not 28, 29, 30, or 31
:( rejects non-positive pennies
\ expected prompt for input, not exit code of 0
:) rejects a non-numeric days input of "foo"
:) rejects a non-numeric penny input of "foo"
:) rejects a non-numeric days input of ""
:) rejects a non-numeric penny input of ""
正如你所看到的那样,它表示接受负面投入,并没有产生正确的金额。这是check50中的错误,还是我做错了什么?
答案 0 :(得分:1)
假设它是this assignment,你的数学错误。
如果您的初始便士数量每天增加一个月,则会发生分配。如果你从5便士开始,那意味着5然后10然后20然后40 ... 5 * 2 * 2 * 2 ...或5 * 2 N 其中N
是数字几天。
但你有pow(pennies, days)
。如果pennies = 5
和days = 30
是5 30 ,这是完全不同且非常大的。
您需要的是pennies * pow(2, days)
。这就是便士的数量,所以除以100得到美元金额。
这不是100%正确,但由于这是一项练习,我将其余细节留给您。
至于“退出0”问题,就是这样:
for (; pennies < 0; pennies = GetInt())
{
printf("You can't have negative pennies! Please try again.\n");
}
你允许0便士。你应该拒绝这一点。
如果用户没有为第一天的便士数输入正整数,程序应提示用户重试。
至于为什么它说它没有得到提示,它给你0便士,并预计会再次提示。编写检查代码的人没想到看是否有输出。