如何设置 class 中的值 Present Value 以调用方法
how can I set the value Present Value in my class to be called into a Method
我目前正在尝试从 FutureValue 方法中的 PresentValue 方法中提取 return 值进行计算。
我试图通过声明 pv1 并仅使用该值来解决问题。新问题是它被设置为 0。
有谁知道我该如何解决这个问题,或者至少能够使用从现值 return 得到的值?
public class Bank1 {
private double cash;
private double rate = .0425;
private int years;
public Bank1(double cash, int years){
this.cash = cash;
this.years = years;
}
private double pv1 = cash/Math.pow((1+rate), years);
public double PresentValue(){
double pv = cash/Math.pow((1+rate), years);
double present = Math.round(pv *100);
present = present/100;
return present;
}
public double FutureValue(){
double fv = pv1*Math.pow((1+rate), years);
double future = Math.round(fv *100);
future = future/100;
return future;
}
}
输出只是对方法的调用。
我收到的输出是
你想要多少钱?
100
你投资了多少年? 1
100.0 美元是您想要的金额。
95.92 美元是您需要投资的金额
$0.0 将是您的未来价值
感谢您的帮助!!
给FutureValue
一个参数,这样你就可以传入结果:
public double FutureValue(double value) {
//use value
}
调用时传入PresentValue
的返回值:
double value = PresentValue();
double futureValue = FutureValue(value);
按照惯例,方法名称(标识符)应以小写字母开头。唯一应该以大写字母开头的是类型标识符(class/enum/interface 名称)或常量(静态最终变量)
只需更改 pv1*Math.pow((1+rate), years);
for this.PresentValue()*Math.pow((1+rate), years);
public class Bank1 {
private double cash;
private double rate = .0425;
private int years;
public Bank1(double cash, int years){
this.cash = cash;
this.years = years;
}
private double pv1 = cash/Math.pow((1+rate), years);
public double PresentValue(){
double pv = cash/Math.pow((1+rate), years);
double present = Math.round(pv *100);
present = present/100;
return present;
}
public double FutureValue(){
double fv = this.PresentValue()*Math.pow((1+rate), years);
double future = Math.round(fv *100);
future = future/100;
return future;
}
public static void main(String args[]){
Bank1 bank = new Bank1(100.0, 1);
System.out.println("PresentValue: " + bank.PresentValue());
System.out.println("FutureValue: " + bank.FutureValue());
}
}
有几种方法可以解决这个问题,但鉴于您的 class 是不可变的,最好的方法可能是在构造函数中预先计算当前值和未来值,然后简单地 return 它们来自访问器方法。请注意,我还将 presentValue 传递给 calculateFutureValue():
public class Bank1 {
private double cash;
private double rate = .0425;
private int years;
private double presentValue;
private double futureValue;
public Bank1(double cash, int years){
this.cash = cash;
this.years = years;
presentValue = calculatePresentValue();
futureValue = calculateFutureValue(presentValue);
}
private double calculatePresentValue(){
double pv = cash/Math.pow((1+rate), years);
double present = Math.round(pv *100);
present = present/100;
return present;
}
private double calculateFutureValue(final double value){
double fv = value*Math.pow((1+rate), years);
double future = Math.round(fv *100);
future = future/100;
return future;
}
public double getPresentValue() {
return presentValue;
}
public double getFutureValue() {
return futureValue;
}
}
我目前正在尝试从 FutureValue 方法中的 PresentValue 方法中提取 return 值进行计算。
我试图通过声明 pv1 并仅使用该值来解决问题。新问题是它被设置为 0。
有谁知道我该如何解决这个问题,或者至少能够使用从现值 return 得到的值?
public class Bank1 {
private double cash;
private double rate = .0425;
private int years;
public Bank1(double cash, int years){
this.cash = cash;
this.years = years;
}
private double pv1 = cash/Math.pow((1+rate), years);
public double PresentValue(){
double pv = cash/Math.pow((1+rate), years);
double present = Math.round(pv *100);
present = present/100;
return present;
}
public double FutureValue(){
double fv = pv1*Math.pow((1+rate), years);
double future = Math.round(fv *100);
future = future/100;
return future;
}
}
输出只是对方法的调用。
我收到的输出是
你想要多少钱? 100
你投资了多少年? 1
100.0 美元是您想要的金额。
95.92 美元是您需要投资的金额
$0.0 将是您的未来价值
感谢您的帮助!!
给FutureValue
一个参数,这样你就可以传入结果:
public double FutureValue(double value) {
//use value
}
调用时传入PresentValue
的返回值:
double value = PresentValue();
double futureValue = FutureValue(value);
按照惯例,方法名称(标识符)应以小写字母开头。唯一应该以大写字母开头的是类型标识符(class/enum/interface 名称)或常量(静态最终变量)
只需更改 pv1*Math.pow((1+rate), years);
for this.PresentValue()*Math.pow((1+rate), years);
public class Bank1 {
private double cash;
private double rate = .0425;
private int years;
public Bank1(double cash, int years){
this.cash = cash;
this.years = years;
}
private double pv1 = cash/Math.pow((1+rate), years);
public double PresentValue(){
double pv = cash/Math.pow((1+rate), years);
double present = Math.round(pv *100);
present = present/100;
return present;
}
public double FutureValue(){
double fv = this.PresentValue()*Math.pow((1+rate), years);
double future = Math.round(fv *100);
future = future/100;
return future;
}
public static void main(String args[]){
Bank1 bank = new Bank1(100.0, 1);
System.out.println("PresentValue: " + bank.PresentValue());
System.out.println("FutureValue: " + bank.FutureValue());
}
}
有几种方法可以解决这个问题,但鉴于您的 class 是不可变的,最好的方法可能是在构造函数中预先计算当前值和未来值,然后简单地 return 它们来自访问器方法。请注意,我还将 presentValue 传递给 calculateFutureValue():
public class Bank1 {
private double cash;
private double rate = .0425;
private int years;
private double presentValue;
private double futureValue;
public Bank1(double cash, int years){
this.cash = cash;
this.years = years;
presentValue = calculatePresentValue();
futureValue = calculateFutureValue(presentValue);
}
private double calculatePresentValue(){
double pv = cash/Math.pow((1+rate), years);
double present = Math.round(pv *100);
present = present/100;
return present;
}
private double calculateFutureValue(final double value){
double fv = value*Math.pow((1+rate), years);
double future = Math.round(fv *100);
future = future/100;
return future;
}
public double getPresentValue() {
return presentValue;
}
public double getFutureValue() {
return futureValue;
}
}