gpt4 book ai didi

java - 无法编译,我做错了什么?

转载 作者:行者123 更新时间:2023-12-02 08:21:22 25 4
gpt4 key购买 nike

好的,我可以看到第 33 行的“setUniqueType”和第 50 行的“ItemDetails”是需要更正的内容,但我无法弄清楚它出了什么问题以及如何修复它。请帮助我,以便我可以继续我的计划...

主要

package inventory4;
import java.util.Scanner;
import java.util.Arrays;
import javax.swing.JOptionPane;

public class RunApp
{
public static void main(String[] args)
{

Scanner input = new Scanner(System.in);

ItemDetails theItem = new ItemDetails();

int number;
String Name = "";
String Type = "";

String sNumber = JOptionPane.showInputDialog(null,"How many items are to be put into inventory count?: ");
number = Integer.parseInt(sNumber);

ItemDetails[] inv = new ItemDetails[number];

for (int count = 0; count < inv.length; ++count)
{
Name = JOptionPane.showInputDialog(null,"What is item " + (count + 1) + "'s name?");
theItem.setName(Name);


Type = JOptionPane.showInputDialog(null,"Enter " + Name + "'s product type");

theItem.setUniqueType(Type);


String spNumber = JOptionPane.showInputDialog(null,"Enter " + Name + "'s product number");
double pNumber = Double.parseDouble(spNumber);
theItem.setpNumber(pNumber);

String sUnits = JOptionPane.showInputDialog(null,"How many " + Name + "s are there in inventory?");

double Units = Double.parseDouble(sUnits);
theItem.setUnits(Units);

String sPrice = JOptionPane.showInputDialog(null,Name + "'s cost");
double Price = Double.parseDouble(sPrice);
theItem.setPrice(Price);


inv[count] = new ItemDetails(Name, Price, Units, pNumber, Type);
}



for (int i = 0; i < inv.length; ++i)
{
JOptionPane.showMessageDialog(null, "Product Name" + inv[i].getName());

JOptionPane.showMessageDialog(null, "Product Type" + inv[i].getUniqueType());

JOptionPane.showMessageDialog(null, "Product Number" + inv[i].getpNumber());

JOptionPane.showMessageDialog(null, "Amount of Units in Stock" + inv[i].getUnits());

JOptionPane.showMessageDialog(null, "Price per Unit" + inv[i].getPrice());

JOptionPane.showMessageDialog(null, String.format("Total cost for %s in stock: $%.2f", inv[i].getName(), inv[i].calculateTotalPrice()));

JOptionPane.showMessageDialog(null,String.format("Restocking fee for %s is $%.2f", inv[i].getName(), inv[i].calculateRestock()));

String combinedData = inv[i].toString();

if(i == (inv.length -1)){

String lastItem = String.format("\nTotal Cost for all items entered: $%.2f\n", Items.getCombinedCost(inv));

combinedData = combinedData + lastItem;
JOptionPane.showMessageDialog(null, combinedData);

}else{
JOptionPane.showMessageDialog(null, combinedData);
}


} //end for


} //end main


} //end class

项目

package inventory4;


public class Items implements Comparable
{
private String Name;
public double pNumber, Units, Price;
String allInfo;
public Items()
{
Name = "";
pNumber = 0.0;
Units = 0.0;
Price = 0.0;
}

public int compareTo(Object item)
{

Items tmp = (Items)item;

return this.getName().compareTo(tmp.getName());
} // end compareTo method

public Items(String productName, double productNumber, double unitsInStock, double unitPrice)
{
Name = productName;
pNumber = productNumber;
Units = unitsInStock;
Price = unitPrice;
}

public String toString()
{
StringBuffer allInfo = new StringBuffer();


allInfo.append(String.format("Name: %s\n pNumber: %.0f \n Units: %.0f \n Price: %.2f\n",
getName(),getpNumber(),getUnits(),getPrice()));

return allInfo.toString();
}


//setter methods
public void setName(String n)
{
Name = n;
}

public void setpNumber(double no)
{
pNumber = no;
}

public void setUnits(double u)
{
Units = u;
}

public void setPrice(double p)
{
Price = p;
}

//getter methods
public String getName()
{
return Name;
}

public double getpNumber()
{
return pNumber;
}

public double getUnits()
{
return Units;
}

public double getPrice()
{
return Price;
}

public double calculateTotalPrice()
{
return (Units * Price);
}

public static double getCombinedCost(Items[] item)
{
double combined = 0;

for (int i = 0; i < item.length; ++i)
{
combined = combined + item[i].calculateTotalPrice();
} //end loop

return combined;
} //end method

} //end class

商品详情

package inventory4;

public class ItemDetails extends Items
{
private String UniqueType;

public ItemDetails()
{
super();
}

public String enterUniqueType()
{
return UniqueType;
}

public String getUniqueType()
{
return UniqueType;
}

public double calculateRestock() //setting up to calculate the restocking fee at 5%
{
return (Price * .05);
}
}

最佳答案

编译错误:

RunApp.java:31: cannot find symbol
symbol : method setUniqueType(java.lang.String)
location: class ItemDetails
theItem.setUniqueType(Type);
^
RunApp.java:48: cannot find symbol
symbol : constructor ItemDetails(java.lang.String,double,double,double,java.lan
g.String)
location: class ItemDetails
inv[count] = new ItemDetails(Name, Price, Units, pNumber, Type);
^
2 errors
  1. 您的 ItemDetails 类没有 setUniqueType 方法。您应该添加一个。
  2. 您在 :48 使用的构造函数是“String, double, double, double, String”,但您的构造函数实际上只是“String double double double”删除 Type 或添加 String参数添加到 Item 构造函数的末尾,或者使用五个参数创建一个 ItemDEtails 构造函数。
  3. 您的 ItemDetails 没有与该参数匹配的构造函数。如果您想链接构造函数,请在 ItemDetails 中声明它

链接构造函数:

public ItemDetails(String productName, double productNumber, double unitsInStock, double unitPrice
super(productName,productNumber,unitsInStock,unitPrice);
}

关于java - 无法编译,我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5400640/

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