【问题标题】:how do you create a loop that cycles through more than one List in C#如何在 C# 中创建一个循环遍历多个列表的循环
【发布时间】:2018-12-24 21:14:12
【问题描述】:

我不想要答案,我只想知道如何解决这个问题或一些例子!

要求

创建一个名为 CombineTheLists 的自定义方法并使用您的 2 个列表 在函数调用中作为参数创建。

我。该方法应该有 2 个参数并捕获传入的列表。

二。它不应该返回任何东西。

三。在函数内部,创建一个循环遍历两者 同时列出。

四。每次循环运行时,从第一个列表中拉出一个项目,然后 匹配第二个列表中的价格并将它们组合成一个文本字符串 使用“X 成本 $Y”的格式。其中 X 是要购买的物品 Y 是项目的成本。确保使用 $ 和格式为 2 小数位

我当前的代码

enter code    class Program
{
    static void Main(string[] args)
    {
        // list for items
        List<string> items = new List<string>() { 
          "laptop", "book", "backpack", "cellphone", "pencils", "notebook", "pens" };

        // list for prices
        List<double> prices = new List<double>() { 
          900.54, 40.20, 21.00, 600.00, 4.25, 10.50, 5.00 };
    }
}   

public static void CombineTheLists( string item, double prices)
{
    for (int i = 0; i < item.Length; i++)
    {

    }
}

}

【问题讨论】:

  • 我不想要答案……那你为什么要问这个问题呢?
  • @B001ᛦ 大多数人倾向于完成我的代码以完成,我只是想要他们自己的示例或关于如何去做的想法......而不是对我的特定代码给出答案。跨度>
  • 这两个列表是否总是具有相同数量的项目?
  • @SBFrancies 是的,它们是硬编码的,我设置了值
  • "它应该捕获传入的列表" - 那么string item, double prices 应该是List&lt;string&gt; item, List&lt;double&gt; prices 在您的参数中

标签: c# list function loops


【解决方案1】:

看来,您正在寻找Zip:

var data = items
  .Zip(prices, (item, price) => new {
     item,
     price });

...

foreach (var value in data) {
  // value.item  for item
  // value.price for price
}

编辑:如果是旧的for循环:

namespace MySolution {
  class Program {
    static void Main(string[] args) {
      ...

      CombineTheLists(items, prices);
    }

    public static void CombineTheLists(List<string> items, List<double> prices) {
      for (int i = 0; i < Math.Min(items.Count, prices.Count); ++i)
        Console.WriteLine($"item {items[i]} costs {prices[i]:f2}"); // f2 - format string
    }
  }
}

【讨论】:

  • 我可能应该提到我是一名学生,我们不允许超出我们所学的范围。到目前为止,我们还没有触及 .Zip
  • 知道为什么我的方法名称出现语法错误吗? (命名空间不能直接包含字段或方法等成员)
  • 我很确定当 Sweeper 发布他的答案时你的答案不存在。那是9分钟。你怎么隐藏答案这么久?
  • @RoVaLu,东西需要在一个类中。命名空间>类>字段/方法
  • @Drag and Drop:我已经用Zip 给出了答案,得到了Zip 未被接受并提供了for 循环的版本:几个编辑,这就是我得到的原因9分钟时差
【解决方案2】:

您在(其中一个)正确的轨道上,但首先,您应该更改您的方法声明以接受两个列表,而不是字符串和双精度:

public static void CombineTheLists(List<string> items, List<double> prices)
{
    for (int i = 0; i < items.Count; i++)
    {

    }
}

您已经编写了一个带有循环变量i 的for 循环。如您所知,i 每次迭代都会增加。这意味着items[i] 将是该迭代中的对应项目,prices[i] 将是对应的价格。例如,这里有一些代码可以在一行上打印商品和价格:

// in the for loop
Console.WriteLine($"{items[i]} - {prices[i]}");
// try to do the formatting to 2 d.p. yourself

【讨论】:

  • 错字:i &lt; item.Length 应该是i &lt; items.Count
  • @DmitryBychenko 已更正。谢谢!
  • @Drag and Drop: indexer [...] 也可以使用 List&lt;T&gt;; items[i] 是正确的语法。您可能正在混合 C# 和 Java
【解决方案3】:

为什么不创建一个具有两个属性和一个对象列表的对象?

 class item {
        String name;
        String price;

        /*getter and setters*/
        }
class main {
     public static void CombineTheLists()
    {
        List<item> items = new List<item>() { item1, item2, item3,.... };
    }
}

【讨论】:

    猜你喜欢
    • 2015-12-11
    • 1970-01-01
    • 2016-02-24
    • 1970-01-01
    • 2019-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多