使用数组的高分计划

时间:2011-07-03 01:26:16

标签: c# arrays class

分配是让用户输入10组首字母和10组分数,并将它们存储在名为“播放器”的数组中。

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

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

我尝试了各种选项让我的数组存储了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

2 个答案:

答案 0 :(得分:0)

从我下面粘贴的代码片段中,很明显,无论何时从控制台读取其他首字母/得分集,您都会丢弃之前的结果。您需要在创建新播放器对象的nscore =行之后添加一行代码并将其存储在数组中,否则nInitialsnScore的值将在下次被丢弃循环运行。

    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);

答案 1 :(得分:0)

namespace eheeeeeeeeeeeee
{
    class Program
    {
        static void Main(string[] args)
        {
            Player[] players=new Player[10];
            for (int i = 0; i < 10; i++)
            {
                string tempName;
                int tempScore;

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

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

                players[i]=new Player(tempName,tempScore);

            }

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine(players[i].ToString());
            }
            Console.ReadLine();
        }
    }


    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);
        }
    }
}