我写了这段代码:
class Program
{
static void Main(string[] args)
{
Test t = new Test();
int[] tal1 = { 3, 2, 3};
int[] tal2 = { 1, 2, 3};
Console.WriteLine(t.theSameInBoth(tal1,tal2));
}
}
class Test
{
public Boolean theSameInBoth(int[] a, int[] b)
{
bool check = false;
if (a.Length == b.Length)
{
for (int i = 0; i < a.Length; i++)
if (a[i].Equals(b[i]))
{
check = true;
return check;
}
}
return check;
}
}
所以这里的交易是。我需要发送两个包含数字的数组。然后我需要检查数组。如果数组中的所有数字都相同。我需要将支票设置为 true 并返回。唯一的问题是。使用我在此处设置的代码,我发送了一个包含 3,2,3 的数组和一个包含 1,2,3 的数组,它仍然返回检查为真。
我是这方面的新手,所以我希望这里的任何人都可以帮助我!
最佳答案
你需要逆向测试:
class Test
{
public bool theSameInBoth(int[] a, int[] b)
{
if (a.Length == b.Length)
{
for (int i = 0; i < a.Length; i++)
{
if (a[i] != b[i])
{
return false;
}
}
return true;
}
return false;
}
}
只要一对项目不同,两个数组就不同。
在您的代码中,您实际上是说只要一对项目相等,两个数组就相等。
关于c# - 比较两个 int 数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26576111/