private static double FVCALC(..)
和
private static double validate(........)
我不太明白我需要怎么做。我目前的代码只允许我输入3个利率值并停止。是因为private
mehtods?我不知道该怎么做,我已经搜索了3天了。
public class interest_rate
{
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
double i;
double n;
double FVCalc;
double PV;
System.out.print("please enter value for n (years): ");
n = input.nextInt();
System.out.print("please enter interest rate: ");
i=input.nextDouble();
System.out.print("please enter Present Value: ");
PV = input.nextDouble();
}
private static double validate (double upLimit, double lowLimit, double PV)
{
upLimit=100000.00;
lowLimit=0.00;
while(PV>upLimit|| PV<lowLimit)
{
System.out.print("please enter value between "+upLimit+" and "+lowLimit);
System.out.print("please enter PV");
PV=input.nextDouble();
}
return PV;
}
private static double FVCalc(double PV, double i, double n, double FV)
{
FV = PV*Math.pow(1+(i/100), n);
return(FV);
}
}
答案 0 :(得分:0)
首先,您需要调用 main中的方法。
其次,你无法传递PV
,然后重新分配它并期望值更新。
例如......
private static double validate (double upLimit, double lowLimit, double PV)
你需要在主体中调用它,如此
PV = 0.0; // some double value
double validated = validate(0,100000); // return the value, don't pass it in
并删除该方法中的这些行,因为覆盖参数通常很糟糕。
upLimit=100000.00;
lowLimit=0.00;
接下来,为要在整个班级中使用的值添加字段。
public class InterestRate
{
static double pv , fvCalc;
static Scanner input = new Scanner(System.in);
public static void main(String[] args) {
}
然后在main中删除这些行并改为使用这两个类变量
double FVCalc;
double PV;
此外,我没有看到将fvCalc存储为变量的理由。你可以计算它
private static double fvCalc(double pV, double i, double n)
{
return pV*Math.pow(1+(i/100), n);
}
答案 1 :(得分:0)
我相信我已经弄明白了。我做得很漂亮请原谅。我想让他们留在那里,看看我做错了什么。谢谢大家。
public class interest_rate
{
static double pV , fvCalc, i, n;
static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
double validated;
double calculated;
double i;
double n;
//double fV;
//double pV1;
System.out.print("please enter value for n (years): ");
n = input.nextInt();
System.out.print("please enter interest rate: ");
i=input.nextDouble();
validated = validate(0,100000);
System.out.println("pV is validated and equal to: "+validated);
calculated= fvCalc(validated,i,n);
System.out.println("your calulation for interest is: "+calculated);
}
private static double validate(double upLimit, double lowLimit) {
double pV;
System.out.print("please enter pV");
pV=input.nextDouble();
while(pV<upLimit|| pV>lowLimit)
{
System.out.print("please enter value between "+upLimit+" and "+lowLimit);
System.out.println("please enter pV");
pV=input.nextDouble();
}
return pV;
}
private static double fvCalc(double pV, double i, double n)
{
double fV;
fV=pV*Math.pow(1+(i/100), n);
return fV;
}
}