【问题标题】:Having trouble with compiling error [closed]编译错误时遇到问题[关闭]
【发布时间】:2017-11-29 03:24:44
【问题描述】:

我是 Java 的初学者,我正在做一个课程项目。我还发现 Java 很难掌握,而且我遇到了这个编译错误的问题。这只是 1 个错误,让我更加困惑,因为当我在其他地方使用该变量时我没有得到一个。


* 错误 *

文件:D:\Programming\Project 2\Product.java [行:47]

错误:newCompType 无法解析为变量

public class Product
{
  //Instance variables
  private String name; //Name of Product
  private String compType; //Type of Product
  private double price; //Price of Product
  private int quantity; //How many computers

  //Constructors
  public Product()
  {
    name = "Dell";
    compType = "Laptop";
    price = 200;
    quantity = 1;
  }

  public Product(String newName, String newCompType, double newPrice, int newQuantity)
  {
    name = newName;
    compType = newCompType;
    price = newPrice;
    quantity = newQuantity;
  }

  //Instance methods
  public void printData()
  {
    System.out.println("Name of computer: " + name);
    System.out.println("Type of computer: " + compType);
    System.out.println("Number of computers: " + quantity);
    System.out.println("Cost of computer: " + price);
  }

  public void setName(String newName)
  {
    name = newName;
  }

  public String getName()
  {
    return name;
  }

  public void setCompType(String setCompType)
  {
    compType = newCompType;
  }

  public String getCompType()
  {
    return compType;
  }

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

  public double getPrice()
  {
    return price;
  }
  public void setQuantity(int newQuantity)
  {
    quantity = newQuantity;
  }

  public int getQuantity()
  {
    return quantity;
  }

   public String toString(){
   return name + " " + compType + " " + quantity + " " + price + "\n";
 }
}

【问题讨论】:

  • 因此请阅读指定行的代码,看看是否无法发现问题。很清楚——差点跳出来打你的鼻子。

标签: java compiler-errors


【解决方案1】:

在:

public void setCompType(String setCompType) {
    compType = newCompType; // <-- should be setCompType instead of newCompType
}

【讨论】:

  • 他们都应该是newCompType
  • @shmosel 你懂我的意思 :)
  • @alfasin 该死的。现在我觉得自己很笨哈哈。这个项目将是我的结束 x_x 谢谢你的帮助。
  • @Xiraiya 看到 shmosel 的评论。变量名不应该是setCompType - 名称newCompType 更好!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多