gpt4 book ai didi

java - 设置值并调用 get 方法后程序返回 0?

转载 作者:行者123 更新时间:2023-12-01 17:16:38 25 4
gpt4 key购买 nike

我正在开发一个java项目,据我所知,我已经完成了所有工作,但是当我运行该程序时,应用程序上的第一组编码执行了它应该执行的操作,但是更新后的金额给我 0 的价格和金额。第二个对象的编码也是如此,原始发票信息工作正常,除了以下事实:对于更新后的代码,当它们应该为零时,它会给出负值,除此之外,它工作正常。对于更新的发票金额,它给我的价格和最终金额为 0。我看不到任何错误,它全部编译并运行。这是类代码。

public class Invoice
{
private String partNumber;
private String partDescription;
private int quantity;
private double price;

public Invoice (String part,String description, int quan, double value)
{
partNumber = part;
partDescription = description;

if(quan > 0);
quantity = quan;

if(price > 0.0);
price = value;

}
public void setpartNumber(String part)
{
partNumber = part;
}
public String getpartNumber()
{
return partNumber;
}
public void setpartDescription(String description)
{
partDescription = description;
}
public String getpartDescription()
{
return partDescription;
}
public void setquantity(int quan)
{
if(quan > 0);
quantity = quan;
if(quan <= 0);
quan = 0;
}

public int getquantity()
{

return quantity;
}
public void setprice(double value)
{
if(value > 0.0);
price = value;

if(value <= 0.0);
price = 0.0;

}
public double getprice()
{
return price;
}
public double getInvoiceAmount()
{
return (getquantity() * getprice());

}

}

这是应用程序代码。

public class InvoiceTest
{
public static void main (String args[])
{
Invoice invoiceA = new Invoice("1234", "Hammer",2,14.95 );


System.out.println("Original Invoice Information");
System.out.printf("Part Number: %s\n", invoiceA.getpartNumber());
System.out.printf("Description: %s\n",invoiceA.getpartDescription());
System.out.printf("Quantity: %d\n", invoiceA.getquantity());
System.out.printf("Price: %.2f\n", invoiceA.getprice());
System.out.printf("Invoice Amount: %.2f\n",invoiceA.getInvoiceAmount());


invoiceA.setpartNumber("001234");
invoiceA.setpartDescription("Yellow Hammer");
invoiceA.setquantity(3);
invoiceA.setprice(19.49);
System.out.println();

System.out.println("Updated Invoice Information");
System.out.printf("Part Number: %s\n", invoiceA.getpartNumber());
System.out.printf("Description: %s\n",invoiceA.getpartDescription());
System.out.printf("Quantity: %d\n",invoiceA.getquantity());
System.out.printf("Price: %.2f\n",invoiceA.getprice());
System.out.printf("Invoice Amount: %.2f\n",invoiceA.getInvoiceAmount());

Invoice invoiceB = new Invoice("5678","Paint Brush",-5,-9.99);


System.out.println("\nOriginal Invoice Information");
System.out.printf("Part Number: %s\n", invoiceB.getpartNumber());
System.out.printf("Description: %s\n",invoiceB.getpartDescription());
System.out.printf("Quantity: %d\n", invoiceB.getquantity());
System.out.printf("Price: %.2f\n", invoiceB.getprice());
System.out.printf("Invoice Amount: %.2f\n",invoiceB.getInvoiceAmount());


invoiceB.setquantity(3);
invoiceB.setprice(9.49);
System.out.println();

System.out.println("Updated Invoice Information");
System.out.printf("Part Number: %s\n", invoiceB.getpartNumber());
System.out.printf("Description: %s\n",invoiceB.getpartDescription());
System.out.printf("Quantity: %d\n",invoiceB.getquantity());
System.out.printf("Price: %.2f\n",invoiceB.getprice());
System.out.printf("Invoice Amount: %.2f\n",invoiceB.getInvoiceAmount());


}
}

最佳答案

整个代码中的 if 条件后面有额外的分号。真正导致您的错误的那个:

public void setprice(double value)
{
if(value > 0.0);
price = value;

if(value <= 0.0);
price = 0.0;

}

分号充当 if 的主体,因此后续语句始终会执行,并且 price 始终设置为 0。删除分号:

public void setprice(double value)
{
if(value > 0.0)
price = value;

if(value <= 0.0)
price = 0.0;

}

以类似方式更改代码中的其他 if

关于java - 设置值并调用 get 方法后程序返回 0?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21639110/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com