作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经正式走到了穷途末路的地步。我找不到我做错了什么。我完成的这个程序几乎与我几天前编写的另一个程序一模一样,但我在编译时遇到了问题。我不知道为什么输出线上出现错误。请帮忙:
这是正在运行的文件:
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/
我是一名优秀的程序员,十分优秀!