gpt4 book ai didi

c# - 列表不会显示其内容

转载 作者:太空宇宙 更新时间:2023-11-03 18:05:50 24 4
gpt4 key购买 nike

我想制作一个显示当前开店的应用程序。每个商店都有一个名称,工作时间和类别。我做了一个包含每个商店及其属性的列表,但是当我使用foreach方法时,它不会显示其中的任何一个...您是否偶然知道问题出在哪里?或其他解决方法?

 class Shop
{
public string name;
public DayOfWeek day{ get; set; }
public TimeSpan start{ get; set; }
public TimeSpan end{ get; set; }
public string address{ get; set; }
public string category{ get; set; }




public Shop(string name, DayOfWeek day, TimeSpan start, TimeSpan end, string address, string category)
{
name = this.name;
day = this.day;
start = this.start;
end = this.end;
address = this.address;
category = this.category;
}
}


public openshops()
{
List<Shop> openShop = new List<Shop>();
openShop.Add(new Shop("ill forno", DayOfWeek.Tuesday, new TimeSpan(18, 0, 0), new TimeSpan(00, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("Η ΣΥΡΑ", DayOfWeek.Sunday, new TimeSpan(12, 0, 0), new TimeSpan(00, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("ΠΙΤΑΚΙ", DayOfWeek.Tuesday, new TimeSpan(19, 30, 0), new TimeSpan(00, 30, 0), "ermoupoli", "souvlaki"));

this.InitializeComponent();


DayOfWeek today = DateTime.Today.DayOfWeek;
TimeSpan now = DateTime.Now.TimeOfDay;
if (today == DayOfWeek.Friday)
{
foreach (Shop shop in openShop)
{
if ((now > shop.start) && (now < shop.end))
{
Button btn = new Button();
btn.Content = shop.name;
myStackPanel.Children.Add(btn);
}

}
}
}

最佳答案

除了确定如何设置属性外,开始时间和结束时间也有问题。

openShop.Add(new Shop("ill forno", DayOfWeek.Tuesday, new TimeSpan(18, 0, 0), new TimeSpan(00, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("Η ΣΥΡΑ", DayOfWeek.Sunday, new TimeSpan(12, 0, 0), new TimeSpan(00, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("ΠΙΤΑΚΙ", DayOfWeek.Tuesday, new TimeSpan(19, 30, 0), new TimeSpan(00, 30, 0), "ermoupoli", "souvlaki"));


这些 Shop对象的开始时间将大于结束时间,因此此 if语句

if ((now > shop.start) && (now < shop.end))


这些 Shop对象始终为false。我猜您实际上是想交换这些值。

openShop.Add(new Shop("ill forno", DayOfWeek.Tuesday, new TimeSpan(00, 0, 0), new TimeSpan(18, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("Η ΣΥΡΑ", DayOfWeek.Sunday, new TimeSpan(00, 0, 0), new TimeSpan(12, 0, 0), "ermoupoli", "pizza"));
openShop.Add(new Shop("ΠΙΤΑΚΙ", DayOfWeek.Tuesday, new TimeSpan(00, 30, 0), new TimeSpan(19, 30, 0), "ermoupoli", "souvlaki"));

关于c# - 列表不会显示其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30402841/

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