allignment 2 array c#using loop

时间:2015-10-09 23:31:13

标签: c# arrays

我有两个食物和价格阵列。我希望在显示时对齐两者 像这样

Food           Price

Food1           Price1
Food2           Price2
Food3           Price3

到目前为止我的代码:

int[] array = { 1, 2, 3, 4, 5 };
string[] food = { "Kare-Kare", "Hotdog", "Egg", "Ham", "Pancit Canton" };
Console.Write("FOOD\t\tPRICE\n");
for(int i=0; i<food.Length;i++)
{
    for(int j =i; j<array.Length;j++)
    {
        Console.Write(array[j]);
    }
    Console.WriteLine(food[i]);
}

感谢

1 个答案:

答案 0 :(得分:0)

我认为这就是你想要的。该代码确实假设您在食品中的条目与价格一样多。

int[] prices = { 1, 2, 3, 4, 5 };
string[] food = { "Kare-Kare", "Hotdog", "Egg", "Ham", "Pancit Canton" };
Console.Write("FOOD\t\tPRICE\n");
for(int i = 0; i < food.Length; i++)
{
    Console.WriteLine(food[i] + "\t\t" + prices[i]);
}