0

这是我的成分类的代码:

public class Ingredient {

 /* Attribute declarations */
 private String name; //  name
 private int calorieCount; //calorie count

  /* Constructor */
 public Ingredient(String name, int calorieCount) {
  this.name = name;
  this.calorieCount = calorieCount;
 }

 public String getName(){
  return name;
 }

  public int getCalorieCount(){
  return calorieCount;
 }

  public static void main (String[] args)
  {
    Ingredient item1 = new Ingredient ("Butter", "100");
    System.out.println(item1);
  }
}

当我尝试运行它时,我得到一个编译器错误:

1 error found:
File: C:\eclipse\workspace\Assignment NEW1\Ingredient.java  [line: 28]
Error: C:\eclipse\workspace\Assignment NEW1\Ingredient.java:28: cannot find symbol
symbol  : constructor Ingredient(java.lang.String,java.lang.String)
location: class Ingredient

我究竟做错了什么?

4

3 回答 3

4

100在构造函数中作为字符串传递:-

Ingredient item1 = new Ingredient ("Butter", "100");

将其更改为: -

Ingredient item1 = new Ingredient ("Butter", 100);
于 2013-01-24T18:27:49.437 回答
1

您应该将第二个参数作为 int 传递。但是,您传递了字符串“100”,请将其更改为数字 100 而不是“100”。

于 2013-01-24T19:06:20.900 回答
0

每当您收到有关未找到某些符号的编译时错误时,请始终记住您在程序中使用了存在的东西,或者使用当前类路径无法找到它。

于 2013-01-24T18:34:47.853 回答