【问题标题】:C# Simpler way to iterate thru display selectsC# 通过显示选择进行迭代的更简单方法
【发布时间】:2014-02-23 01:57:18
【问题描述】:

新手在这里寻求帮助。

目前我正在使用类,为此,我正在使用类根据我的产品类对象打印出 5 个直接对象。

这是打印我的物品的代码:

Console.WriteLine("1." + prod1.GetDetails() + "\n\n");
Console.WriteLine("2." + prod2.GetDetails() + "\n\n");
Console.WriteLine("3." + prod3.GetDetails() + "\n\n");
Console.WriteLine("4." + prod4.GetDetails() + "\n\n");
Console.WriteLine("5." + prod5.GetDetails() + "\n\n");

查看我的代码,感觉非常多余,因为只有两个字符发生了变化:第一个双引号中显示的项目编号,然后是区分我使用我的类对象打印的“产品”的数字产品(我是新手,希望我能正确传达这一点)。

我尝试创建一种方法来迭代它,因为这样会更干净且可扩展,但我的方法工作正常。

我最近的尝试:

public void DisplayOfferings()
{
    for (int i = 1; i < 6; i++ )//prints items 1 to 5 
    {
      Console.WriteLine(i + ". " + prod(i).GetDetails() + "\n\n"); 
    }  //iterate number      
}

这就是我存储我要打印的产品的方式,我将它们放在类 PROGRAM 级别,因此它们可用于所有功能,因为我最终会使用它们几次 课堂节目 {

    Product prod1 = new Product("iPod Shuffle", "The clip-and-go iPod shuffle. With buttons, VoiceOver, and playlists, it's the best of iPod shuffle. Available in seven colors.", 49.89, true);

    Product prod2 = new Product("iPod Nano", "About the size of a credit card — and just 5.4 mm thin — iPod nano is the thinnest iPod ever made. Available in seven colors.", 149.89, true);

    Product prod3 = new Product("iPod Classic", "With 160GB of storage, iPod classic is the take-everything-everywhere iPod. Available in Silver or Midnight", 249.89, false);

    Product prod4 = new Product("iPod Red", "With 160GB of storage, iPod [RED] PRODUCT lets other fashion victims know you aren't just some mass consumer status concious drone, your a drone with a concious because Bono said so! Available in red.", 349.89, false);

    Product prod5 = new Product("iTouch", "It's just like an iPhone accept that your parents obviously don't trust you with a phone yet.", 449.89, true);

【问题讨论】:

  • 编辑了以上内容以显示我如何存储/构建要打印的项目/类对象。

标签: c# class loops methods


【解决方案1】:

您可以尝试将您的实例存储到List

var list = new List<YourType> { prod1, prod2, prod3, prod4,prod5 };

然后遍历该列表:

int i = 1;
foreach(var prod in list)
{
   Console.WriteLine(i + ". " + prod.GetDetails() + "\n\n"); 
   i++;
}

如果你想为此定义一个方法,你可以这样定义:

public void DisplayOfferings(List<Product> products)
{ 
    int i = 1;
    foreach(var product in products)
    { 
        Console.WriteLine(i + ". " + product.GetDetails() + "\n\n"); 
        i++;
    }
}

然后首先创建您的列表并调用它:

var productList = new List<Product> { prod1, prod2, prod3, prod4,prod5 };
DisplayOfferings(productList);

其实你可以在这里使用params关键字,它会让你的工作更轻松。例如,如果你像这样改变你的方法定义:

public void DisplayOfferings(params Product[] products)

你可以在不定义列表的情况下调用它:

DisplayOfferings(prod1, prod2, prod3, prod4,prod5);

【讨论】:

  • 如果您必须使用变量来增加 (i),我宁愿使用经典的 for
  • 我尝试了这个,但我禁止了它。我像这样列出方法调用“DisplayOfferings(Product[]);”然后对方法“ public void DisplayOfferings(Product[] products) { var list = new List(Product[]) { prod1, prod2, prod3, prod4, prod5 }; int i = 1; foreach (var prod in list) { Console.WriteLine(i + ". " + prod.GetDetails() + "\n\n"); i++; } }".我做错了一些事情,不知道是什么。我是新手,但我很高兴看到即使我还不能做到这一点也可以做到
  • @Chezshire 首先创建您的列表,然后像这样传递您的方法:DisplayOfferings(productList); 并且您的列表定义错误,不要使用括号 List(Product[]) 而是使用 new List&lt;Product&gt;
  • 谢谢,看看这里的一切帮助我意识到将我的垃圾放入数组中,然后执行此操作“for (int i = 0; i
  • 但是今晚我会尝试你的建议:D 谢谢你学到了很多东西,这一直是我的目标
【解决方案2】:
public void DisplayOfferings(Product[] products)
{
    for (int i = 1; i <= products.length; i++ ) 
    {
      Console.WriteLine(i + ". " + products[i - 1].GetDetails() + "\n\n"); 
    }      
}

其中 products 是 prod1 ... prodN 的数组

【讨论】:

  • 你是说products[i - 1] 吗?
  • 感谢您的帮助 - 您以一些非常好的方式帮助我摆脱了困境。谢谢。
【解决方案3】:

这些单独的产品对象是怎么来的?我假设您正在创建这 5 个对象并测试 GetDetails() 方法?为了使其更简洁,您始终可以创建一个“产品”对象的数组/列表并调用如下所述的方法;再次没有您提供足够的数据,我无法弄清楚 1)您如何获得这些产品以及 2)您打算如何处理它们 :-) -->


class Program {
    static void Main(string[] args) {
        List<Product> products = new List<Product> 
                                        {   
                                            new Product("product 1"),
                                            new Product("product 2"),
                                            new Product("product 3"),
                                            new Product("product 4"),
                                            new Product("product 5")
                                        };
        /** do some more work here in this segment */

        // now call your GetDetails() method in a nicer Object Oriented (subjective) manner
        for(int i=0; i<5; i++) {
            Console.WriteLine("\"" + (i+1) + ".\"" + products[i].GetDetails() + "\n\n");
        }
        Console.ReadLine();
    }

    private class Product {
        public Product(string data) {
            Data = data;
        }

        public string Data { get; set; }

        public string GetDetails() {
            return "The initial data string is:\t" + Data;
        }
    }
}

【讨论】:

  • 我刚刚添加了存储对象的方式,希望我能正确理解您的要求。我是新手,仅供参考
  • 感谢您的帮助,我搞定了。现在我已经开始工作了,我将尝试所有其他想法 - 谢谢
【解决方案4】:

你总是可以走 LINQ 路径:

var list = new List<YourType>
{
    prod1, prod2, prod3, prod4, prod5
};

list
    .Select((prod, i) => String.Format("{0}. {1}\n\n", i, prod.GetDetails()))
    .ToList()
    .ForEach(Console.WriteLine);

【讨论】:

  • 我不知道 LINQ 是什么,但我想我知道我明天要研究什么 - 谢谢
猜你喜欢
  • 2020-09-17
  • 2014-10-24
  • 2011-07-25
  • 1970-01-01
  • 1970-01-01
  • 2010-09-21
  • 2013-06-16
  • 1970-01-01
相关资源
最近更新 更多