2014-03-28 100 views
0

收到「Just Tax」問題。程序編譯時,不會計算銷售稅 - 僅返回「0」。然而,「銷售」方法(不在列出的代碼中)將確認並打印出售金額。不知道我做錯了什麼。謝謝!Java:返回用戶輸入

//... 
    Purchase saleAmount = new Purchase(); 

    //... 

    System.out.println("Please enter a positive (+) number for the sale amount: $"); 
    double saleAmt = input.nextDouble(); 
    saleAmount.setSale(saleAmt); 

    //... 

    System.out.println("The 5% tax applied is: " + saleAmount.getJustTax()); 

public class Purchase 
//all data types have been declared 
final double SALES_TAX = 0.05; 
{ 
    //Initial Sale Cost 
    public double getSale() 
{ 
    return sale; 
} 

    public void setSale(double amount) 
{ 
    amount = sale; 
} 

//Get Just Sales Tax 
    public double getJustTax() 
{ 
    return justTax; 
} 
    public void setJustTax(double sale) 
{ 
    justTax = (sale * SALES_TAX); 
} 

}

+0

你調用了'setJustTax()'來計算'justTax'嗎? – nikis

回答

1

分配在setSale方法是倒退。目前,它會將sale中的內容分配給您的本地變量amount

變化

public void setSale(double amount) 
{ 
    amount = sale; 
} 

public void setSale(double amount) 
{ 
    sale = amount; 
} 
+0

謝謝,rgettman – ewbrowning

1
public void setSale(double amount) 
{ 
    amount = sale; 
} 

這是導致該問題。你現在正在做的是採取方法參數併爲其分配sale變量。 (但是我在代碼中看不到它)。

public void setJustTax(double sale) 
{ 
    justTax = (sale * SALES_TAX); 
} 

這意味着,當上面的代碼被調用時,sale還是沒有價值吧,我會猜銷售進行實例化0