它没有执行 else if(i==1) 中的代码,为什么会这样?
it's not executing the code inside else if(i==1), why is that happening?
它只执行 if(i==0) 内部的代码并忽略 if(i==1) 内部的代码 还有 if(i==2) 但由于 [=13 我删除了=] 限制是我的新手,但如果我能弄清楚为什么它不执行第一个 else if 语句中的代码,我也许可以同时修复这两个问题
string[][] friendFamily = new string[][]
{
new string[]{"khzix","rengar","shaco" },
new string[]{"jhin","tf","karma" },
new string[]{"qiyanna","braum","thresh" }
};
for (int i = 0; i < friendFamily.Length; i++)
{
if (i == 0)
{
for (int x = 0; x < friendFamily[i].Length; x++)
{
for (int j = 0; j < friendFamily.Length; j++)
{
if (j == 1 || j == 2)
{
for (int k = 0; k < friendFamily.Length; k++)
{
Console.WriteLine("hey {0} this is {1}.", friendFamily[i][x], friendFamily[j][k]);
}
}
}
}
}
else if (i == 1)
{
for (int x = 0; x < friendFamily[i].Length; x++)
{
for (int j = 0; j < friendFamily.Length; j++)
{
if (j == 0 || j == 2)
{
for (int k = 0; k < friendFamily.Length; k++)
{
Console.WriteLine("hey {0} this is {1}.", friendFamily[i][x], friendFamily[j][k]);
}
}
}
}
}
我可以看到你的代码工作正常,所有条件都满足。实际上,阅读您的代码我感到很痛苦。尝试了解我在这里所做的,这正是您所需要的:
using System;
using System.Linq;
string[][] friendFamilies = new string[][]
{
new string[] { "khzix","rengar","shaco" },
new string[] { "jhin","tf","karma" },
new string[] { "qiyanna","braum","thresh" }
};
var allFriends = friendFamilies.SelectMany(x => x);
foreach (var friendToGreet in allFriends)
{
foreach (var friendWhoGreet in allFriends.Where(x => x != friendToGreet))
{
Console.WriteLine($"Hey {friendToGreet} this is {friendWhoGreet}.");
}
}
它只执行 if(i==0) 内部的代码并忽略 if(i==1) 内部的代码 还有 if(i==2) 但由于 [=13 我删除了=] 限制是我的新手,但如果我能弄清楚为什么它不执行第一个 else if 语句中的代码,我也许可以同时修复这两个问题
string[][] friendFamily = new string[][]
{
new string[]{"khzix","rengar","shaco" },
new string[]{"jhin","tf","karma" },
new string[]{"qiyanna","braum","thresh" }
};
for (int i = 0; i < friendFamily.Length; i++)
{
if (i == 0)
{
for (int x = 0; x < friendFamily[i].Length; x++)
{
for (int j = 0; j < friendFamily.Length; j++)
{
if (j == 1 || j == 2)
{
for (int k = 0; k < friendFamily.Length; k++)
{
Console.WriteLine("hey {0} this is {1}.", friendFamily[i][x], friendFamily[j][k]);
}
}
}
}
}
else if (i == 1)
{
for (int x = 0; x < friendFamily[i].Length; x++)
{
for (int j = 0; j < friendFamily.Length; j++)
{
if (j == 0 || j == 2)
{
for (int k = 0; k < friendFamily.Length; k++)
{
Console.WriteLine("hey {0} this is {1}.", friendFamily[i][x], friendFamily[j][k]);
}
}
}
}
}
我可以看到你的代码工作正常,所有条件都满足。实际上,阅读您的代码我感到很痛苦。尝试了解我在这里所做的,这正是您所需要的:
using System;
using System.Linq;
string[][] friendFamilies = new string[][]
{
new string[] { "khzix","rengar","shaco" },
new string[] { "jhin","tf","karma" },
new string[] { "qiyanna","braum","thresh" }
};
var allFriends = friendFamilies.SelectMany(x => x);
foreach (var friendToGreet in allFriends)
{
foreach (var friendWhoGreet in allFriends.Where(x => x != friendToGreet))
{
Console.WriteLine($"Hey {friendToGreet} this is {friendWhoGreet}.");
}
}