[英]Required: Double, int Found: String, String
这是我的代码: -
公共课程计划{
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;
}
}
这是我编译程序时的问题:
required:double,double,int,int
found:String,String,String,String
reason:实际参数String不能通过方法调用转换转换为double
Program.java:19:错误:类CompoundInterestCalculator中的方法ContinuousCompound无法应用于给定类型;
首先,将String与.equals
not ==
进行比较,因为在使用==
比较对象时比较内存地址。
所以(args[0] == "interest")
成为(args[0].equals("interest"))
其次,您将String
s传递给声明为的方法
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]);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.