-2
我在論壇上看到過幾篇文章,但至今沒有明確的答案。我想檢查一個年齡列表。我想確保年齡範圍不重疊。關鍵是開始和結束年齡可以是相同的數字,這不應該導致發現重疊。整數範圍重疊驗證
任何幫助將是巨大的
示例代碼
namespace RangeValidation
{
public class RangeValidate
{
private readonly IList<Age> _listOfRanges = new List<Age>();
private readonly IList<Age> _secondListOfRanges = new List<Age>();
public void Validate()
{
_listOfRanges.Add(new Age { BeginingAge = 20, EndingAge = 20 });
_listOfRanges.Add(new Age { BeginingAge = 21, EndingAge = 30 });
_listOfRanges.Add(new Age { BeginingAge = 31, EndingAge = 60 });
_secondListOfRanges.Add(new Age { BeginingAge = 20, EndingAge = 20 });
_secondListOfRanges.Add(new Age { BeginingAge = 20, EndingAge = 30 });
_secondListOfRanges.Add(new Age { BeginingAge = 31, EndingAge = 60 });
Debug.Write(Overlaps(_listOfRanges).ToString()); // NO OVERLAPS
Debug.Write(Overlaps(_secondListOfRanges).ToString()); // Has overlaps
}
private static bool Overlaps(IEnumerable<Age> listOfRanges)
{
return true; // needs implementation here
}
}
public class Age
{
public int BeginingAge
{
get;
set;
}
public int EndingAge
{
get;
set;
}
}
}
在你對應該重疊什麼代碼中的註釋似乎是在與你的開始和結束的年齡文本賠率被允許是相同的... – Paddy
雖然你有代碼顯示你的設置/要求,你還沒有試圖編寫代碼。請嘗試一下,然後告訴我們您遇到問題的位置。 – Harrison