一个类的多个实例化取决于用户输入

时间:2018-12-28 18:02:00

标签: c#

我正在创建一个宾果游戏,其中有带有不同变量的“ Player”类。我想问用户他/她想要多少个玩家,并根据输入内容创建所需的实例化实例,但是我不知道该怎么做。这是我的代码:

int players = int.Parse(Console.ReadLine());
for(int i = 0; i <= players; i++)
    {
        Player player +1  = new Player((int[5, 3]), "Player " + i);
    } 

预期的结果是能够实例化一个类的次数如变量所述。

编辑:这是Player类:

public class Player
    {
        private int[,] playerTicket;
        private int ticketId;
        private string name;

        public int[,] PlayerTicket
        {
            get { return playerTicket; }
            set { playerTicket = value; }
        }
        public int TicketId
        {
            get { return ticketId; }
            set { ticketId = value; }
        }
        public string Name
        {
            get { return Name; }
            set { Name = value; }
        }

        public Player(int[,] playerTicket, int ticketId, string name)
        {
            this.playerTicket = PlayerTicket;
            this.ticketId = TicketId;
            this.name = Name;
        }
    }

2 个答案:

答案 0 :(得分:1)

//You should be setting your class fields to the ones passed into the constructor
public Player(int[,] playerTicket, int ticketId, string name)
{
            this.playerTicket = playerTicket;
            this.ticketId = ticketId;
            this.name = name;
}

编辑您的Player类:

要么添加一个接受两个参数的构造函数,要么确保为构造函数的第二个参数传递一个整数:

<div id="dados-ordem-tabela" class="alphaBorda1 settingsConf shadow-box ativa-display-dialog">
<div class="titulo alphaBackground3">
    <a>Dados de Ordem</a>
</div>
<div class="menuSt">
    <ul class="settingsConf-ul">
        <li id="aba-dados-ordem" tab=".dados-ordem" class="mouseoverAlpha5 settingsConf-li tippy-cursor-point pri aq">Ordens</li>
        <li id="aba-dados-ordem-oms" tab=".dados-ordem-oms" class="mouseoverAlpha5 settingsConf-li tippy-cursor-point pri">Stop</li>
    </ul>
