Java:返回用户输入

时间:2014-03-28 19:08:04

标签: java constructor

在收到" Just Tax。"时遇到问题。程序编译,不会计算产出的销售税 - 只需返回" 0。" "销售"但是,方法(不在列出的代码中)将确认并打印销售金额。不确定我做错了什么。谢谢!

  //...
  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);
}

}

2 个答案:

答案 0 :(得分:1)

setSale方法中的分配是向后的。目前,它会将sale中的任何内容分配给您的本地变量amount

更改

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

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

答案 1 :(得分:1)

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

这导致了这个问题。您目前正在做的是获取方法参数并为其分配sale变量。 (但我在代码中看不到它。)

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

这意味着在调用上述代码时,sale仍然没有任何价值,我猜这个销售会使用0进行实例化。