简体   繁体   中英

How to Display Array Values using Nested For loops?

Before we begin, i am new at programming and C# language, So please bear with me. The problem i am having is to display two individual array values in front of each other like this :

Integer Type : sbyte, byte, short, ushort
Real Floating point types : float, double

The Code I have tried is :

string[] VariableTypes = { "Integer Type", "Real Floating Point Types" };

string[] IntergerTypes = { "sbyte", "byte", "short", "ushort" };

for (int i = 0; i <= VariableTypes.Length - 1; i++)
{
    for(int j=0; j<=IntergerTypes.Length - 1; j++)
    {
        Console.Write(IntergerTypes[j] + " ");
    }
    Console.WriteLine(VariableTypes[i] + " : ");
}

Output i am getting :

sbyte byte short ushort Integer Type :
sbyte byte short ushort Real Floating Point Types :

You need another array to hold the values you want to relate to Real Floating Point Types and then conditionally check the variable type to see which integer types are associated with it.

string[] VariableTypes = { "Integer Type", "Real Floating Point Types" };
string[] FloatingPointTypes = { "float", "double" };
string[] IntergerTypes = { "sbyte", "byte", "short", "ushort" };

for (int i = 0; i <= VariableTypes.Length - 1; i++)
{
    Console.WriteLine(VariableTypes[i] + " : ");

    if(VariableTypes[i] == "Integer Type")
    {
        for(int j=0; j<=IntergerTypes.Length - 1; j++)
        {
            Console.Write(IntergerTypes[j] + ", ");
        }
    }
    else
    {
        for(int j=0; j<=FloatingPointTypes.Length - 1; j++)
        {
            Console.Write(FloatingPointTypes[j] + ", ");
        }
    }
}

In my opinion, it would be cleaner do this without the nested loops if all you're wanting is a way to achieve the mentioned output:

string[] FloatingPointTypes = { "float", "double" };
string[] IntergerTypes = { "sbyte", "byte", "short", "ushort" };

Console.WriteLine("Integer Type: " + string.Join(", ", IntergerTypes));
Console.WriteLine("Real Floating Point Types: " + string.Join(", ", FloatingPointTypes));

Additionally, you could utilize a class to make this a bit more streamline for you. See the following:

public class VariableType
{
    public string Name { get; set; }

    public List<string> DataTypes { get; set; }
}

and use this class to achieve your desired outcome via the following:

var variableTypes = new List<VariableType>
{
    new VariableType
    {
        Name = "Integer Types",
        DataTypes = new List<string>{ "sbyte", "byte", "short", "ushort" }
    },
    new VariableType
    {
        Name = "Real Floating Point Types",
        DataTypes = new List<string>{ "float", "double" }
    }
};

foreach(var variableType in variableTypes)
{
    Console.WriteLine(variableType.Name + " : " + string.Join(", ", variableType.DataTypes));
}
string[] VariableTypes = {
  "Integer Type", "Real Floating Point Types" 
};
string[][] NumberTypes = {
  new string[] { "sbyte", "byte", "short", "ushort" },
  new string[] { "float", "double" }
};

for (int i = 0; i < VariableTypes.Length; i++)
{
    Console.Write(VariableTypes[i] + " : ");
    for(int j = 0; j < NumberTypes[i].Length; j++)
    {
        Console.Write(NumberTypes[i][j] + " ");
    }
    Console.WriteLine();
}

Here's a short method to achieve this:

string[] VariableTypes = { "Integer Types", "Real Floating Point Types" };
string[] IntegerTypes = { "sbyte", "byte", "short", "ushort" };
string[] RealFloatingPointTypes = { "float", "double" };
Console.WriteLine(String.Concat(VariableTypes[0], " : ", String.Join(", ", IntegerTypes)));
Console.WriteLine(String.Concat(VariableTypes[1], " : ", String.Join(", ", RealFloatingPointTypes)));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM