C# c语言中的对象数组#

C# c语言中的对象数组#,c#,arrays,C#,Arrays,在调试后创建存储对象到数组并用c#打印时,我遇到了几个问题。我这里有什么问题?将对象添加到数组并打印对象标题时开始出现问题 static void Main(string[] args) { ComputerGame cg1 = new ComputerGame("Age of Empires",49.99); Console.WriteLine(cg1.title); ComputerGame cg2 = new ComputerGam

在调试后创建存储对象到数组并用c#打印时,我遇到了几个问题。我这里有什么问题?将对象添加到数组并打印对象标题时开始出现问题

static void Main(string[] args)
    {
        ComputerGame cg1 = new ComputerGame("Age of Empires",49.99);
        Console.WriteLine(cg1.title);

        ComputerGame cg2 = new ComputerGame("Heroes and Generals", 30.00);
        ComputerGame cg3 = new ComputerGame("Team Fortress 2", 19.50);
        ComputerGame[] gameAlbum = new ComputerGame[5];
        for (int i = 0; i < 5;i++)
        {
            gameAlbum[0] = new ComputerGame();
            gameAlbum[1] = new ComputerGame();
            gameAlbum[2] = new ComputerGame();
        }
        foreach(ComputerGame o in gameAlbum)
        {
            Console.WriteLine(o.title);
        }
    }

public class ComputerGame
{
    public string title;
    public double price;
    public ComputerGame(string title, double price)
    {
        this.title = title;
        this.price = price;

    }
}
static void Main(字符串[]args)
{
电脑游戏cg1=新电脑游戏(“帝国时代”,49.99);
Console.WriteLine(cg1.title);
电脑游戏cg2=新电脑游戏(“英雄与将军”,30.00);
ComputerGame cg3=新的ComputerGame(“团队堡垒2”,19.50);
ComputerGame[]gameAlbum=新电脑游戏[5];
对于(int i=0;i<5;i++)
{
gameAlbum[0]=新的ComputerGame();
gameAlbum[1]=新电脑游戏();
gameAlbum[2]=新电脑游戏();
}
foreach(游戏相册中的计算机游戏o)
{
控制台写入线(o.标题);
}
}
公开课电脑游戏
{
公共字符串标题;
公开双价;
公共电脑游戏(字符串标题,双倍价格)
{
this.title=标题;
这个价格=价格;
}
}

游戏相册0、1和2是使用无参数构造函数创建的,但您需要两个参数

尝试向类中添加另一个构造函数,或在新语句中包含参数

顺便说一句,没有理由循环5次,因为循环总是做同样的事情。

试试看:

ComputerGame cg1 = new ComputerGame("Age of Empires",49.99);
ComputerGame cg2 = new ComputerGame("Heroes and Generals", 30.00);
ComputerGame cg3 = new ComputerGame("Team Fortress 2", 19.50);

ComputerGame[] gameAlbum = new ComputerGame[5];
gameAlbum[0] = cg1;
gameAlbum[1] = cg2;
gameAlbum[2] = cg3;

foreach(ComputerGame o in gameAlbum)
{
    if (o != null)
        Console.WriteLine(o.title);
}
改为这样做:

static void Main(string[] args)
{
    ComputerGame cg1 = new ComputerGame("Age of Empires",49.99);
    Console.WriteLine(cg1.title);

    ComputerGame cg2 = new ComputerGame("Heroes and Generals", 30.00);
    ComputerGame cg3 = new ComputerGame("Team Fortress 2", 19.50);
    ComputerGame[] gameAlbum = new ComputerGame[5];

    gameAlbum[0] = cg1;
    gameAlbum[1] = cg2;
    gameAlbum[2] = cg3;

    foreach(ComputerGame o in gameAlbum)
    {
        if (o != null)
           Console.WriteLine(o.title);
    }

    double total = gameAlbum.Where(g => g != null).Sum(g => g.price);
}
使用列表而不是数组的更简单方法:

List<ComputerGame> games = new List<ComputerGame>();
games.Add(new ComputerGame("Age of Empires", 49.99));
games.Add(new ComputerGame("Heroes and Generals", 30.00));
games.Add(new ComputerGame("Team Fortress 2", 19.50));
games.Add(new ComputerGame("Portal", 19.50));
games.Add(new ComputerGame("Portal 2", 29.50));

foreach(ComputerGame game in games)
{
    if (game != null)
        Console.WriteLine($"Title: {game.title}, Price: {game.price}");
}

