所以我试图让这个循环遍历每个高分数组(scorelist)的索引,以检查当前分数(playerScore)是否高于当前索引.如果if语句得到"是",那么它应该沿着索引推下所有分数(或者更高?)并将playerScore放在应该的位置.
{
Console.Write("input name: ");
string playerName = Console.ReadLine();
Console.Write("input score: ");
int playerScore = Convert.ToInt32(Console.ReadLine());
for (int i = 0; i < 10; i++)
{
if (playerScore > scoreList[i])
{
int nextNo = 9;
for (int n = 10; n > 0; n--)
{
scoreList[n] = scoreList[nextNo];
nameList[n] = nameList[nextNo];
nextNo--;
}
scoreList[i] = playerScore;
nameList[i] = playerName;
}
}
}
Run Code Online (Sandbox Code Playgroud)
使用此当前代码,它只是将playerScore放在第一个索引上(如果它高于之前存储的索引),然后将其复制到索引的其余部分.对于如何解决这个问题,有任何的建议吗?
它可以更轻松地完成:
int prev = playerScore;
for (int i = 0; i < 10; i++)
{
if (playerScore > scoreList[i])
{
int temp = scoreList[i];
scoreList[i] = prev;
prev = temp;
}
}
Run Code Online (Sandbox Code Playgroud)
我假设你只需要前10个结果(你的阵列中已经有10个元素).
| 归档时间: |
|
| 查看次数: |
623 次 |
| 最近记录: |