我正在尝试返还银行余额,该余额考虑了年数,帐户金额和利率。但是,当我编译程序时,我在return语句中收到错误。当我尝试将返回放在循环外时,我得到另一个错误。
In Loop:
Exercise3.java:35: error: missing return statement
Out of Loop:
Exercise3.java:34: error: cannot find symbol
}return sum;
^
symbol: variable sum
location: class Exercise3
1 error
代码在
之下import java.util.Scanner;//import scanner class
public class Exercise3{//Name of prograsm
public static void main(String args[]){ //declare main method
Scanner sc = new Scanner(System.in);//Declare the scanner method
System.out.println("How much in in your account?");//ask user for balance details
double accBalance = sc.nextDouble();//Store balance details
System.out.println("how many years are you saving? "); //ask the user for how many years they will be saveing
double yrSaving = sc.nextDouble();// Amount of years stored
System.out.println("What is the yearly interest rate ?");//ask the user for the interest rate
double rateInterest = sc.nextDouble();//store the interest rate
double results = balanceAccount(accBalance, yrSaving, rateInterest);//invoke the methofd
System.out.println(results); //print thte results of the method
}
////////////////////////////////////////////////////////////////////////////////
//Calculate the balance of the account method
public static double balanceAccount(double accBalance, double yrSaving, double rateInterest){
double rate = rateInterest / 100;
for(int x = 0; x <= yrSaving; x++){
double sum = accBalance*rate;
return sum;
}
}
}
答案 0 :(得分:1)
将此作为方法的主体使用。你的问题是你没有在每个场景中返回。你的sum
逻辑也是错误的。
double rate = rateInterest / 100;
double sum = 0;
for(int x = 0; x <= yrSaving; x++){
sum += accBalance * rate;
}
return sum;
答案 1 :(得分:0)
这就是我做的方式
public static double balanceAccount(double accBalance,double yrSaving,double rateInterest){
double rate = rateInterest / 100;
double sum = 0;
double finishAmt = 0;
sum += accBalance * rate;
finishAmt = sum * yrSaving +(accBalance);
return finishAmt;
}
}