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

本文关键字:显示 特定条件 搜索 数组 | 更新日期: 2023-09-27 18:33:34

嗨,我是 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");
}

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

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");

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