我创建的方法中的奇怪问题。
我无法返回pointer
变量。
public static int serch_Acount(List<Accounts> AllAccount)
{
int pointer;
Console.WriteLine("please enter the id number");
int idSerch = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < AllAccount.Count ; i++)
{
if (idSerch ==AllAccount[i].id)
{
Console.WriteLine("name: "+AllAccount[i].accountName);
Console.WriteLine("id: " + AllAccount[i].id);
Console.WriteLine("age: " + AllAccount[i].age);
Console.WriteLine("balance: " + AllAccount[i].balance);
pointer = i;
}
else
{
Console.WriteLine("id doent found");
pointer= -1;
}
}
return pointer;
}
使用未分配的局部变量pointer
答案 0 :(得分:2)
如果- df[["a", "b"]].diff(axis=1)["b"]
为空,则循环将永远不会执行,也不会为AllAccount
赋值。
查看您的代码,我怀疑您可以在声明时将其设置为pointer
来轻松解决此问题:
-1
答案 1 :(得分:0)
1:您的指针应带有-1,0,默认值之类的值
2:如果发现任何东西,则必须返回,否则下一个发射器将更改您的指针
public static int serch_Acount(List<Accounts> AllAccount)
{
int pointer = default; // does not need
Console.WriteLine("please enter the id number");
int idSerch = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < AllAccount.Count; i++)
{
if (idSerch == AllAccount[i].id)
{
Console.WriteLine("name: " + AllAccount[i].accountName);
Console.WriteLine("id: " + AllAccount[i].id);
Console.WriteLine("age: " + AllAccount[i].age);
Console.WriteLine("balance: " + AllAccount[i].balance);
pointer = i; // does not need
return pointer;
// return i;
}
Console.WriteLine("id doent found");
pointer = -1; // does not need
}
return pointer;
// return -1;
}