ArrayList stateList, gdpList, rankList;
public void Page_Load(object sender, EventArgs e)
{
stateList = new ArrayList(); //The state array list
stateList.Add("Delaware");
stateList.Add("Alaska");
stateList.Add("North Dakota");
stateList.Add("Connecticut");
stateList.Add("Wyoming");
stateList.Add("Massachusetts");
stateList.Add("New York");
stateList.Add("New Jersey");
stateList.Add("Oregon");
stateList.Add("Washington");
stateList.Add("Virginia");
stateList.Add("Minnesota");
rankList = new ArrayList(); //The ranking array list for each state
rankList.Add(1);
rankList.Add(2);
rankList.Add(3);
rankList.Add(4);
rankList.Add(5);
rankList.Add(6);
rankList.Add(7);
rankList.Add(8);
rankList.Add(9);
rankList.Add(10);
rankList.Add(11);
rankList.Add(12);
gdpList = new ArrayList(); //The GDP array list for each state
gdpList.Add("61,183");
gdpList.Add("61,156");
gdpList.Add("55,250");
gdpList.Add("54,925");
gdpList.Add("54,305");
gdpList.Add("53,221");
gdpList.Add("53,067");
gdpList.Add("49,430");
gdpList.Add("48,069");
gdpList.Add("47,146");
gdpList.Add("47,127");
gdpList.Add("47,028");
}
void GDP_Click(object sender, EventArgs e)
{
string state1 = State.Text;
for (int i = 0; i < stateList.Count; i++ )
{
if (state1 == stateList[i])
{
Response.Write("The " + stateList[i] + " state GDP is " + gdpList[i] + " and the rank is " + rankList[i]);
}
else if (state1 == stateList[i])
{
Response.Write("The state that you entered is not a part of our state list");
}
}
}
所以我有这三个数组。 一个有十二个州,一个排名为1-12,其他国家的GDP排在另一个州。有一个文本框和一个按钮。如果您在文本框中输入say .. Delaware,然后单击按钮,它将生成一个标签,其中显示:州,他们的等级和他们的GDP。但是,无论我放入什么状态,它都会返回“状态未列出”,即使它与数组匹配。我假设循环继续运行。所以我尝试了,在每次回复后添加了break,但是没有用。然后我尝试了,在每次回复后添加返回,但也没有用。我试过布尔但是无法弄清楚它。
答案 0 :(得分:0)
你的循环中的条件检查同样的事情。并且,如果在没有匹配时执行第二个条件,您仍然会写入stateList集合中每个项目的响应。
相反,请尝试:
void GDP_Click(object sender, EventArgs e)
{
string state1 = State.Text;
bool foundMatch = false;
for (int i = 0; i < stateList.Count; i++ )
{
if (state1 == stateList[i])
{
Response.Write("The " + stateList[i] + " state GDP is " + gdpList[i] + " and the rank is " + rankList[i]);
foundMatch = true;
break;
}
}
if (!foundMatch)
{
Response.Write("The state that you entered is not a part of our state list");
}
}
答案 1 :(得分:0)
else
的条件与if
条件完全相同。
我认为你重构代码就像
一样void GDP_Click(object sender, EventArgs e)
{
string state1 = State.Text;
for (int i = 0; i < stateList.Count; i++ )
{
if (state1 == stateList[i])
{
Response.Write("The " + stateList[i] +
" state GDP is " + gdpList[i] + " and the rank is " + rankList[i]);
return;
}
}
Response.Write("The state that you entered is not a part of our state list");
}
答案 2 :(得分:0)
这是一种更简单的方法来做你想要做的事情注意我已经初始化列表和列表的不同方式现在你可以使用它作为一种学习工具,作为一种更有效的方式做你编码的。如果你想在代码不在列表中时突破代码,那么你需要在这一行之后添加break;
Response.Write(string.Format("The {0} state GDP is {1} and the rank is {2} " ,stateList[i], gdpList[i], rankList[i]));
如果你需要返回一个布尔值,我会改变方法,或者我会创建一个你可以在当前所在方法之外访问的属性
var stateList = new List<string>
{
"Delaware",
"Alaska",
"North Dakota",
"Connecticut",
"Wyoming",
"Massachusetts",
"New York",
"New Jersey",
"Oregon",
"Washington",
"Virginia",
"Minnesota"
};
var rankList = new List<int>
{
1,2,3,4,5,6,7,8,9,10,11,12
};
var gdpList = new List<string>
{
"61,183",
"61,156",
"55,250",
"54,925",
"54,305",
"53,221",
"53,067",
"49,430",
"48,069",
"47,146",
"47,127",
"47,028"
};
var bFound = false;
string state1 = State.Text;
for (int i = 0; i < stateList.Count; i++)
{
if (state1.Contains(stateList[i]))
{
Response.Write(string.Format("The {0} state GDP is {1} and the rank is {2} " ,stateList[i], gdpList[i], rankList[i]));
bFound = true;
break;
}
}
if (!bFound)
{
Response.Write("The state that you entered is not a part of our state list");
return;
}
答案 3 :(得分:0)
将GDP_Click
方法中的代码更改为:
string state1 = State.Text;
for (int i = 0; i < stateList.Count; i++)
{
if (state1.Equals(stateList[i]))
{
Response.Write("The " + stateList[i] + " state GDP is " + gdpList[i] + " and the rank is " + rankList[i]);
return;
}
}
Response.Write("The state that you entered is not a part of our state list");
这里有重要的部分:state1.Equals(stateList[i])
为什么==
无效?
ArrayList
是一个非泛型集合,当您从中检索元素时,它会返回一个盒装Object
类型。因此,在执行state1 == stateList[i]
时,这意味着您要将String
类型与Object
类型进行比较,将不 String
与String
进行比较,比较将作为Object
参考比较执行(两个操作数是否指向同一个对象)
请注意,==
运算符和Equals
方法不同。如果您确定要比较的两个实例具有相同的特定类型,那么使用==
运算符是安全的。否则,如果其中一个实例被装入Object
类型,则我建议您使用Equals
方法。
*)进一步阅读:
考虑以下代码:
object a = 123;
object b = 123;
a == b
将始终返回 false ,即使它们包含相同的整数。因为比较两个Object
类型将进行参考比较,而不是对象本身的内容。
但是a.Equals(b)
或b.Equals(a)
将能够返回true。或者甚至更好地使用Equals(a, b)
(来自Object.Equals(a, b)
的快捷方式),如果NullReferenceException
或a
为b
,则最新表单将避免null
。