多个班级:我在这里做错了什么?

时间:2011-02-26 05:42:49

标签: java class

我已经正式走到了尽头。我找不到我做错了什么。我几乎完全像我几天前编写的另一个程序一样完成了这个程序,但是编译时遇到了问题。我不知道为什么我在输出线上出错。请帮忙:

这是运行文件:

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);
  }
}

2 个答案:

答案 0 :(得分:2)

它不编译的原因是因为你的主要代码:

  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()实现不存在。你没有正确的签名。把它改成一个合适的吸气剂,它应该工作。其他吸气剂也一样。

而不是:

  // 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;
  }

答案 1 :(得分:0)

我认为问题在于您阅读价值观的方式。虽然使用Strings读取Scanner.next()是安全的,但对于其他数据结构,您可能需要在实际读取值之前从输入流中清除换行符。

但这只是在理论上,因为你几乎没有给出任何关于你的问题的信息。