【发布时间】:2023-01-22 11:16:40
【问题描述】:
我试图根据用户的输入返回一个索引,输入只有 2 个字符,比如 a1、b2、c3...
public int returnInt(string x)
{
if (x == "a")
return 0;
else if (x == "b")
return 1;
else if (x == "c")
return 2;
else if (x == "d")
return 3;
else if (x == "e")
return 4;
else if (x == "f")
return 5;
else if (x == "g")
return 6;
else if (x == "h")
return 7;
else if (x == "1")
return 0;
else if (x == "2")
return 1;
else if (x == "3")
return 2;
else if (x == "4")
return 3;
else if (x == "5")
return 4;
else if (x == "6")
return 5;
else if (x == "7")
return 6;
else if (x == "8")
return 7;
return 0;
}
这就是我使用方法的地方:
var toMove = myButtonArray[returnInt(totxt.Text.Substring(0)), returnInt(totxt.Text.Substring(1))];该方法适用于第二个子字符串,但不适用于第一个子字符串 (0)。谁能帮我解决这个问题?当我键入 a1 时,程序应该返回 1 和 1,但它只为第一个子字符串返回 0。
【问题讨论】:
-
子字符串返回第一个参数中给出的索引之后的所有内容。查看文档和其中的示例。
-
使用 String.Substring 函数的第二个重载版本。换成
var toMove = myButtonArray[returnInt(totxt.Text.Substring(0,1)), returnInt(totxt.Text.Substring(1,1))];
标签: c# forms methods return return-value