//Note that you declare this method static: as such it will have no access to object
//member fields, only to class fields: these are member fields declared 'static'
public static void calculatePrice(){
Scanner userInput = new Scanner(System.in);//Note this has little to do with actually calculating price.
int orderNumber;
double totalBill = 0;//You a declaring a new variable that will disappear once it goes out of the scope of this method
orderNumber = userInput.nextInt();
switch(orderNumber){
case 1:
totalBill = totalBill + American;
displayMenu();
calculatePrice();//Recursive call!
break;
//... eliding for brevity
case 0:
System.out.println("Your total bill will be $"+ totalBill);
break;
}
} //this is the point where all method variables go out of scope, because this 'stack frame' is unloaded. Any subsequent call to this method will not 'remember' any state that was not saved elsewhere.
因此,請注意您正在進行遞歸調用。這意味着你的重新輸入你的功能。你的'totalBill'變量被新實例化,掩蓋了其他所有'totalBill'變量。這些變量仍然存在......只是在較低的堆棧幀上無法訪問。把棧幀看作一個新的表格,在這個表格上運行你的函數。當你調用一個新的函數時,一個新的表格被放置在頂部......但你只能看到頂部的表格。所有其他表都在它下面。
通過聲明變量static,這意味着您將其設置在一邊,以便此類型的所有對象中的所有函數都可以訪問相同的內存空間。 static
變量可用於該類的所有對象;因此它們通常被稱爲「類字段」,而非靜態成員變量則被稱爲「對象字段」。你的類簽名應,在一個精心設計的世界是這樣的:
public class RegisterOrder {
public double totalBill;//Available to only ONE instance (object) of this class.
public void calculatePrice() {//note no 'static' modifier!
//some code
}
}
這可能不會與您目前正在呼喚你calculatePrice功能的工作方式,因爲當前的方法是static
。你必須要改變,要:
RegisterOrder order = new RegisterOrder();
order.calculatePrice();
注意,那就是,你的遞歸調用意味着,如果你爲了很多很多的訂單程序將會崩潰。把它看成堆疊太高的表。在現代系統中,這是很難做到的(大量的內存),但良好的設計要求這裏的循環:
public function getOrder() {
//instantiate variables
orderNumber = userInput.nextInt();
while (orderNumber != 0) {
switch(orderNumber){
case 1:
totalBill = totalBill + American;
break;
case 2:
totalBill = totalBill + Expresso;
break;
case 3:
totalBill = totalBill + Latte;
break;
default:
//Error handling is important!
break;
}
displayMenu();
orderNumber = userInput.nextInt();
}
System.out.println("Your total bill will be $"+ totalBill);
}
注意,在這個版本中,你不能再呼叫您的輸入功能。還要注意,在此函數中聲明的'totalBill'變量不會被另一個對此函數的調用卸載或屏蔽。在真正乾淨的代碼中,你可以將你的輸入獲取方法與賬單計算方法分開,但是步驟很簡單。 :)
這是關於範圍。 –
對,我需要回到那一章。我認爲只要它在同一個街區就可以。不過謝謝!我至少可以回頭重讀:D – Gibbo