C# 使用数组的高分程序

C# 使用数组的高分程序,c#,arrays,class,C#,Arrays,Class,任务是让用户输入10组首字母和10组分数,并将它们存储在名为“Player”的数组中 下面我将展示我创建的代码 目前我的问题是,当我将数组打印到控制台窗口时,它只显示最后输入的分数和首字母 using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace HighScores { class Program { static void Main(string[]

任务是让用户输入10组首字母和10组分数,并将它们存储在名为“Player”的数组中

下面我将展示我创建的代码

目前我的问题是,当我将数组打印到控制台窗口时,它只显示最后输入的分数和首字母

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HighScores
{
class Program
{
    static void Main(string[] args)
    {
        string nInitials;

        int nScore;

        int counter = 0;

        do
        {
            Console.Write("Please enter your initials:");
            nInitials = Convert.ToString(Console.ReadLine());

            Console.Write("Please enter your score:");
            nScore = Convert.ToInt32(Console.ReadLine());

            counter++;
        }
        while (counter <= 2);

        for(int counter2 = 0; counter <= 2; counter2++)
        {
        Player[] myPlayerArray = new Player[3];

        Player[] myPlayer = 
           {
              new Player(nInitials, nScore)
           };

            foreach (var value in myPlayer)
                    {
                    Console.WriteLine("{0}", myPlayer[ counter2 ]);
                }
            }

#if DEBUG
        Console.ReadLine();
#endif

    }//end main

}//end class

public class Player
{

    public string initials { get; set; }

    public int score { get; set; }

    public Player(string pInitials, int pScore)
    {
        initials = pInitials;

        score = pScore;

    }

public override string  ToString()
    {
return string.Format("{0}, {1}", score, initials);
    }

}//end class Player
}//end namespace
我尝试了各种方法来让我的阵列存储10组数据,但我遇到了困难

问题1:数组中只能存储一组首字母和分数。需要10套

问题2:只打印一套,这是最后输入的一套

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace HighScores
{
class Program
{
    static void Main(string[] args)
    {
        string nInitials;

        int nScore;

        int counter = 0;

        do
        {
            Console.Write("Please enter your initials:");
            nInitials = Convert.ToString(Console.ReadLine());

            Console.Write("Please enter your score:");
            nScore = Convert.ToInt32(Console.ReadLine());

            counter++;
        }
        while (counter <= 2);

        for(int counter2 = 0; counter <= 2; counter2++)
        {
        Player[] myPlayerArray = new Player[3];

        Player[] myPlayer = 
           {
              new Player(nInitials, nScore)
           };

            foreach (var value in myPlayer)
                    {
                    Console.WriteLine("{0}", myPlayer[ counter2 ]);
                }
            }

#if DEBUG
        Console.ReadLine();
#endif

    }//end main

}//end class

public class Player
{

    public string initials { get; set; }

    public int score { get; set; }

    public Player(string pInitials, int pScore)
    {
        initials = pInitials;

        score = pScore;

    }

public override string  ToString()
    {
return string.Format("{0}, {1}", score, initials);
    }

}//end class Player
}//end namespace

从我在下面粘贴的代码片段中可以明显看出,无论何时从控制台读取另一个首字母缩写/分数集,都会丢弃以前的结果。您需要在nscore=行之后添加一行代码,创建一个新的player对象并将其存储在数组中,否则下次循环运行时将丢弃nInitials和nscore的值

    do
    {
        Console.Write("Please enter your initials:");
        nInitials = Convert.ToString(Console.ReadLine());

        Console.Write("Please enter your score:");
        nScore = Convert.ToInt32(Console.ReadLine());

        counter++;
    }
    while (counter <= 2);

在不需要的地方停止使用var。这不是它的目的。如果你的教授让你使用它,提醒他他不知道他在说什么,如果他仍然坚持,只有在你毕业之前才能使用它。@Shirik:在这段代码中单独使用var有什么不对?var的用途是什么?你能确定教授不知道他在说什么吗?你确定你知道你在说什么吗?@spender根据我的经验,在不需要var的地方使用var的人往往会频繁使用var。这是个问题。之所以需要var,是因为无法存储匿名类型。不过,编译器可以解决这个问题,因此var在这方面非常有用。但对于这里使用的这种情况,它只会使读取代码变得更加困难。像许多教授今天所教的那样,把它们放在文件的许多地方,所发生的一切就是学习如何编写不可读的代码。尽早捕获它是修复它的最简单方法。var适用于1。当你说不出类型的时候。2.当您信任编译器推断类型时。在原始代码中使用var不是第一种情况。学生们还没有与编译器建立起对用例2的信任。@Shirik:读一读Eric Lippert最近关于var的文章肯定是值得的。同意,他不是教授,但在这件事上没有更高的权威:@David我几乎在最后一个答案之前就知道了@user672117:我很感激这个解决方案,但我更希望得到指导。我不会提交我的作业,直到我完全理解这是如何工作的。我不会仅仅通过复制和粘贴来学习任何东西;再次感谢大家的快速反应!我将尽量避免过度使用var=X