double total = games.Sum(p => p.price);
List games=newlist();
添加(新电脑游戏(“帝国时代”,49.99));
游戏。添加(新电脑游戏(“英雄与将军”,30.00));
游戏。添加(新电脑游戏(“团队堡垒2”,19.50));
添加(新电脑游戏(“门户”),19.50);
添加(新电脑游戏(“门户2”,29.50));
foreach(游戏中的电脑游戏)
{
如果(游戏!=null)
Console.WriteLine($“Title:{game.Title},Price:{game.Price}”);
}
双倍合计=游戏总数(p=>p.price);
ComputerGame[]gameAlbum=新电脑游戏[5];
对于(int i=0;i<5;i++)
{
gameAlbum[0]=新的ComputerGame();
gameAlbum[1]=新电脑游戏();
gameAlbum[2]=新电脑游戏();
}

在for循环中,您一次又一次地初始化数组的前三个元素。您还调用了
ComputerGame
类的默认构造函数。因此,
ComputerGame
title
元素不会被初始化。所以您不会在控制台上看到任何打印内容。

代码的问题是ComputerGame的默认构造函数被覆盖。没有构造函数,例如:

public ComputerGame()
{...
}
因此,您可以执行以下操作:

gameAlbum[0] = new ComputerGame("Age of Empires",49.99);
gameAlbum[1] = new ComputerGame("Heroes and Generals", 30.00);
gameAlbum[2] = new ComputerGame("Team Fortress 2", 19.50);

当您仅使用5个数组元素中的3个时,也可能会出现错误。因此,在需要时使用列表而不是数组来动态创建对象

在for循环中,我没有看到对数组“gameAlbum”的任何分配。
试试下面的一个,看看

static void Main(string[] args)
{
    ComputerGame[] gameAlbum = new ComputerGame[3];

    gameAlbum[0] = new ComputerGame("Age of Empires",49.99);
    gameAlbum[1] = new ComputerGame("Heroes and Generals", 30.00);
    gameAlbum[2] = new ComputerGame("Team Fortress 2", 19.50);

    foreach(ComputerGame o in gameAlbum)
    {
       Console.WriteLine(o.title);
    }
}

public class ComputerGame
{
    public string title;
    public double price;
    public ComputerGame(string title, double price)
    {
        this.title = title;
        this.price = price;

    }
}

拆下循环。像您那样使用参数创建实例。将其签名到gameObjs[0]=cg1等数组。

尝试使用此代码

循环数据并获取价格的总和

         ComputerGame[] gameAlbum = new ComputerGame[5];
        gameAlbum[0] = new ComputerGame("Age of Empires", 49.99);
        gameAlbum[1] = new ComputerGame("Heroes and Generals", 30.00);
        gameAlbum[2] = new ComputerGame("Team Fortress 2", 19.50);
        gameAlbum[3] = new ComputerGame("Portal", 19.50);
        gameAlbum[4] = new ComputerGame("Portal 2", 29.50);

        //looping the data
        foreach (ComputerGame item in gameAlbum)
        {
            if (item != null)
                Response.Write("Name : " + item.title + " Price : " + item.price);
        }
        //get the sum of the price
        double total = gameAlbum.Sum(p => p.price);
        Response.Write(total);

代码中有什么问题?您没有将cg1、cg2和cg3放入数组对象GameAlgum。通过阅读,我认为当你运行你的代码时,你会得到一个空指针错误。你正确地创建了对象,但是你用forloop中的默认构造函数重新创建了游戏对象,这意味着一个类的所有成员都是空的。一个接一个地逐个签名和创建实例,就像在looplets说我想在对象中显示总价之前一样。我该怎么做?为此,我会问一个单独的StackOverflow问题:-)@J.Koh以获取计算机总数。试试这个方式。。双倍合计=游戏总数(p=>p.price)@在我添加double total=gameAlbum.Sum(p=>p.price)之后,它抛出一个空指针引用@我添加了新的答案…检查是否可以只使用数组而不是列表?你的意思是代替列表?是否将其设置为数组?原始代码使用数组对象。该列表只是@gotnull的建议。我将代码更新为array。。如果这是有用的,请将此标记为答案
         ComputerGame[] gameAlbum = new ComputerGame[5];
        gameAlbum[0] = new ComputerGame("Age of Empires", 49.99);
        gameAlbum[1] = new ComputerGame("Heroes and Generals", 30.00);
        gameAlbum[2] = new ComputerGame("Team Fortress 2", 19.50);
        gameAlbum[3] = new ComputerGame("Portal", 19.50);
        gameAlbum[4] = new ComputerGame("Portal 2", 29.50);

        //looping the data
        foreach (ComputerGame item in gameAlbum)
        {
            if (item != null)
                Response.Write("Name : " + item.title + " Price : " + item.price);
        }
        //get the sum of the price
        double total = gameAlbum.Sum(p => p.price);
        Response.Write(total);