Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
57 views
in Technique[技术] by (71.8m points)

c# - it's not executing the code inside else if(i==1), why is that happening?

its only executing the code inside of the if(i==0) and ignoring the one inside if(i==1) there is also if(i==2) but i removed due to post restrictions im new to this but if i can figuere out why its not executing the code inside of the first else if statement i might be able to fix fix both

 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]);
                            }
                        }

                    }
                }
            }
question from:https://stackoverflow.com/questions/65643479/its-not-executing-the-code-inside-else-ifi-1-why-is-that-happening

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

I could see that your code works fine, all conditions are met. Actually, I felt pain reading your code. Try to understand what I've done here, it is just what you need:

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}.");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...