高分程序使用数组

本文关键字:数组 程序 | 更新日期: 2023-09-27 18:01:15

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

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

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

我已经尝试了各种选择来让我的数组存储十组,但我遇到了困难。

问题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 =行之后添加一行代码,以创建新的播放器对象并将其存储在您的数组中,否则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);
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);
        }
    }
}