gpt4 book ai didi

java - 如何在 java 中编写带组的数组?

转载 作者:行者123 更新时间:2023-11-29 05:35:07 25 4
gpt4 key购买 nike

新人在这里,

如何在数组中创建组?

receipt[] re11 = {["Book", 12.49, 1, false, true],["Music CD", 14.99, 1, false, false]};

我知道在 javascript 中你可以像那样使用方括号,但我就是不记得在 java 中该怎么做。

编辑:这是我剩下的类(class)。抱歉,这可能会有更多帮助:

public class receipt {

private double price;
private Integer quantity;
private String itemName;
private boolean imported;
private boolean taxExemption;

private double basicTaxRate = .10;
private double importDuty = .05;
private double grandTotal;

public receipt(){

}

public receipt(String newItemName, double newPrice, Integer newQuantity, boolean newImported, boolean newTaxExemption){
itemName = newItemName;
price = newPrice;
quantity = newQuantity;
imported = newImported;
taxExemption = newTaxExemption;
}

//Accessors
public double getPrice(){
return price;
}

public Integer getQuantity(){
return quantity;
}

public String getItemName(){
return itemName;
}

public boolean getImported(){
return imported;
}

public boolean getTaxExemption(){
return taxExemption;
}


//mutators
public void setPrice(double newPrice){
//JOptionPane.showInputDialog("What is the price of the item?");
this.price = newPrice;
}

public void setQuantity(Integer newQuantity){
this.quantity = newQuantity;
}

public void setItemName(String newItemName){
this.itemName = newItemName;
}

public void setImported(boolean newImported){
this.imported = newImported;
}

public void setTaxExemption(boolean newTaxExemption){
this.taxExemption = newTaxExemption;
}

public double computeTax(){
if(imported == true){
return price*(1+(basicTaxRate+importDuty));
}
else{
return price*(1+basicTaxRate);
}
}

public double computeTotal(){
if (taxExemption = false){
return computeTax()*quantity;
}
else
return price*quantity;

}

public void computeGrandTotal(){
grandTotal =+ computeTotal();
//return grandTotal;
}

最佳答案

你是这个意思吗?

Receipt[] re11 = {new Receipt("Book", 12.49, 1, false, true),new Receipt("Music CD", 14.99, 1, false, false)};

关于java - 如何在 java 中编写带组的数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19782521/

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