我知道我可能会为此得到很多反对票,但我在 c#
方面还不错,但还有一段时间。如果有人能帮助我,我真的很感激。我的应用程序主要包含在名为 dailyMenu
的类中。该类具有以下字段:
public class dailyMenu
{
private string day = "";
private int date = 0;
public string entree { get; private set; }
private double price;
private double calories;
static int initalDate = 1;
static string[] daysOfWeek = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
static string[] entrees = {"Beef Tenderloin Fresco", "Madagascar Filet Mignon", "Filet Mignon", " Lobster Ravioli", "Asian Infused Braised Beef", "New Age Chicken Cordon Bleu", "Short Ribs", " Beef Wellington",
"Fajitas", "Bacon Cheeseburger", " Beef Burgandy", "Spagehetti"};
static double[] entreePrices = { 5.99, 7.99, 6.99, 4.50, 9.99, 10.29, 5.67, 8.99, 3.99, 4.78, 10, 79, 6.98 };
static int[] entreeMealCaloricVal = { 999, 1288, 770, 699, 450, 999, 1500, 873, 911, 1011, 777, 500 };
这些是我的属性:
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 double Calories
{
get { return calories; }
set { calories = value; }
}
接下来,我必须在我的 main 方法中创建我的 dailymenu
类的对象。
public static void Main(string[] args)
{
dailyMenu[,] daysOfMonth = new dailyMenu[4, 5];
for (int column = 0; column < daysOfMonth.GetLength(0); column++)
{
for (int row = 0; row < daysOfMonth.GetLength(1); row++)
{
dailyMenu dm = new dailyMenu();
daysOfMonth[column, row] = dm;
Console.WriteLine(dm.ToString());
}
}
}
现在,我正在尝试想出一个功能,在用户输入上述主菜之一后,控制台将打印出所有供应输入主菜的日期。它还将二维数组作为参数。 这是我到目前为止的想法。
static void entreeSearch(dailyMenu[,] daysOfMonth)
{
Console.WriteLine("PLease enter the entree you'd like to search for today :)");
string response = Console.ReadLine();
response = response.ToUpper();
for (int column = 0; column < daysOfMonth.GetLength(0); column++)
{
for (int row = 0; row < daysOfMonth.GetLength(1); row++)
{
if (response ==)
{
dailyMenu dm = new dailyMenu();
daysOfMonth[column, row] = dm;
Console.WriteLine(dm.ToString());
}
}
}
最佳答案
那么你需要填写哪一天有什么主菜。目前您正在初始化为默认值,因此每一天的主菜都是 null
。但是一旦你解决了这个问题,这应该可以工作:
static void entreeSearch(dailyMenu [,] daysOfMonth)
{
Console.WriteLine ("PLease enter the entree you'd like to search for today :)");
string response = Console.ReadLine ();
response = response.ToUpper ();
for (int column = 0; column < daysOfMonth.GetLength(0); column++)
{
for (int row = 0; row < daysOfMonth.GetLength(1); row++)
{
dailyMenu dailyMenuAtThisRowAndColumn = daysOfMonth[column, row];
if (dailyMenuAtThisRowAndColumn.Entree.ToUpper() == response)
{
Console.WriteLine($"Entree {response} was found on day {daysOfMonth[column, row].Day}");
}
}
}
关于c# - 如何编写一种方法,根据用户输入打印出主菜供应天数列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36779808/