【发布时间】:2016-08-21 22:04:36
【问题描述】:
到目前为止,这是我的代码;我的主要问题在于主要方法。
namespace Lab
{
class dailyMenu
{
public static int r;
public string day;
public int date;
public string entree;
public double price;
public int calories;
public static int assignDate = 1;
public string Day
{
get { return day; }
set { day = value; }
}
public int Date
{
get { return date; }
set { date = value; }
}
public string Entree
{
get { return entree; }
set { entree = value; }
}
public double Price
{
get { return price; }
set { price = value; }
}
public int Calories
{
get { return calories; }
set { calories = value; }
}
private static string[] DayArray = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday" , "Saturday" , "Sunday" };
private static string[] EntreeArray = { "Pizza", "Spaghetti", "Cheeseburger", "Salad", "Soup", "Sandwich", "Pork", "Roast Chicken", "Kebab", "Steak", "Tacos", "Risotto" };
private static double[] PriceArray = { 2.50, 4.00, 1.50, 1.50, 1.50, 3.00, 3.50, 3.00, 2.50, 4.00, 3.00, 2.00 };
private static int[] CalorieArray = { 300, 600, 650, 270, 190, 680, 250, 300, 180, 250, 350, 600 };
public static void DayofMonth(int date)
{
date = assignDate;
assignDate++;
if (date == 5 || date == 12 || date == 19 || date == 26)
{
assignDate += 2;
}//end if
}// end DateofMonth
public static void DayofWeek (int day)
{
day = (day % 7) - 1;
}// end DayofWeek
public void randomItem()
{
Random rnd = new Random();
r = rnd.Next(0, 12);
this.entree = EntreeArray[r];
this.calories = CalorieArray[r];
this.price= PriceArray[r];
}// end randomItem
public dailyMenu()
{
randomItem();
}
static void Main(string[] args)
{
// Populates the 2D array
dailyMenu [,] day = new dailyMenu [4, 5];
for (int row = 0; row < 4; row ++)
{
for (int column = 0; column < 5; column++)
{
day[row, column] = new dailyMenu();
for (int i; r < Length.day; i++)
{
Console.WriteLine(r);
}
}//end forloop column
}// end forloop row
}//end Main
}//end dailyMenu
}//end namespace
我正在尝试使用 Main 中的 for 循环打印出包含三个数组的 DailyMenu 的新实例,但是我收到的错误消息是“名称 Length 在当前上下文中不存在。”
有什么帮助吗?谢谢。
【问题讨论】:
-
很清楚...你还没有在任何地方定义一个名为
Length的变量。 -
我认为你的意思是
day.Length而不是Length.day,但你也没有提炼r。你的意思是i?最后,二维数组的Length将为您提供整个数组的大小,而不仅仅是一维。 -
只是因为我感兴趣:您认为该代码会打印什么?你确定你需要一个主类的多维数组吗?关于您的代码有很多问题 :-) 欢迎来到 StackOverflow,我想您很快就会有新问题。
-
@ThorstenDittmar - 我已经用 dailyMenu 填充了 day 数组,它应该从卡路里数组、主菜数组和价格数组中打印出来,因为它们都是并行数组。自从提出问题以来,我已将“Console.WriteLine(r)”更改为“Console.WriteLine(EntreArray [r] + " " + CalorieArray [r] + " " + PriceArray [r])。哪个确实有效,但我得到了“天”长度的每个数组项的打印,例如“披萨 2.50 300” 20 次。
标签: c# for-loop multidimensional-array