没有for循环递增

时间:2017-04-10 15:00:07

标签: c# oop for-loop if-statement

我有以下代码,我想创建一些守门员对象,然后是一些Defender对象,然后是Midfielders等等。 我的目的是在创建每个对象后使用i整数增量,并使用if语句检查我的值,并相应地指定一个播放器类型。

目前,我将所有对象作为守门员对象返回,这意味着我没有递增。我有什么想法可以解决这个问题吗?

//create different types of player
int i = 0;


if (i < 2)
{
    i+=1;
    //create gk
    p = new Goalkeeper(fname, lname, saves, tackles, assists, goals, minutesplayed, fitness);
}
else if (i >= 2 && i < 9)
{
    i++;
    //create d
    p = new Defender(fname, lname, headerswon, tackles, assists, goals, minutesplayed, fitness);
}
else if (i >= 9 && i < 16)
{
    i++;
    //crete mf
    p = new Midfielder(fname, lname, passsuccessrate, tackles, assists, goals, minutesplayed, fitness);
}
else
{
    i++;
    //create striker
    p = new Striker(fname, lname, shotsontarget, tackles, assists, goals, minutesplayed, fitness);
}

return p; //return the randomly generated player

3 个答案:

答案 0 :(得分:5)

您需要一个循环,否则整个if分支结构只会被检查一次。

你为什么厌恶厌恶?有了一个循环,它很简单:

List<Player> players = new List<Player>();

for (int i = 0; i < numPlayers; ++i)
{
    Player p;

    if (i < 2)
        p = new GoalKeeper(...);
    else if (i < 9)
        p = new Defender(...);
    else if (i < 16)
        p = new Midfielder(...);
    else
        p = new Striker(...);

    players.Add(p);
}

答案 1 :(得分:0)

让我静止

//创建不同类型的播放器

static int i = 0;

            if (i < 2)
            {
            i+=1;
            //create gk
            p = new Goalkeeper(fname, lname, saves, tackles, assists, goals, minutesplayed, fitness);

        }
            else if (i >= 2 && i < 9)
            {
            i++;
            //create d
            p = new Defender(fname, lname, headerswon, tackles, assists, goals, minutesplayed, fitness);

            }
            else if (i >= 9 && i < 16)
            {
            i++;
            //crete mf
            p = new Midfielder(fname, lname, passsuccessrate, tackles, assists, goals, minutesplayed, fitness);

        }
            else
            {
            i++;
            //create striker
            p = new Striker(fname, lname, shotsontarget, tackles, assists, goals, minutesplayed, fitness);

        }



            return p; //return the randomly generated player

答案 2 :(得分:0)

解决:我从调用方法传递一个int。谢谢大家!

private Player GetRandomPlayer(int count)
            {

                    Player p; //create new player

                //calculate various stats using random
                int tackles = rand.Next(0, 20);
                int assists = rand.Next(0, 3);
                int goals = rand.Next(0, 3);
                int  minutesplayed = rand.Next(0, 90);
                int saves = rand.Next(0,30);
                int headerswon = rand.Next(0, 20);
                int passsuccessrate = rand.Next(0, 100);
                int shotsontarget = rand.Next(0, 25);
                string fitness = "Fit";

                //create random player names from the names data declared earlier
                string fname = firstnames[rand.Next(0, firstnames.Length)];
                string lname = lastnames[rand.Next(0, lastnames.Length)];

                //2gk, 7d,7m,4 striker

                //create different types of player





                    if (count < 2)
                    {

                        //create gk
                        p = new Goalkeeper(fname, lname, saves, tackles, assists, goals, minutesplayed, fitness);

                    }
                    else if (count < 9)
                    {

                        //create d
                        p = new Defender(fname, lname, headerswon, tackles, assists, goals, minutesplayed, fitness);

                    }
                    else if (count < 16)
                    {

                        //crete mf
                        p = new Midfielder(fname, lname, passsuccessrate, tackles, assists, goals, minutesplayed, fitness);

                    }
                    else
                    {

                        //create striker
                        p = new Striker(fname, lname, shotsontarget, tackles, assists, goals, minutesplayed, fitness);

                    }



                    return p; //return the randomly generated player





        }