【发布时间】:2021-12-29 18:30:49
【问题描述】:
我正在尝试使用数据结构制作一个简单的送餐系统。我在 ArrayList 中保存了 Neighborhood 名称,并在 GenericList 中保存了 Delivery Count、Food Name 和它的计数。我画了原理图并附上了照片。
我编写了打印“Hood Name and it's delivery count”的程序,我的代码和输出在这里:
using System;
using System.Collections;
using System.Collections.Generic;
namespace temp
{
internal class delivery
{
public string food;
public int count;
}
internal class Hood
{
public string Name;
public int Number;
}
class programm
{
static void Main(string[] args)
{
string[] HoodName = { "Cherryhood", "NewCastle", "Greenlight", "Summerlin", "Westcity", "Paradise", "Legions", "Flamingos" };
int[] TeslimatSayisi = { 4, 2, 7, 2, 7, 3, 0, 1 };
ArrayList arrayList = new ArrayList();
int counter = 0;
List<Hood> genericList;
Hood ClassExample;
for (int i = 0; i < HoodName.Length;)
{
genericList = new List<Hood>();
int elementCount = (int)Math.Pow(2, counter);
for (int j = 0; j < elementCount; j++)
{
ClassExample = new Hood();
ClassExample.Name = HoodName[i];
ClassExample.Number = TeslimatSayisi[i];
genericList.Add(ClassExample);
i++;
if (i == HoodName.Length) break;
}
arrayList.Add(genericList);
counter++;
}
int counter2 = 0;
foreach (List<Hood> temp in arrayList)
{
foreach (Hood temp2 in temp)
Console.WriteLine("Hood: " + temp2.Name +" | "+" Delivery Count: " + temp2.Number);
}
我的输出是:
Hood: Cherryhood | Delivery Count: 4
Hood: NewCastle | Delivery Count: 2
Hood: Greenlight | Delivery Count: 7
Hood: Summerlin | Delivery Count: 2
Hood: Westcity | Delivery Count: 7
Hood: Paradise | Delivery Count: 3
Hood: Legions | Delivery Count: 0
Hood: Flamingos | Delivery Count: 1
我怎样才能得到这样的输出:
Hood: Cherryhood | Delivery Count: 4 | Food's, count: Salat:2, Taco:5, Pizza:1, Burger:2
Hood: NewCastle | Delivery Count: 2 | Food's, count: Pasta:15, Cake,7
Hood: Greenlight | Delivery Count: 7 | Food's, count: ................
Hood: Summerlin | Delivery Count: 2 | ..........
Hood: Westcity | Delivery Count: 7 |...........
Hood: Paradise | Delivery Count: 3 |.................
Hood: Legions | Delivery Count: 0 |...........
Hood: Flamingos | Delivery Count: 1 |.....................
我猜我必须像这样制作食物清单和计数清单:
foods = {pizza, taco, burger, salad, pasta, cake..........}
count = {1, 5, 2, 2, 15 ,7...........}
我需要创建配送类(包含膳食名称、数量字段)。然后我必须用相关邻域中的 Delivery 对象的数量填充 ArrayList 中的每个通用列表。我可以创建一个食物列表并从中随机选择食物信息。对于这个项目,我们可以假设每次交付只能包含一种餐食(包括多少)。
我是 C# 和数据结构的新手,非常感谢您的帮助。
【问题讨论】:
标签: c# for-loop arraylist data-structures generic-list