【发布时间】:2014-12-09 04:33:08
【问题描述】:
您好,我对 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");
}
【问题讨论】:
-
您是否有任何理由不将有关房屋的信息存储在
House类中,而不是一堆不同的数组中?这将使它更容易,更不容易出错。 -
看起来像它的价格。我认为您的意思是检查价格是否大于下限并低于上限。
-
另外,你得到的房子不符合所有标准的原因是你的逻辑没有做你想做的事。您正在遍历所有房屋,并且只要您满足其中一个条件,您就设置
found = true。唯一设置为 false 的情况是它不符合价格范围。但是你的价格范围条件都是不可能的。例如,一个数字永远不会是 500000。 -
是的,我在定价部分看到了错误,谢谢。但是,我已经注释掉了定价部分,只是运行了单选按钮,这些按钮也不符合我的标准。
-
请带本书学习面向对象编程。您应该使用一个名为
House的类,其中包含所有必需的属性和方法。然后一切变得容易。