代码如下 结果应该是 1吧 怎么运行后结果是12呢? 还有 把输出改成h[1]后 结果什么输出都没有 只有"按任意键退出..."using System;
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication4
{
class Program
{
static string[] h = new string[255];
static void Main(string[] args)
{
try
{
string[] a ={ "1", "12", "13" };
string[] b ={ "12", "1", "15" };
Same(a, b);
Console.WriteLine(h[0]);
}
catch(Exception e)
{ }
}
public static void Same(string[] ArrayA, string[] ArrayB)
{
for (int i = 0; i < ArrayA.Length; i++)
{
for (int j = 0; j < ArrayB.Length; j++)
{
if (ArrayA[i] == ArrayB[j])
{
int y = 0;
h[y]=ArrayA[i];
++y;
}
}
}
}
}
}
using System.Collections.Generic;
using System.Text;namespace ConsoleApplication4
{
class Program
{
static string[] h = new string[255];
static void Main(string[] args)
{
try
{
string[] a ={ "1", "12", "13" };
string[] b ={ "12", "1", "15" };
Same(a, b);
Console.WriteLine(h[0]);
}
catch(Exception e)
{ }
}
public static void Same(string[] ArrayA, string[] ArrayB)
{
for (int i = 0; i < ArrayA.Length; i++)
{
for (int j = 0; j < ArrayB.Length; j++)
{
if (ArrayA[i] == ArrayB[j])
{
int y = 0;
h[y]=ArrayA[i];
++y;
}
}
}
}
}
}
if (ArrayA[i] == ArrayB[j])
{
int y = 0;//只给第一个元素
h[y]=ArrayA[i];
++y;//没用
}
h[0]先是“1”
后来变为"12"
故最终是“12”
h[y]=ArrayA[i];
++y;你这里的 h[y] 永远都是 h[0].
{
int y = 0;
h[y]=ArrayA[i];
++y;
}//第一次相等时是1,第二次是12,覆盖掉第一次
lz把int y = 0;提到循环最外面就可以了