Java 为什么我';我无法计算销售金额和销售税金额?
您的发票号码是:1234。 你的销售额是:10.0。 你的销售税是:0.0。-----问题区域 ----jGRASP wedge2:进程的退出代码为0。Java 为什么我';我无法计算销售金额和销售税金额?,java,loops,methods,Java,Loops,Methods,您的发票号码是:1234。 你的销售额是:10.0。 你的销售税是:0.0。-----问题区域 ----jGRASP wedge2:进程的退出代码为0。 ----jGRASP:操作完成。您从未使用过setSalePrice方法,因此您的SalesTax参数从未被初始化。您可以这样初始化它:double-SalesTax=salePrice*0.05您从未调用setSalePrice,因此销售税从未设置 这里有一种方法可以纠正这一点,但实际上,您可能应该在调用displaySalePrice之前调
----jGRASP:操作完成。您从未使用过
setSalePrice
方法,因此您的SalesTax
参数从未被初始化。您可以这样初始化它:double-SalesTax=salePrice*0.05代码>您从未调用setSalePrice,因此销售税从未设置
这里有一种方法可以纠正这一点,但实际上,您可能应该在调用displaySalePrice之前调用setSalePrice,而不是在其内部
public class Purchase
{
int invoiceNumber = 1234;
double salePrice = 10.00;
double SalesTax;
public void setInvoiceNumber(int invoice)
{
invoiceNumber = invoice;
}
public void setSalePrice(double saleAmount)
{
salePrice = saleAmount;
SalesTax = (saleAmount * .05);//when I'm compiling it's not calculating
}
public void displaySalePrice()
{
System.out.println("Your invoice number is:" + invoiceNumber + ".");
System.out.println("Your sale amount is: " + salePrice + ".");
System.out.println("Your sales tax is: " + SalesTax + ".");
}
}
这会有用的
public void displaySalePrice()
{
setSalePrice(salePrice);
System.out.println("Your invoice number is:" + invoiceNumber + ".");
System.out.println("Your sale amount is: " + salePrice + ".");
System.out.println("Your sales tax is: " + SalesTax + ".");
}
注意这个类有一些风格上的问题
- “SalesTax”以大写字母开头,应该为类(和接口)名称保留。正确的拼写是“销售税”
- 它缺少构造函数
示例构造函数:
public class Purchase
{
int invoiceNumber = 1234;
double salePrice = 10.00;
double SalesTax = 0.0; // by default this is initialized to zero.
public void setInvoiceNumber(int invoice)
{
invoiceNumber = invoice;
}
public void setSalePrice(double saleAmount)
{
salePrice = saleAmount;
SalesTax = (saleAmount * .05);//when I'm compiling it's not calculating
}
public void displaySalePrice()
{
System.out.println("Your invoice number is:" + invoiceNumber + ".");
System.out.println("Your sale amount is: " + salePrice + ".");
System.out.println("Your sales tax is: " + SalesTax + ".");
}
public static void main(String args[])
{
setSalePrice(10.0); // sets SalesTax to (100.0 * .05)
displaySalePrice();
}
}
- 购买是一种一旦完成就不会改变的东西。它的数据成员是可变的(可更改),但它们应该是不变的(最终或常量)
- 该类有setter(设置/更改变量),但缺少getter(在不更改变量的情况下检索变量值)。通常,变量应尽可能声明为“私有”和“最终”。所以如果我写这个类,我会这样写:
修订示例:
final int invoiceNumber; // These are set in the Constructor.
final double salePrice; // Once they are set, they don't change.
final double salesTax;
你能补充一些信息吗?您在哪里调用setSalePrice(双倍销售额)函数?
final int invoiceNumber; // These are set in the Constructor.
final double salePrice; // Once they are set, they don't change.
final double salesTax;
public class Purchase
{
private final int invoiceNumber;
private final double salePrice;
private final double salesTax;
// Constructor
public Purchase(int invoiceN, double salesP) {
invoiceNum = invoiceN;
salesPrice = salesP;
salesTax = salesPrice * .05; // The Constructor can figure this out.
}
public int getInvoiceNumber()
{
return this.invoiceNumber; // "this." is optional
}
public double getSalePrice()
{
return this.salePrice();
}
public double getSalesTax()
{
return this.salesTax;
}
public void displaySalePrice()
{
System.out.println("Your invoice number is:" + getInvoiceNumber() + ".");
System.out.println("Your sale amount is: " + getSalePrice() + ".");
System.out.println("Your sales tax is: " + getSalesTax() + ".");
}
public static void main(String args[])
{
Purchase shoesPurchase = new Purchase(1234, 10.00);
shoesPurchase.displaySalePrice();
}
}