</div>
<div class="dados-ordem style-tabela style-tabela-ordens menuBck" style="">
    <table>
        <thead class="cor-fundo-principal">
            <tr>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="CANCELAR">CANCELAR</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="C/V">C/V</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="PAPEL">PAPEL</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="QTD.">QTD.</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="PREÇO">PREÇO</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="STATUS">STATUS <span class="mouseoverAlpha5 icon-imgIntervalo"></span></th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="QTD. EX.">QTD. EX.</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="PRÇ. MED.">PRÇ. MED.</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="ORIGEM">ORIGEM</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="DATA DE ENVIO">DATA DE ENVIO</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="TIPO">TIPO</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="VALIDADE">VALIDADE</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="CÓD. CLI.">CÓD. CLI.</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="ULT. ALT.">ULT. ALT.</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="QTD. APARENTE">QTD. APARENTE</th>
                <th class="cor-fundo-principal alphaborda1" valuetocsv="SALDO">SALDO</th>
            </tr>
        </thead>
        <tbody>
            <tr class="alphaBackground3" style="background-color: rgba(0, 240, 120, 0.05);">
                <td valuetocsv="<span class='mouseoverAlpha5 icon-imgRemove cancela-ordem' value='4073684' ></span>"
                    class="text-center"><span class="mouseoverAlpha5 icon-imgRemove cancela-ordem" value="4073684"></span></td>
                <td valuetocsv="C" class="text-center CV">C</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.055,00" class="text-right">87.055,00</td>
                <td valuetocsv="<span class='aberto'>Aberto</span>" class="text-left"><span class="aberto">Aberto</span></td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:28" class="">28/12/2018 11:30:28</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:28:117" class="">11:30:28:117</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="1" class="">1</td>
            </tr>
            <tr class="alphaBackground5" style="background-color: rgba(0, 240, 120, 0.05);">
                <td valuetocsv="<span class='mouseoverAlpha5 icon-imgRemove cancela-ordem' value='4073683' ></span>"
                    class="text-center"><span class="mouseoverAlpha5 icon-imgRemove cancela-ordem" value="4073683"></span></td>
                <td valuetocsv="C" class="text-center CV">C</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.080,00" class="text-right">87.080,00</td>
                <td valuetocsv="<span class='aberto'>Aberto</span>" class="text-left"><span class="aberto">Aberto</span></td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:27" class="">28/12/2018 11:30:27</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:27:970" class="">11:30:27:970</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="1" class="">1</td>
            </tr>
            <tr class="alphaBackground3" style="background-color: rgba(0, 240, 120, 0.05);">
                <td valuetocsv="<span class='mouseoverAlpha5 icon-imgRemove cancela-ordem' value='4073682' ></span>"
                    class="text-center"><span class="mouseoverAlpha5 icon-imgRemove cancela-ordem" value="4073682"></span></td>
                <td valuetocsv="C" class="text-center CV">C</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.135,00" class="text-right">87.135,00</td>
                <td valuetocsv="<span class='aberto'>Aberto</span>" class="text-left"><span class="aberto">Aberto</span></td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:27" class="">28/12/2018 11:30:27</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:27:830" class="">11:30:27:830</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="1" class="">1</td>
            </tr>
            <tr class="alphaBackground5" style="background-color: rgba(0, 240, 120, 0.05);">
                <td valuetocsv="-" class="text-center"> - </td>
                <td valuetocsv="C" class="text-center CV">C</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.205,00" class="text-right">87.205,00</td>
                <td valuetocsv="<span class='executada'>Executada</span>" class="text-left"><span class="executada">Executada</span></td>
                <td valuetocsv="1" class="">1</td>
                <td valuetocsv="87.205,0000" class="">87.205,0000</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:27" class="">28/12/2018 11:30:27</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:36:34:917" class="">11:36:34:917</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="0" class="">0</td>
            </tr>
            <tr class="alphaBackground3" style="background-color: rgba(252, 83, 83, 0.05);">
                <td valuetocsv="-" class="text-center"> - </td>
                <td valuetocsv="V" class="text-center CV">V</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.290,00" class="text-right">87.290,00</td>
                <td valuetocsv="<span class='executada'>Executada</span>" class="text-left"><span class="executada">Executada</span></td>
                <td valuetocsv="1" class="">1</td>
                <td valuetocsv="87.305,0000" class="">87.305,0000</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:27" class="">28/12/2018 11:30:27</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:28:300" class="">11:30:28:300</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="0" class="">0</td>
            </tr>
            <tr class="alphaBackground5" style="background-color: rgba(252, 83, 83, 0.05);">
                <td valuetocsv="-" class="text-center"> - </td>
                <td valuetocsv="V" class="text-center CV">V</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.335,00" class="text-right">87.335,00</td>
                <td valuetocsv="<span class='executada'>Executada</span>" class="text-left"><span class="executada">Executada</span></td>
                <td valuetocsv="1" class="">1</td>
                <td valuetocsv="87.335,0000" class="">87.335,0000</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:27" class="">28/12/2018 11:30:27</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:49:663" class="">11:30:49:663</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="0" class="">0</td>
            </tr>
            <tr class="alphaBackground3" style="background-color: rgba(252, 83, 83, 0.05);">
                <td valuetocsv="-" class="text-center"> - </td>
                <td valuetocsv="V" class="text-center CV">V</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.320,00" class="text-right">87.320,00</td>
                <td valuetocsv="<span class='executada'>Executada</span>" class="text-left"><span class="executada">Executada</span></td>
                <td valuetocsv="1" class="">1</td>
                <td valuetocsv="87.320,0000" class="">87.320,0000</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:27" class="">28/12/2018 11:30:27</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:34:423" class="">11:30:34:423</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="0" class="">0</td>
            </tr>
            <tr class="alphaBackground5" style="background-color: rgba(252, 83, 83, 0.05);">
                <td valuetocsv="-" class="text-center"> - </td>
                <td valuetocsv="V" class="text-center CV">V</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.290,00" class="text-right">87.290,00</td>
                <td valuetocsv="<span class='executada'>Executada</span>" class="text-left"><span class="executada">Executada</span></td>
                <td valuetocsv="1" class="">1</td>
                <td valuetocsv="87.305,0000" class="">87.305,0000</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:27" class="">28/12/2018 11:30:27</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:28:300" class="">11:30:28:300</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="0" class="">0</td>
            </tr>
            <tr class="alphaBackground3" style="background-color: rgba(252, 83, 83, 0.05);">
                <td valuetocsv="-" class="text-center"> - </td>
                <td valuetocsv="V" class="text-center CV">V</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.240,00" class="text-right">87.240,00</td>
                <td valuetocsv="<span class='executada'>Executada</span>" class="text-left"><span class="executada">Executada</span></td>
                <td valuetocsv="1" class="">1</td>
                <td valuetocsv="87.255,0000" class="">87.255,0000</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:26" class="">28/12/2018 11:30:26</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:35:35:683" class="">11:35:35:683</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="0" class="">0</td>
            </tr>
            <tr class="alphaBackground5" style="background-color: rgba(252, 83, 83, 0.05);">
                <td valuetocsv="-" class="text-center"> - </td>
                <td valuetocsv="V" class="text-center CV">V</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.185,00" class="text-right">87.185,00</td>
                <td valuetocsv="<span class='executada'>Executada</span>" class="text-left"><span class="executada">Executada</span></td>
                <td valuetocsv="1" class="">1</td>
                <td valuetocsv="87.205,0000" class="">87.205,0000</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:26" class="">28/12/2018 11:30:26</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:41:48:920" class="">11:41:48:920</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="0" class="">0</td>
            </tr>
            <tr class="alphaBackground3" style="background-color: rgba(252, 83, 83, 0.05);">
                <td valuetocsv="<span class='mouseoverAlpha5 icon-imgRemove cancela-ordem' value='4073674' ></span>"
                    class="text-center"><span class="mouseoverAlpha5 icon-imgRemove cancela-ordem" value="4073674"></span></td>
                <td valuetocsv="V" class="text-center CV">V</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.140,00" class="text-right">87.140,00</td>
                <td valuetocsv="<span class='aberto'>Aberto</span>" class="text-left"><span class="aberto">Aberto</span></td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:26" class="">28/12/2018 11:30:26</td>
                <td valuetocsv="STP" class="text-center">STP</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:26:583" class="">11:30:26:583</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="1" class="">1</td>
            </tr>
            <tr class="alphaBackground5" style="background-color: rgba(252, 83, 83, 0.05);">
                <td valuetocsv="<span class='mouseoverAlpha5 icon-imgRemove cancela-ordem' value='4073673' ></span>"
                    class="text-center"><span class="mouseoverAlpha5 icon-imgRemove cancela-ordem" value="4073673"></span></td>
                <td valuetocsv="V" class="text-center CV">V</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.110,00" class="text-right">87.110,00</td>
                <td valuetocsv="<span class='aberto'>Aberto</span>" class="text-left"><span class="aberto">Aberto</span></td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:26" class="">28/12/2018 11:30:26</td>
                <td valuetocsv="STP" class="text-center">STP</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:26:517" class="">11:30:26:517</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="1" class="">1</td>
            </tr>
            <tr class="alphaBackground3" style="background-color: rgba(252, 83, 83, 0.05);">
                <td valuetocsv="<span class='mouseoverAlpha5 icon-imgRemove cancela-ordem' value='4073672' ></span>"
                    class="text-center"><span class="mouseoverAlpha5 icon-imgRemove cancela-ordem" value="4073672"></span></td>
                <td valuetocsv="V" class="text-center CV">V</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="87.055,00" class="text-right">87.055,00</td>
                <td valuetocsv="<span class='aberto'>Aberto</span>" class="text-left"><span class="aberto">Aberto</span></td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:26" class="">28/12/2018 11:30:26</td>
                <td valuetocsv="STP" class="text-center">STP</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:26:300" class="">11:30:26:300</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="1" class="">1</td>
            </tr>
            <tr class="alphaBackground5" style="background-color: rgba(0, 240, 120, 0.05);">
                <td valuetocsv="<span class='mouseoverAlpha5 icon-imgRemove cancela-ordem' value='4073671' ></span>"
                    class="text-center"><span class="mouseoverAlpha5 icon-imgRemove cancela-ordem" value="4073671"></span></td>
                <td valuetocsv="C" class="text-center CV">C</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="86.950,00" class="text-right">86.950,00</td>
                <td valuetocsv="<span class='aberto'>Aberto</span>" class="text-left"><span class="aberto">Aberto</span></td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:26" class="">28/12/2018 11:30:26</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:26:210" class="">11:30:26:210</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="1" class="">1</td>
            </tr>
            <tr class="alphaBackground3" style="background-color: rgba(0, 240, 120, 0.05);">
                <td valuetocsv="<span class='mouseoverAlpha5 icon-imgRemove cancela-ordem' value='4073670' ></span>"
                    class="text-center"><span class="mouseoverAlpha5 icon-imgRemove cancela-ordem" value="4073670"></span></td>
                <td valuetocsv="C" class="text-center CV">C</td>
                <td valuetocsv="WING19" class="text-center">WING19</td>
                <td valuetocsv="1" class="text-right">1</td>
                <td valuetocsv="86.945,00" class="text-right">86.945,00</td>
                <td valuetocsv="<span class='aberto'>Aberto</span>" class="text-left"><span class="aberto">Aberto</span></td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="0" class="">0</td>
                <td valuetocsv="WEB" class="text-center">WEB</td>
                <td valuetocsv="28/12/2018 11:30:26" class="">28/12/2018 11:30:26</td>
                <td valuetocsv="DIA" class="text-center">DIA</td>
                <td valuetocsv="28/12/2018" class="">28/12/2018</td>
                <td valuetocsv="7332" class="">7332</td>
                <td valuetocsv="11:30:26:010" class="">11:30:26:010</td>
                <td valuetocsv="-" class=""> - </td>
                <td valuetocsv="1" class="">1</td>
            </tr>





        </tbody>
    </table>
</div>

<div class="footer">
    <button class="alphaBackground3 alphaBorda1 button bt-atualizar" type="button">Atualizar</button>
    <button class="alphaBackground3 alphaBorda1 button bt-cancelar" type="button">Fechar</button>
</div>

答案 1 :(得分:0)

我认为您应该使用List和.Add(T)方法。例如:

int players = int.Parse(Console.ReadLine());
List<Player> activePlayers = new List<Player>();
for(int i = 0; i <= players; i++)
{
    activePlayers.Add(new Player((int[5, 3]), "Player " + i));
}