【问题标题】:Code execution stops after for loop?for循环后代码执行停止?
【发布时间】:2017-01-16 14:51:49
【问题描述】:

所以我正在尝试在 C# 中制作一个使用英雄联盟 API 的程序。

我发现了一个 NuGet 包,它使 API 的使用变得更加容易。

到目前为止一切正常,除了代码执行在 我使用的第一个 for 循环。

代码如下:(当然是我取出了api密钥)

string[] summnames;
long[] champids;
long[] teamids;
long[] champs;

CreepScoreAPI.ParticipantLive[] enemy;
CreepScoreAPI.ParticipantLive[] ally;
CreepScoreAPI.ParticipantLive centsumm;
CreepScoreAPI.ParticipantLive[] champsss;
CreepScoreAPI.ChampionStatic[] champions;
CreepScoreAPI.Summoner[] sumners;
CreepScoreAPI.League[] leaguesz;

Dictionary<string, List<CreepScoreAPI.League>>[] leagues;

int[] champidsint;
string[] champnames;

int s;
int se;

public async Task<string> game(string summname)
{
    string data;

    CreepScoreAPI.CreepScore cs = new CreepScoreAPI.CreepScore("api key");

    var summoner = await cs.RetrieveSummoner(CreepScoreAPI.CreepScore.Region.EUNE, summname);
    long summid = summoner.id;
    var thegame = await summoner.RetrieveCurrentGameInfo();

    CreepScoreAPI.ParticipantLive[] participants = thegame.participants.ToArray();

    for (int i = 0; i <= 9; i++) { summnames[i] = participants[i].summonerName;}

    for (int i = 0; i <= 9; i++) { champids[i] = participants[i].championId;}

    for (int i = 0; i <= 9; i++) { teamids[i] = participants[i].teamIdLong;}

    for (int i = 0; i <= 9; i++) { champids[i] = participants[i].championId;}

    for (int i = 0; i <= 9; i++) { champidsint[i] = Convert.ToInt32(champids[i]);}

    for (int i = 0; i <= 9; i++) { champions[i] = await cs.RetrieveChampionData( CreepScoreAPI.CreepScore.Region.EUNE, champidsint[i], CreepScoreAPI.Constants.StaticDataConstants.ChampData.All, "en_US", "7.1.1",false ); }

    for (int i = 0; i <= 9; i++) { champnames[i] = champions[i].name; }

    for (int i = 0; i <= 9; i++) { sumners[i] = await cs.RetrieveSummoner(CreepScoreAPI.CreepScore.Region.EUNE, summnames[i]); }

    for (int i = 0; i <= 9; i++) { leagues[i] = await sumners[i].RetrieveLeague(); }

    /* teamsorter */
    foreach (CreepScoreAPI.ParticipantLive p in participants)
    {
        if (p.summonerId == summid)
        {
            centsumm = p;
        }

        if (p.teamIdLong == centsumm.teamIdLong)
        {
            ally[s] = p;
            s++;
        }
        if (p.teamIdLong != centsumm.teamIdLong)
        {
            enemy[se] = p;
            se++;
        }
    }

    data = " I'LL FORMAT A STRING THAT OUTPUTS ALL THE DATA I NEED HERE";
    return data;
}

当我调用游戏函数并输入召唤者的名字时,我进入了第一个 for 循环,甚至没有填充 summnames[] 数组并停止执行,没有错误代码。

我要对所有循环执行的操作是填充我在函数之前创建的变量,以便以后可以将它们用于其他目的。

【问题讨论】:

  • 您永远不会将数组分配给summnames 或任何其他数组。如果你这样做并将它们全部设为 10,那么你可以只使用一个 for 循环。

标签: c# api nuget


【解决方案1】:

我认为您应该分配所有数组并将它们的长度设置为至少 10。

这可以解决问题。

【讨论】:

  • 你认为“分配所有数组”对一个如此年轻以至于他不知道初始化它们的人意味着什么?你不能假设它会表示任何非常具体的东西。
  • ...再想一想,如果 OP 接受了答案,这显然足以说明他的情况。
猜你喜欢
  • 1970-01-01
  • 2021-08-10
  • 2019-05-16
  • 1970-01-01
  • 2019-06-22
  • 1970-01-01
  • 2021-08-09
  • 1970-01-01
  • 2017-02-24
相关资源
最近更新 更多