这是我的代码: -
公共课程计划{public static void main(String[] args) {
if (args[0] == "interest"){
public CompoundInerestCalculator(){
CompoundInterestCalculator runCal = new CompoundInterestCalculator();
if (args[1] == "annual"){
runCal.compoundAnnually(args[2], args[3], args[4], args[5]);
}
else {
args[1] = "continuous";
runCal.continuousCompound(args[2], args[3], args[4]);
}
}
}
从中提取代码的下一课: -
公共类CompoundInterestCalculator {
// Calculation for Annual Compound Interest
public BigDecimal compoundAnnually(double principal, double rate, int periods, int years) {
double finalAmount = principal * Math.pow( 1 + ( rate / periods), years * periods);
double compoundInterest = finalAmount - principal;
BigDecimal finalInterest = new BigDecimal(compoundInterest);
System.out.print(finalInterest);
return finalInterest;
}
// Calculation for Continuous Compound Interest
public BigDecimal continuousCompound(double principal, double rate, int years) {
double finalAmount = principal * Math.pow( Math.E, rate * years);
double compoundInterest = finalAmount - principal;
BigDecimal finalInterest = new BigDecimal(compoundInterest);
System.out.print(finalInterest);
return finalInterest;
}
}
编译程序时出现问题: -
必需:double,double,int,int
发现:String,String,String,String
原因:实际参数String不能通过方法调用转换转换为double
Program.java:19:错误:类CompoundInterestCalculator中的方法continuousCompound不能应用于给定的类型;
答案 0 :(得分:0)
首先,在使用.equals
比较对象时比较内存地址时,将String与==
进行比较而不是==
。
因此(args[0] == "interest")
成为(args[0].equals("interest"))
其次,您将String
传递给声明为
compoundAnnually(double principal, double rate, int periods, int years)
通过此次通话
runCal.compoundAnnually(args[2], args[3], args[4], args[5]);
您需要将它们解析为正确的类型......
runCal.compoundAnnually(Double.parseDouble(args[2]), Double.parseDouble(args[3]), Integer.parseInt(args[4]), Integer.parseInt(args[5]))
第三,对呼叫runCal.continuousCompound(args[2], args[3], args[4]);