gpt4 book ai didi

java - 找不到构造函数符号

转载 作者:行者123 更新时间:2023-12-01 17:26:35 26 4
gpt4 key购买 nike

这是我的成分类的代码:

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

我做错了什么?

最佳答案

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

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

将其更改为:-

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

关于java - 找不到构造函数符号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14508173/

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