它只执行 if(i==0) 内的代码并忽略 if(i==1) 内的代码 还有 if(i==2) 但由于发布限制我删除了我是新手这个但是如果我能弄清楚为什么它不执行第一个 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}.");
}
}
关于c# - 它没有执行 else if(i==1) 中的代码,为什么会这样?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65643479/