文本框输入不会从数组生成
本文关键字:数组 输入 文本 | 更新日期: 2023-09-27 18:29:48
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 在另一个数组中。有一个文本框和一个按钮。如果你输入说..特拉华州 在文本框中,然后单击按钮,它将生成一个标签,上面写着:州、他们的排名和他们的 GDP。但是,无论我输入什么状态,即使它与数组匹配,它也将始终返回"状态未列出"。我假设循环继续运行。所以我尝试了,在每个回复后添加中断,但没有奏效。然后我尝试了,在每个响应后添加返回,这也不起作用。我尝试了布尔值,但无法完全弄清楚。
你在循环中的条件检查同样的事情。 而且,如果在不匹配时执行第二个条件,您仍将写入 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");
}
}
您的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");
}
这是一种更简单的方法来执行您正在尝试做的事情,请注意我初始化列表和列表的不同方式,现在您可以将其用作学习工具,作为执行已编码操作的更有效方法。 如果您想在代码不在列表中时中断代码,则需要在此行之后添加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;
}
方法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
方法。
*( 进一步阅读:
- == 和 Equals(( 之间的 C# 差异
- 是字符串。等于((和==运算符真的一样吗?
请考虑以下代码:
object a = 123;
object b = 123;
a == b
将始终返回 false,即使它们包含相同的整数也是如此。因为比较两个Object
类型会做一个参考比较,而不是对象本身的内容。
但a.Equals(b)
或b.Equals(a)
将能够返回真。或者更好的是使用Equals(a, b)
(Object.Equals(a, b)
的快捷方式(,如果a
或b
null
,最新的表单将避免NullReferenceException
。