Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 用多个变量c填充数组#_C#_.net_Arrays - Fatal编程技术网

C# 用多个变量c填充数组#

C# 用多个变量c填充数组#,c#,.net,arrays,C#,.net,Arrays,我试图找出如何用一个具有多个变量的对象填充数组。我需要的是创建一个数组,而不是一个列表(我正在尝试学习数组),并用5种不同的波旁威士忌填充它。是否可以填充数组并将名称、年龄、蒸馏厂存储在一个索引中?比如说, 如果调用索引0,它将显示: Name: Black Maple Hill Distillery: CVI Brands, Inc Age: 8 years 到目前为止,我已经有了它,其中bourbon是来自Whisky的派生类,并在主类中调用一个方法来提示用户输入 class Bourbo

我试图找出如何用一个具有多个变量的对象填充数组。我需要的是创建一个数组,而不是一个列表(我正在尝试学习数组),并用5种不同的波旁威士忌填充它。是否可以填充数组并将名称、年龄、蒸馏厂存储在一个索引中?比如说,

如果调用索引0,它将显示:

Name: Black Maple Hill
Distillery: CVI Brands, Inc
Age: 8 years
到目前为止,我已经有了它,其中bourbon是来自Whisky的派生类,并在主类中调用一个方法来提示用户输入

class Bourbon : Whiskey
{
    private Bourbon[] bBottles = new Bourbon[5];

    public void bourbon(string name, string distillery, int age)
    {
        Name = name;
        Distillery = distillery;
        Age = age;
     }

    public void PopulateBottles()
    {
         Console.WriteLine("Please enter the information for 5 bourbons:");
         for (int runs = 0; runs < 5; runs ++)
         {

         }
     }
}
class波旁威士忌:威士忌
{
私人波旁威士忌[]b瓶=新波旁威士忌[5];
public void bourbon(字符串名称、字符串蒸馏厂、整数年龄)
{
名称=名称;
蒸馏厂=蒸馏厂;
年龄=年龄;
}
公共空间人口瓶()
{
Console.WriteLine(“请输入5波旁威士忌的信息:”);
对于(int运行=0;运行<5;运行++)
{
}
}
}

在代码中,您没有定义在for循环中使用的
变量。您可以创建该类的新实例,然后将它们存储在数组中:

public void PopulateBottles()
{
    Console.WriteLine("Please enter the information for 5 bourbons:");
    for (int runs = 0; runs < 5; runs ++)
    {
        Console.WriteLine("Name:");
        var name = Console.ReadLine();
        Console.WriteLine("Distillery:");
        var distillery = Console.ReadLine();
        Console.WriteLine("Age:");
        var age = int.Parse(Console.ReadLine());
        var bourbon = new Bourbon(name, distillery, age);
        bBottles[runs] = bourbon;
    }
}

@乔纳森。是的,根据我的解释,这是可能的。您可以尝试使用索引器

Class Bourbon : Whiskey {
 public Bourbon this[int index]
 {
     get {
         return bBottles[index];
     }
     set {
         bBottles[index] = value;
     }  
    }  
}

选中此项需要创建属性和一个构造函数来实现您的要求

class Bourbon
    {

        private Bourbon[] bBottles = new Bourbon[5];

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string distillery;

        public string Distillery
        {
            get { return distillery; }
            set { distillery = value; }
        }
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public Bourbon(string name, string distillery, int age)
        {
            Name = name;
            Distillery = distillery;
            Age = age;
        }

        public void PopulateBottles()
        {
            Console.WriteLine("Please enter the information for 5 bourbons:");
            for (int runs = 0; runs < 5; runs++)
            {
                Console.WriteLine("Name:");
                var name = Console.ReadLine();
                Console.WriteLine("Distillery:");
                var distillery = Console.ReadLine();
                Console.WriteLine("Age:");
                var age = int.Parse(Console.ReadLine());
                var bourbon = new Bourbon(name, distillery, age);
                bBottles[runs] = bourbon;
            }
        }
    }
class波旁威士忌
{
私人波旁威士忌[]b瓶=新波旁威士忌[5];
私有字符串名称;
公共字符串名
{
获取{返回名称;}
设置{name=value;}
}
私人串酒厂;
公用串酒厂
{
获取{返回酿酒厂;}
设置{蒸馏厂=值;}
}
私人互联网;
公共信息
{
获取{返回年龄;}
设置{age=value;}
}
公共波旁威士忌(品名、品酒厂、年份)
{
名称=名称;
蒸馏厂=蒸馏厂;
年龄=年龄;
}
公共空间人口瓶()
{
Console.WriteLine(“请输入5波旁威士忌的信息:”);
对于(int运行=0;运行<5;运行++)
{
Console.WriteLine(“名称:”);
var name=Console.ReadLine();
控制台。WriteLine(“酿酒厂:”);
var destillery=Console.ReadLine();
控制台。WriteLine(“年龄:”);
var age=int.Parse(Console.ReadLine());
var bourbon=新波旁威士忌(名称、酿酒厂、年份);
bBottles[跑步]=波旁威士忌;
}
}
}

伙计,你需要学习的第一件事就是用清晰的方式表达你的想法!把问题分成几个部分。为那些愿意回答的人提供一些背景。这闻起来像是家庭作业。你的直觉被误导了。我的家庭作业是制作DVD租赁程序。这是我在尝试理解数组。这个问题似乎明确地说明了我在寻找什么。语法相当准确,我的观点是按逻辑顺序介绍的。也许我没有用语义上合理的方式表达自己,尽管我试图学习C#的语义。但请允许我尽可能清楚地说:我想知道是否可以在数组中存储bourbon对象。这些行应该做什么?Name=Console.ReadLine();bBottles[runs]=value此操作如何处理创建OP代码中缺少的新对象?@ryadavilli。这是公布的部分代码。必须编写解析器并构造Bourbon类型的对象。解析器,它从控制台读取。我了解您所做的。我问这个问题是为了得到你更完整的答案。最好使用带有代码和解释的自解释答案。
class Bourbon
    {

        private Bourbon[] bBottles = new Bourbon[5];

        private string name;

        public string Name
        {
            get { return name; }
            set { name = value; }
        }
        private string distillery;

        public string Distillery
        {
            get { return distillery; }
            set { distillery = value; }
        }
        private int age;

        public int Age
        {
            get { return age; }
            set { age = value; }
        }

        public Bourbon(string name, string distillery, int age)
        {
            Name = name;
            Distillery = distillery;
            Age = age;
        }

        public void PopulateBottles()
        {
            Console.WriteLine("Please enter the information for 5 bourbons:");
            for (int runs = 0; runs < 5; runs++)
            {
                Console.WriteLine("Name:");
                var name = Console.ReadLine();
                Console.WriteLine("Distillery:");
                var distillery = Console.ReadLine();
                Console.WriteLine("Age:");
                var age = int.Parse(Console.ReadLine());
                var bourbon = new Bourbon(name, distillery, age);
                bBottles[runs] = bourbon;
            }
        }
    }