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

C# 在数组中搜索特定条件并显示,c#,arrays,nested-if,C#,Arrays,Nested If,嗨,我对C#编程完全是新手,我的代码被卡住了。我的程序是向用户询问城市或邮政编码、他们想要的床位/浴室数量以及价格范围。我需要搜索我的数组,然后显示所有符合条件的房子(想想Zillow,网站)。我的代码当前显示的随机房屋不符合我为住宅选择的任何标准。救命啊 for (int a = 0; a < HOUSES; ++a) { if (zipChecker == zip[a]) // check zip code { found = true;

嗨,我对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");
}
for(int a=0;a0&&巴斯[a]1&&巴斯[a]2&&巴斯[a]3)
{
发现=真;
foundPosition=a;
}
}
如果(BtnBed1.Checked)//检查床
{
如果(床[a]>0和床[a]1和床[a]2和床[a]3)
{
发现=真;
foundPosition=a;
}
}
if(BoxPrice1.Checked)//检查价格
{
如果(价格[a]<200000和价格[a]>300000)
{
发现=错误;
foundPosition=a;
}
}
if(BoxPrice2.Checked)//检查价格
{
如果(价格[a]<300000和价格[a]>400000)
{
发现=错误;
foundPosition=a;
}
}
if(BoxPrice3.Checked)//检查价格
{
如果(价格[a]<400000和价格[a]>500000)
{
发现=错误;
foundPosition=a;
}
}
if(BoxPrice4.Checked)//检查价格
{
如果(价格[a]<500000)
{
发现=错误;
foundPosition=a;
}
}
}
如果(找到)
{
标签1.文本+=
格式(“Bed:{0},Bath:{1},要价:{2},City:{3},SQFT:{4},”+
“邮政编码:{5},年份:{6}”,bed[foundPosition],bath[foundPosition],
价格[foundPosition]。ToString(“c2”),城市[foundPosition],
sqft[foundPosition].ToString(“n0”),zip[foundPosition],
年份[职位];
}
其他的
{
label1.Text=(“对不起,没有符合您标准的房屋”);
}
int printcount=0;
对于(int a=0;a0&&巴斯[a]1&&巴斯[a]2&&巴斯[a]3){
发现=真;
foundPosition=a;
}否则就断了;
}
如果(BtnBed1.Checked)//检查床
{
如果(床[a]>0和床[a]1和床[a]2和床[a]3){
发现=真;
foundPosition=a;
}否则就断了;
}
if(BoxPrice1.Checked)//检查价格
{
如果(价格[a]<200000和价格[a]>300000){
发现=错误;
foundPosition=a;
}否则就断了;
}
if(BoxPrice2.Checked)//检查价格
{
如果(价格[a]<300000和价格[a]>400000){
发现=错误;
foundPosition=a;
}否则就断了;
}
if(BoxPrice3.Checked)//检查价格
{
如果(价格[a]<400000和价格[a]>500000){
发现=错误;
foundPosition=a;
}否则就断了;
}
if(BoxPrice4.Checked)//检查价格
{
如果(价格[a]<500000){
发现=错误;
foundPosition=a;
}否则就断了;
}
如果(找到){
printcount++;
label1.Text+=string.Format(“Bed:{0},Bath:{1},要价:{2},城市:{3},SQFT:{4},邮政编码:{5},年份:{6}”,Bed[foundPosition],Bath[foundPosition],Price[foundPosition]。ToString(“c2”),City[foundPosition],SQFT[foundPosition]。ToString(“n0”),Zip[foundPosition],Year[foundPosition],Year];
}
}
if(printcount==0)label1.Text=(“很抱歉,没有符合您标准的房屋”);

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

是否有任何理由不将有关房屋的信息存储在
房屋
类中,而不是存储在一堆不同的数组中?这将使它变得更容易,更不容易出错。看起来这是价格的问题。我想你的意思是检查价格是否高于下限,是否低于上限。此外,你得到的房子不符合所有标准的原因是,你的逻辑不是你想要的。你在所有的房子里循环,只要你满足其中一个条件,你就会设置
found=true
。只有当它不符合价格范围时,才会设置为false。但你们的价格范围条件都是不可能的。例如,一个数字永远不会小于400000和>500000。是的,我看到定价部分有错误,谢谢。然而,我已经注释掉了定价部分,只运行了单选按钮,这些也不符合我的标准。请拿一本书学习面向对象编程。您应该使用名为
House
的类,以及其中所有必需的属性和方法。然后一切都变得容易了。
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");