简体   繁体   English

计算误差

[英]Computation error

I've been having trouble with my program. 我的程序遇到了麻烦。 Im supposed to take in 3 variables and plug them into a formula to get an answer. 我应该接受3个变量并将其插入公式中以获得答案。 My answer comes out to 0.0 and im not sure what i am doing wrong. 我的答案是0.0,我不确定我在做什么错。

public double compute_cert (int years, double amount, double rate, double certificate)
{
    certificate = amount * Math.pow(1 + rate/100, years);
    return certificate;
}

The variables rate, amount and years are set up correctly but the answer certificate is always returned as 0.0 正确设置了变量费率,金额和年份,但答案证书始终返回为0.0

public static void main(String[] args)
{
    int years = 0;
    double amount = 0;
    double rate = 0;
    double certificate = 0;
    char ans;// allows for char 

    do{
        CDProgram C = new CDProgram(years, amount, rate, certificate);
        C.get_years();
        C.get_amount();
        C.get_rate();
        C.get_info();
        C.compute_cert(years, amount, rate, certificate);
        System.out.println ("Would you like to repeat this program? (Y/N)");
        ans = console.next().charAt(0);// user enters either Y or y until they wish to exit the program
   } while(ans == 'Y'||ans == 'y'); // test of do/while loop

}

Not sure what else to do. 不知道该怎么办。 Thanks for the help 谢谢您的帮助

It looks like you are not assigning the local variables that you are passing into the computation function? 看来您没有分配要传递给计算功能的局部变量?

   years = C.get_years();
   amount = C.get_amount();
   rate = C.get_rate();
   info = C.get_info();

As it is, the code is just passing 0 for every parameter into your function. 实际上,代码只是将每个参数的0传递给函数。 Multiplying by 0 will get you 0 . 乘以0将得到0 If you pass 0 , the following line will multiply 0 by some quantity. 如果传递0 ,则下一行将0乘以一定数量。

certificate = amount * Math.pow(1 + rate/100, years);

It looks like your CDProgram class has fields for years , amount and rate , and your get_ method are prompting the user for the values. 它看起来像你的CDProgram类有场yearsamountrate ,和你get_方法,促使这些值的用户。

That being the case, it doesn't make sense to pass parameters for them into your calculation method. 在这种情况下,没有必要将参数传递给您的计算方法。 I would change the method to this. 我将方法更改为此。

public double compute_cert () {
    certificate = amount * Math.pow(1 + rate/100, years);
    return certificate;
}

Then when you call it in main , don't pass any values in. This will just use the values from the fields in the CDProgram class. 然后,当您在main调用它时,不要传入任何值。这只会使用CDProgram类中字段的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM