【发布时间】:2020-03-11 22:13:33
【问题描述】:
我正在编写一个程序来存储某些食谱信息。成一个数组。使用方法 RecipeEntry() 及其参数,该程序旨在将最多 3 个食谱存储到一个名为:totalEntry[] 的数组中。
下面是我编写这个程序的尝试,但是,我遇到了错误......无法弄清楚我错过了什么。
import java.util.*;
public class RecipeArray
{
private String author;
private Recipe recpH_1;
private Recipe recpC_2;
private static RecipeEntry[] totalEntry = new RecipeEntry[3];
private static int entryCount;
public RecipeArray(String author, Recipe hot, Recipe cold) // constructor
{
entryCount = 0;
this.author = author;
recpH_1 = hot;
recpC_2 = cold;
totalEntry[entryCount] = new RecipeArray(author, recpH_1, recpC_2);
}
public static void main(String[] args)
{
RecipeEntry("Mary Bush", SpaghettiHOT, SpaghettiCOLD);
// RecipeEntry method, when called should pass its parameter values into
// totalEntry [] array. entryCount variable should keep count of every entry.
System.out.println("ALL ENTRY = " + entryCount + Arrays.toString(totalEntry));
}
}
public class Recipe //create class data of type recipe
{
private String name;
private int id, rating;
public Recipe(String name)
{
this.name = name;
id = 0;
rating = 0;
}
}
预期的输出应打印条目列表 - 示例:
输出:
索引 0 - [Mary Bush, SpaghettiHOT{id=0, rating=0}, SpaghettiCOLD{id=0, rating=0}]
【问题讨论】:
-
也许阅读了那些错误信息。您当然不能
return来自构造函数的数组(或任何值),只是猜测其中一个。 -
@tevemadar 我已经删除了返回行....错误已确认,但是,调用该方法时,我仍然希望将参数中发送的值传递到并存储在数组中:totalEntry[ ] - 特别是在索引 0 处。
标签: java arrays variables methods