如何在try-catch外获取变量x,y和z?
这是我的代码:
public static void mudarSpawn_1(GtaPlayer player, string coordenadas)
{
char[] delimiterChars = { ' ', ',' };
string text = coordenadas;
string[] words = text.Split(delimiterChars);
try
{
float x = float.Parse(words[0], System.Globalization.CultureInfo.InvariantCulture);
float y = float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture);
float z = float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture);
}
catch (Exception e)
{
player.SendClientMessage(Color.DarkOrange, "Os valores de x,y e z não foram inseridos de forma correcta, apenas podes usar numeros");
player.SendClientMessage(Color.DarkOrange, "Ex: /mudarspawn3 6321.6 , 96321.38 , 66322.2 ou /mudarspawn3 6321.6 96321.38 66322.2");
}
MSGameMysql.mudarspawn1(x, y, z);
player.SendClientMessage(Color.AliceBlue, "Acabaste de mudar o spawn 1 para: x='" + x + "' y='" + y + "' z='" + z + "'");
}
MSGameMysql.mudarspawn1(x, y, z);
player.SendClientMessage(Color.AliceBlue, "Acabaste de mudar o spawn 1 para: x='" + x + "' y='" + y + "' z='" + z + "'");
这个x y和z变量在mudarspawn1()内变回x = 0 y = 0且z = 0
我该如何解决这个问题?
我试着做一套但得到......但没有幸福的结局。
主要用途是: 玩家写/ mudarspawn1 [coordenadas]
Ex:/ mudarspawn1 656.32 65.21 698.1
但如果他们使用字符,程序将收到错误,我想向他们发送聊天,告诉他们只能使用数字。
答案 0 :(得分:6)
您必须在try
范围之前声明并初始化它们:
float x = 0f;
float y = 0f;
float z = 0f;
try
{
x = float.Parse(words[0], System.Globalization.CultureInfo.InvariantCulture);
y = float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture);
z = float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture);
}
catch (Exception e)
{
player.SendClientMessage(Color.DarkOrange, "Os valores de x,y e z não foram inseridos de forma correcta, apenas podes usar numeros");
player.SendClientMessage(Color.DarkOrange, "Ex: /mudarspawn3 6321.6 , 96321.38 , 66322.2 ou /mudarspawn3 6321.6 96321.38 66322.2");
}
MSGameMysql.mudarspawn1(x, y, z);
player.SendClientMessage(Color.AliceBlue, "Acabaste de mudar o spawn 1 para: x='" + x + "' y='" + y + "' z='" + z + "'");
但是,您最好不要在float.TryParse()
查看,也不要在此处使用异常处理(这可能会给您带来不良后果)。例如,
float x;
if (!float.TryParse(words[0], NumberStyles.AllowThousands || NumberStyles.Float, System.Globalization.CultureInfo.InvariantCulture, out x))
{
// unable to parse words[0] as a float, handle it here.
}
答案 1 :(得分:3)
如何在try-catch外获取变量x,y和z?
在try...catch
之外声明它们,但将它们设置在其中,例如。
float x = 0f;
float y = 0f;
float z = 0f;
try
{
x = float.Parse(...);
y = float.Parse(...);
z = float.Parse(...);
}
catch (Exception e)
{
...
}
答案 2 :(得分:1)
您需要在外面声明它们并在内部协助它们
float x;
float y;
float z;
try
{
x = float.Parse(words[0], System.Globalization.CultureInfo.InvariantCulture);
y = float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture);
z = float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture);
}
....
答案 3 :(得分:1)
以下是您要查找的代码,
public static void mudarSpawn_1(GtaPlayer player, string coordenadas)
{
char[] delimiterChars = { ' ', ',' };
string text = coordenadas;
string[] words = text.Split(delimiterChars);
float x = new float();
float y = new float();
float z = new float();
try
{
x = float.Parse(words[0], System.Globalization.CultureInfo.InvariantCulture);
y = float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture);
z = float.Parse(words[1], System.Globalization.CultureInfo.InvariantCulture);
}
catch (Exception e)
{
player.SendClientMessage(Color.DarkOrange, "Os valores de x,y e z não foram inseridos de forma correcta, apenas podes usar numeros");
player.SendClientMessage(Color.DarkOrange, "Ex: /mudarspawn3 6321.6 , 96321.38 , 66322.2 ou /mudarspawn3 6321.6 96321.38 66322.2");
}
MSGameMysql.mudarspawn1(x, y, z);
player.SendClientMessage(Color.AliceBlue, "Acabaste de mudar o spawn 1 para: x='" + x + "' y='" + y + "' z='" + z + "'");
}
答案 4 :(得分:0)
谢谢你们!它的工作我在mudarspawn1()中使用了一个if来处理来自xy或z的0
public static void mudarspawn1(GtaPlayer player, float x, float y, float z)
{
if (x == 0 || y == 0 || z == 0)
{
}
else
{
string sql = "update spawnposition set spawn_1x = '" + x + "', spawn_1y = '" + y + "', spawn_1z = '" + z + "' where id ='1'";
try
{
MySqlConnection conn = new MySqlConnection(cs);
conn.Open();
MySqlCommand cmd1 = new MySqlCommand(sql, conn);
cmd1.ExecuteNonQuery();
player.SendClientMessage(Color.DarkOrange, "Foi adicionado o spawn 1");
}
catch (MySqlException e)
{
}
}
修正:D