我的代碼如下所示:爲什麼我在if語句中定義double時遇到問題?
if (gasQuality==87){
double subtotal = unleaded * gallonsSold;
}else if (gasQuality==89){
double subtotal = unleadedPlus * gallonsSold;
}else if (gasQuality==91){
double subtotal = premium * gallonsSold;
}
但由於某些原因,編譯器將無法識別「小計」以後。例如,如果我想稅更遠適用於小計下來的代碼,編譯器讀取:
cannot find symbol
symbol : variable subtotal
location: class Paniagua_Invoice
final double cityTax = .0375 * subtotal;
我在做什麼錯?
變量的範圍,閱讀它。 –
我剛剛做了,它是有道理的。我是新人,並不擅長編程。那麼需要做些什麼呢? – a12b23c34
在if塊之前初始化外部的小計變量,無論塊內部的初始化是否保留在塊的範圍內,並且不能在塊外部訪問 – bjhaid