c# try and catch + 外部变量
c# try and catch + variables outside
如何在 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 + "'");
mudarspawn1() 中的 x y 和 z 变量返回为 x = 0 y = 0 和 z = 0
我该如何解决这个问题?
我试着做了一组并得到...但没有幸福的结局。
主要用途是:
玩家写 /mudarspawn1 [coordenadas]
例如:/mudarspawn1 656.32 65.21 698.1
但是如果他们使用字符,程序会出错,我想给他们发送聊天消息,告诉他们只能使用数字。
how can i get the variables x,y and z outside the try-catch?
在 try...catch
之外声明它们,但在其中设置它们,例如
float x = 0f;
float y = 0f;
float z = 0f;
try
{
x = float.Parse(...);
y = float.Parse(...);
z = float.Parse(...);
}
catch (Exception e)
{
...
}
您必须在 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.
}
需要在外面声明,在里面赋值
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);
}
....
这是您要查找的代码,
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 + "'");
}
谢谢大家!它的工作我在 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
如何在 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 + "'");
mudarspawn1() 中的 x y 和 z 变量返回为 x = 0 y = 0 和 z = 0
我该如何解决这个问题?
我试着做了一组并得到...但没有幸福的结局。
主要用途是: 玩家写 /mudarspawn1 [coordenadas]
例如:/mudarspawn1 656.32 65.21 698.1
但是如果他们使用字符,程序会出错,我想给他们发送聊天消息,告诉他们只能使用数字。
how can i get the variables x,y and z outside the try-catch?
在 try...catch
之外声明它们,但在其中设置它们,例如
float x = 0f;
float y = 0f;
float z = 0f;
try
{
x = float.Parse(...);
y = float.Parse(...);
z = float.Parse(...);
}
catch (Exception e)
{
...
}
您必须在 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.
}
需要在外面声明,在里面赋值
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);
}
....
这是您要查找的代码,
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 + "'");
}
谢谢大家!它的工作我在 mudarspawn1() 中使用了一个 if 来处理来自 xy 或 z
的 0public 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