0

我正在开发一个 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());


}
}
4

4 回答 4

5

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代码中的其他 s。

于 2014-02-07T22:44:31.263 回答
0

在您的setQuantity方法中,您正在设置quanarg 是否为 <= 0 的值。您应该设置quantity.

public void setquantity(int quan)
{
    if(quan > 0)
        quantity = quan;
    if(quan <= 0)
        quan = 0;            // should be quantity
}

编辑:并且,正如 user1066946 和 rgettman 所指出的,您需要从if语句中删除分号。

于 2014-02-07T22:44:16.853 回答
0

改变

if(price > 0.0){
    price = value;

}

if(value> 0.0) {
    price = value;

}

编辑:并从 if 语句中删除分号。

于 2014-02-07T22:47:35.663 回答
0

在你的构造函数中,if(price > 0.0); price = value;应该说if (value > 0.0) { price = value }

您的条件后面有一个错误的分号。在 if 语句中使用大括号也是一种好习惯——它可以更清楚地说明当条件为真时将执行哪些代码。

(顺便说一句,您还试图发明两个词来表示同一件事。相反,price在方法签名中用作名称,并this.price = price;在构造函数中说。 this.price指的是在类中定义的那个。)

于 2014-02-07T22:47:38.720 回答