如何根据另一个函数在一个函数中中断 for 循环
How to break a for loop in one function based off of another function
我有一个名为 RunGame
的方法,其中包含一个图块数组。然后,我有一个 foreach
循环调用另一个名为 CheckValid
的方法,该方法传入图块编号和游戏编号。当调用 checkvalid 时,它会检查数据库中的一个字段,看它是否为空。我想要做的是让 foreach 语句在找到不为空的字段时停止 运行ning。这是我目前拥有的:
public void RunGame()
{
try
{
int[,] game = new int[10, 1] { { 70 }, { 71 }, { 62 }, { 14 }, { 71 }, { 12 }, { 12 }, { 41 }, { 19 }, { 71 } };
int num = 2;
foreach (int i in game)
{
CheckValid(i, num);
}
Console.ReadLine();
}
catch (Exception error)
{
Console.WriteLine(error);
Console.ReadLine();
}
}
public void CheckValid(int tile, int game)
{
using (MySqlCommand cmd = new MySqlCommand("CheckValid", Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("tileVal", tile);
cmd.Parameters.AddWithValue("num", game);
Conn.Open();
using (MySqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
if (read[0] != DBNull.Value)
{
Console.WriteLine($"This tile is occupied");
}
else
{
Console.WriteLine($"Tile {tile} is not occupied");
}
}
read.Close();
}
Conn.Close();
}
}
我希望它 运行 基于这个 if 语句的条件:
if (read[0] != DBNull.Value)
{
Console.WriteLine($"This tile is occupied");
//Stop the foreach loop
}
else
{
Console.WriteLine($"Tile {tile} is not occupied");
//Continue the foreach loop
}
目前,当 tile 71 被占用时,它会打印:
Tile 70 is not occupied
This tile is occupied //I would like it to stop before it prints this line
Tile 62 is not occupied
Tile 14 is not occupied
This tile is occupied
Tile 12 is not occupied
Tile 12 is not occupied
Tile 41 is not occupied
Tile 19 is not occupied
This tile is occupied
有可能实现吗?
您可以将这样的方法写入 return 一个 bool
(true/false) 值:
public bool CheckValid(int tile, int game)
{
using (MySqlCommand cmd = new MySqlCommand("CheckValid", Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("tileVal", tile);
cmd.Parameters.AddWithValue("num", game);
Conn.Open();
using (MySqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
if (read[0] != DBNull.Value)
{
Console.WriteLine($"This tile is occupied");
return false;
}
else
{
Console.WriteLine($"Tile {tile} is not occupied");
return true;
}
}
read.Close();
}
Conn.Close();
}
return false;
}
然后像这样更改您的其他代码以检查 CheckValid
:
的结果
if (!CheckValid(i, num))
{
break;
}
因此您可以使用布尔结果来确定是否应该继续执行循环。
请注意,我已将 CheckValid
修改为 return false
如果没有数据(意味着循环将 break;
)。如果您希望相反,请将 return false;
更改为 return true;
我有一个名为 RunGame
的方法,其中包含一个图块数组。然后,我有一个 foreach
循环调用另一个名为 CheckValid
的方法,该方法传入图块编号和游戏编号。当调用 checkvalid 时,它会检查数据库中的一个字段,看它是否为空。我想要做的是让 foreach 语句在找到不为空的字段时停止 运行ning。这是我目前拥有的:
public void RunGame()
{
try
{
int[,] game = new int[10, 1] { { 70 }, { 71 }, { 62 }, { 14 }, { 71 }, { 12 }, { 12 }, { 41 }, { 19 }, { 71 } };
int num = 2;
foreach (int i in game)
{
CheckValid(i, num);
}
Console.ReadLine();
}
catch (Exception error)
{
Console.WriteLine(error);
Console.ReadLine();
}
}
public void CheckValid(int tile, int game)
{
using (MySqlCommand cmd = new MySqlCommand("CheckValid", Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("tileVal", tile);
cmd.Parameters.AddWithValue("num", game);
Conn.Open();
using (MySqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
if (read[0] != DBNull.Value)
{
Console.WriteLine($"This tile is occupied");
}
else
{
Console.WriteLine($"Tile {tile} is not occupied");
}
}
read.Close();
}
Conn.Close();
}
}
我希望它 运行 基于这个 if 语句的条件:
if (read[0] != DBNull.Value)
{
Console.WriteLine($"This tile is occupied");
//Stop the foreach loop
}
else
{
Console.WriteLine($"Tile {tile} is not occupied");
//Continue the foreach loop
}
目前,当 tile 71 被占用时,它会打印:
Tile 70 is not occupied
This tile is occupied //I would like it to stop before it prints this line
Tile 62 is not occupied
Tile 14 is not occupied
This tile is occupied
Tile 12 is not occupied
Tile 12 is not occupied
Tile 41 is not occupied
Tile 19 is not occupied
This tile is occupied
有可能实现吗?
您可以将这样的方法写入 return 一个 bool
(true/false) 值:
public bool CheckValid(int tile, int game)
{
using (MySqlCommand cmd = new MySqlCommand("CheckValid", Conn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("tileVal", tile);
cmd.Parameters.AddWithValue("num", game);
Conn.Open();
using (MySqlDataReader read = cmd.ExecuteReader())
{
while (read.Read())
{
if (read[0] != DBNull.Value)
{
Console.WriteLine($"This tile is occupied");
return false;
}
else
{
Console.WriteLine($"Tile {tile} is not occupied");
return true;
}
}
read.Close();
}
Conn.Close();
}
return false;
}
然后像这样更改您的其他代码以检查 CheckValid
:
if (!CheckValid(i, num))
{
break;
}
因此您可以使用布尔结果来确定是否应该继续执行循环。
请注意,我已将 CheckValid
修改为 return false
如果没有数据(意味着循环将 break;
)。如果您希望相反,请将 return false;
更改为 return true;