[英]Accesor method is not recieving var from mutator method
我在eclipse,java上制作基本计算器。 但是我对其中一种方法有疑问,因为它不接受正确的变量。
我知道问题出在calculateDifference()
和setCurrentValue()
方法中。
public class Dollar {
static int startingValue = 2650;
static int currentValue;
static int dollars;
static int differenceValue = calculateDifference();
static void setDollarQuantity (int dollarValue) {
dollars = dollarValue;
}
static void setCurrentValue(int currentDollar) {
currentValue = currentDollar;
}
static int calculateDifference() {
return ( currentValue - startingValue) * dollars;
}
public static void main(String[] args) {
setCurrentValue(2780);
setDollarQuantity(111);
calculateDifference();
}
}
来自calculateDifference
方法的预期结果为14,430,但实际值为0。我发现了一个问题,即calculateDifference
方法未将currentValue接受为2780,而是0。有人可以帮助我并修改我的代码吗?
更改
static int diffrenceValue = calculateDifference();
至
static int differenceValue;
和在main()
calculateDifference();
至
differenceValue = calculateDifference();
System.out.println(differenceValue);
这样,您将在其他变量使用正确的值初始化之后(而不是之前)设置differenceValue
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.