[英]Computation error
我的程序遇到了麻煩。 我應該接受3個變量並將其插入公式中以獲得答案。 我的答案是0.0,我不確定我在做什么錯。
public double compute_cert (int years, double amount, double rate, double certificate)
{
certificate = amount * Math.pow(1 + rate/100, years);
return certificate;
}
正確設置了變量費率,金額和年份,但答案證書始終返回為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
}
不知道該怎么辦。 謝謝您的幫助
看來您沒有分配要傳遞給計算功能的局部變量?
years = C.get_years();
amount = C.get_amount();
rate = C.get_rate();
info = C.get_info();
實際上,代碼只是將每個參數的0
傳遞給函數。 乘以0
將得到0
。 如果傳遞0
,則下一行將0
乘以一定數量。
certificate = amount * Math.pow(1 + rate/100, years);
它看起來像你的CDProgram
類有場years
, amount
和rate
,和你get_
方法,促使這些值的用戶。
在這種情況下,沒有必要將參數傳遞給您的計算方法。 我將方法更改為此。
public double compute_cert () {
certificate = amount * Math.pow(1 + rate/100, years);
return certificate;
}
然后,當您在main
調用它時,不要傳入任何值。這只會使用CDProgram
類中字段的值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.