these lines seem to be your problem:
if (t==reference)
{
while (t == difminus || t == difplus)
{
what you are saying there is to display your debug message if the number is exactly equal to 'reference' AND is equal to (diffminus OR difplus) .. this can never be true if factor != 0
also, im not sure the purpose of making the second condition a while loop. if you did manage to hit that peiece of code and have it evaluate to true you would be in an infinite loop as the condition does not get modified
I think what you are after is:
List<double> filtered = new List<double>();
double difminus = reference - factor;
double difplus = reference + factor;
foreach (var t in unfilterednumbers)
{
if (t >= difminus && t <= difplus)
{
filtered.Add(t);
}
}
string s = "";
foreach (var st in filtered)
{
s += st.ToString() + "\n";
}
MessageBox.Show(s);
Some further tips:
- Once you have the logic sorted, take a look at LINQ, it may well enable you to simplify things further (i didnt use LINQ in this solution as, if you arent familiar with it, it can be a little confusing)
- if you are adding strings together as you are here, a stringbuilder is more efficient