gpt4 book ai didi

java - 多个类: What am I doing wrong here?

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

我已经正式走到了穷途末路的地步。我找不到我做错了什么。我完成的这个程序几乎与我几天前编写的另一个程序一模一样,但我在编译时遇到了问题。我不知道为什么输出线上出现错误。请帮忙:

这是正在运行的文件:

package inventory1;

import java.util.Scanner;

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

Scanner input = new Scanner(System.in);

DataCollection theProduct = new DataCollection();

String Name = "";
double pNumber = 0.0;
double Units = 0.0;
double Price = 0.0;

while (true) {
System.out.print("Enter Product Name: ");
Name = input.next();
theProduct.setName(Name);
if (Name.equalsIgnoreCase("stop")) {
return;}
System.out.print("Enter Product Number: ");
pNumber = input.nextDouble();
theProduct.setpNumber(pNumber);

System.out.print("Enter How Many Units in Stock: ");
Units = input.nextDouble();
theProduct.setUnits(Units);

System.out.print("Enter Price Per Unit: ");
Price = input.nextDouble();
theProduct.setPrice(Price);

System.out.print("\n Product Name: " + theProduct.getName());
System.out.print("\n Product Number: " + theProduct.getpNumber());
System.out.print("\n Amount of Units in Stock: " + theProduct.getUnits());
System.out.print("\n Price per Unit: " + theProduct.getPrice() + "\n\n");
System.out.printf("\n Total cost for %s in stock: $%.2f\n\n\n", theProduct.getName(), theProduct.calculatePrice());

}
}
}

这是数据收集文件:

package inventory1;

public class DataCollection {
String productName;
double productNumber, unitsInStock, unitPrice, totalPrice;

public DataCollection() {
productName = "";
productNumber = 0.0;
unitsInStock = 0.0;
unitPrice = 0.0;
}

// setter methods
public void setName(String name) {
productName = name;
}

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

public void setUnits(double units) {
unitsInStock = units;
}

public void setPrice(double price) {
unitPrice = price;
}

// getter methods
public void getName(String name) {
productName = name;
}

public void getpNumber(double pNumber) {
productNumber = pNumber;
}

public void getUnits(double units) {
unitsInStock = units;
}

public void getPrice(double price) {
unitPrice = price;
}

public double calculatePrice() {
return (unitsInStock * unitPrice);
}
}

最佳答案

无法编译的原因是你的主要代码:

  System.out.print("\n Product Name:     " + theProduct.getName());
System.out.print("\n Product Number: " + theProduct.getpNumber());
System.out.print("\n Amount of Units in Stock: " + theProduct.getUnits());
System.out.print("\n Price per Unit: " + theProduct.getPrice() + "\n\n");
System.out.printf("\n Total cost for %s in stock: $%.2f\n\n\n", theProduct.getName(), theProduct.calculatePrice());

需要 getName() 但您的 getName() 实现不存在。您没有正确的签名。将其更改为适当的 setter/getter ,它应该可以工作。其他 setter/getter 也是如此。

而不是:

  // getter methods
public void getName(String name) {
productName = name;
}

public void getpNumber(double pNumber) {
productNumber = pNumber;
}

public void getUnits(double units) {
unitsInStock = units;
}

public void getPrice(double price) {
unitPrice = price;
}

用途:

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

public double getpNumber() {
return productNumber;
}

public double getUnits() {
return unitsInStock;
}

public double getPrice() {
return unitPrice;
}

关于java - 多个类: What am I doing wrong here?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5125312/

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