【发布时间】:2015-04-11 06:21:31
【问题描述】:
这是我的代码:-
公开课程序{
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;
}
}
这是我编译程序时遇到的问题:-
必需:双精度、双精度、整数、整数
找到:字符串、字符串、字符串、字符串
原因:实参String无法通过方法调用转换为double
Program.java:19: 错误:CompoundInterestCalculator 类中的方法 ContinuousCompound 不能应用于给定类型;
【问题讨论】:
-
Java 是强类型的。您必须将这些字符串显式转换为相关类型。
-
@lared 所以你的意思是说,我必须在 runCal.compoundAnnually(args, args) 之后明确键入 args[no.] 作为所需的类型?
-
您必须使用 Jean-Francois 在他的回答中提到的方法 - Double.parseDouble
-
@lared 好的,明白了:)