在数组中搜索特定条件并显示

时间:2014-12-09 04:33:08

标签: c# arrays nested-if

您好我是C#编程的新手,我的代码卡住了。我的计划是向用户询问城市或邮政编码,他们想要的床位/浴室数量以及价格范围。我需要搜索我的阵列,然后显示符合他们标准的所有房屋(想想Zillow,网站)。我的代码目前显示不符合我为家庭选择的任何标准的随机房屋。帮助!

for (int a = 0; a < HOUSES; ++a)
{
    if (zipChecker == zip[a]) // check zip code
    {
        found = true;
        foundPosition = a;
    }

    if (BtnBath1.Checked) // check baths
    {
        if (bath[a] > 0 && bath[a] <= 1)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBath2.Checked) // check baths
    {
        if (bath[a] > 1 && bath[a] <= 2)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBath3.Checked) // check baths
    {
        if (bath[a] > 2 && bath[a] <= 3)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBath4.Checked) // check baths
    {
        if (bath[a] > 3)
        {
            found = true;
            foundPosition = a;
        }
    }

    if (BtnBed1.Checked) // check bed
    {
        if (bed[a] > 0 && bed[a] <= 1)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBed2.Checked) //check bed
    {
        if (bed[a] > 1 && bed[a] <= 2)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBed3.Checked) //check bed
    {
        if (bed[a] > 2 || bed[a] <= 3)
        {
            found = true;
            foundPosition = a;
        }
    }
    else if (BtnBed4.Checked) //check bed
    {
        if (bed[a] > 3)
        {
            found = true;
            foundPosition = a;
        }
    }

    if (BoxPrice1.Checked) //check price
    {
        if (price[a] < 200000 && price[a] > 300000)
        {
            found = false;
            foundPosition = a;
        }
    }
    if (BoxPrice2.Checked) //check price
    {
        if (price[a] < 300000 && price[a] > 400000)
        {
            found = false;
            foundPosition = a;
        }
    }
    if (BoxPrice3.Checked) //check price
    {
        if (price[a] < 400000 && price[a] > 500000)
        {
            found = false;
            foundPosition = a;
        }
    }
    if (BoxPrice4.Checked) //check price
    {
        if (price[a] < 500000)
        {
            found = false;
            foundPosition = a;
        }
    }
}

if (found)
{
    label1.Text +=
        string.Format("Bed: {0}, Bath:{1}, Asking Price:{2}, City:{3}, SQFT:{4}, " +
            "Zip Code:{5}, Year:{6}", bed[foundPosition], bath[foundPosition], 
            price[foundPosition].ToString("c2"), city[foundPosition], 
            sqft[foundPosition].ToString("n0"), zip[foundPosition], 
            year[foundPosition]);
}
else 
{
    label1.Text = ("Sorry there were no houses that met your criteria");
}

1 个答案:

答案 0 :(得分:0)

int printcount = 0;
for (int a = 0; a < HOUSES; ++a) {
    if (zipChecker == zip[a]) // check zip code
    {
        found = true;
        foundPosition = a;
    } else break;
    if (BtnBath1.Checked) // check baths
    {
        if (bath[a] > 0 && bath[a] <= 1) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBath2.Checked) // check baths
    {
        if (bath[a] > 1 && bath[a] <= 2) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBath3.Checked) // check baths
    {
        if (bath[a] > 2 && bath[a] <= 3) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBath4.Checked) // check baths
    {
        if (bath[a] > 3) {
            found = true;
            foundPosition = a;
        } else break;
    }

    if (BtnBed1.Checked) // check bed
    {
        if (bed[a] > 0 && bed[a] <= 1) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBed2.Checked) //check bed
    {
        if (bed[a] > 1 && bed[a] <= 2) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBed3.Checked) //check bed
    {
        if (bed[a] > 2 || bed[a] <= 3) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BtnBed4.Checked) //check bed
    {
        if (bed[a] > 3) {
            found = true;
            foundPosition = a;
        } else break;
    }
    if (BoxPrice1.Checked) //check price
    {
        if (price[a] < 200000 && price[a] > 300000) {
            found = false;
            foundPosition = a;
        } else break;
    }
    if (BoxPrice2.Checked) //check price
    {
        if (price[a] < 300000 && price[a] > 400000) {
            found = false;
            foundPosition = a;
        } else break;
    }
    if (BoxPrice3.Checked) //check price
    {
        if (price[a] < 400000 && price[a] > 500000) {
            found = false;
            foundPosition = a;
        } else break;
    }
    if (BoxPrice4.Checked) //check price
    {
        if (price[a] < 500000) {
            found = false;
            foundPosition = a;
        } else break;
    }
    if (found) {
        printcount++;
        label1.Text += string.Format("Bed: {0}, Bath:{1}, Asking Price:{2}, City:{3},SQFT:{4}, Zip Code:{5}, Year:{6}", bed[foundPosition], bath[foundPosition], price[foundPosition].ToString("c2"), city[foundPosition], sqft[foundPosition].ToString("n0"), zip[foundPosition], year[foundPosition]);
    }
}
if (printcount == 0) label1.Text = ("Sorry there were no houses that met your criteria");

刚刚更改了您的要求的代码,但无法对其进行测试