[英]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.