Bry*_*nte 1 .net c# loops list
我是新来的,我想帮一些List ...
实际上,我想将y我的每个元素添加List<x, y>到变量中.我知道,这可能很容易,但我被困在那一部分..
/// <summary>
/// Number of cards in the deck
/// </summary>
public byte NbTotalCards
{
get
{
byte nbCards = 0;
for (byte i = 0; i <= this.LstCardsWithQt.Count; i++)
{
if (this.LstCardsWithQt[i].Qt != 0)
{
if(this.LstCardsWithQt[i].Qt.Equals(2))
nbCards += 2;
else
{
nbCards += 1;
}
}
else
{
nbCards += 0;
}
}
return nbCardss;
}
}
Run Code Online (Sandbox Code Playgroud)
哪里
public List<DeckEntry> LstCardsWithQt
和
public DeckEntry(Card card, byte qt)
{
this.Card = carte;
this.Qt = qt;
}
Run Code Online (Sandbox Code Playgroud)
顺便说一下,我收到了错误 this.LstCardsWithQt[i].Qt != 0
ArgumentOutOfRangeExeption("索引超出范围.必须是非负数且小于集合的大小")
你以错误的方式循环你的收藏.代替
for (byte i = 0; i <= this.LstCardsWithQt.Count; i++)
Run Code Online (Sandbox Code Playgroud)
它一定要是
for (byte i = 0; i < this.LstCardsWithQt.Count; i++)
Run Code Online (Sandbox Code Playgroud)
(你也可以删除"this"限定符,这看起来像Java Code)
新方法:如果您只想总结card.Qt所有卡片的属性,您可以这样做
public int NbTotalCards
{
get
{
return LstCardsWithQt.Sum( card => card.Qt);
}
}
Run Code Online (Sandbox Code Playgroud)
(证明card.Qt只有0到2之间的值并且它仍然是相同的逻辑 - 我允许自己更改总和的类型int而不是a byte.using System.Linq如果你这样做,你还需要在文件的开头.)
| 归档时间: |
|
| 查看次数: |
124 次 |
| 最近记录: |