具体细节自己去调试便知:
情况1:-->找到后跳转,不会再次执行(但实际上有2个相等的值)
程序代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ConsoleTest
{
class Program
{
static StringBuilder output = new StringBuilder();
static void Main(string[] args)
{
StringBuilder output = new StringBuilder();
int i = 0, j = 0;
int temp = 0;
int[,] s1 = new int[,] { { 45, 23 }, { 1, 2 }, { 8, 9 } };
int[,] s2 = new int[,] { { 4, 23 }, { 10, 20 }, { 7, 9 } };
foreach (int a in s1)
{
i = 0;
for (; i < 3; i++)
{
j = 0;
for (; j < 2; j++)
{
if (a == s2[i, j])
{
temp = a;
goto done;
}
else continue;
}
}
}
done:
output.Append(string.Format("找到{0}位置[{1}][{2}]", temp, i, j) + "\n");
//output += string.Format("没有找到{0}位置[{1}][{2}]", a, i, j);
MessageBox.Show(output.ToString());
}
}
}
完善第一种情况: -->输出了2个相等的值
程序代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace ConsoleTest
{
class Program
{
static StringBuilder output = new StringBuilder();
static int i =0, j =0 ;
static int temp=0;
static void Main(string[] args)
{
int[,] s1 = new int[,] { { 45, 23 }, { 1, 2 }, { 8, 9 } };
int[,] s2 = new int[,] { { 4, 23 }, { 10, 20 }, { 7, 9 } };
foreach (int a in s1)
{
i = 0;
for (; i < 3; i++)
{
j = 0;
for (; j < 2; j++)
{
if (a == s2[i, j])
{
temp = a;
show();
}
else continue;
}
}
}
}
static void show() {
output.Append(string.Format("找到{0}位置[{1}][{2}]", temp, i, j) + "\n");
//output += string.Format("没有找到{0}位置[{1}][{2}]", a, i, j);
MessageBox.Show(output.ToString());
}
}
}