Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/301.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在C中创建已知长度的数组#_C#_Unity3d - Fatal编程技术网

C# 在C中创建已知长度的数组#

C# 在C中创建已知长度的数组#,c#,unity3d,C#,Unity3d,我目前正在学习使用C#在Unity中制作游戏的教程。在这本书中,他们向我们展示了如何使用 using UnityEngine; using UnityEngine.UI; public class Score : MonoBehaviour { public Transform player; public Text scoreText; // Update is called once per frame void Update() {

我目前正在学习使用C#在Unity中制作游戏的教程。在这本书中,他们向我们展示了如何使用

using UnityEngine;
using UnityEngine.UI;

public class Score : MonoBehaviour
{
    public Transform player;
    public Text scoreText;

    // Update is called once per frame
    void Update()
    {
        scoreText.text = player.position.z.ToString("0");
    }
}
但是,我想更进一步,找出如何在左上角列出高分列表。我唯一的经验是使用python,所以我知道我会制作一个大小为10的空数组,然后每次游戏重新启动时,如果分数大于上述列表中最高的10个分数中的任何一个,它会被插入到相应的位置(保持元素的顺序),并且从列表中删除最低的值(只保留10个元素的列表)。但是,我对C#的语法感到困惑

目前,我对代码的思考过程(将在我的重新启动语句中进行),如果是python的话,是这样的

##this is how I would write it in python I believe
array = np.zeros[10]
for i in range(0, len(array)):
    if player.position.z.ToString("0") < array[-i]
          continue
    else:
          array[-i-1] = player.position.z.ToString("0")
###我相信这就是我用python编写它的方式
数组=np.零[10]
对于范围(0,len(数组))中的i:
如果player.position.z.ToString(“0”)
显然,player.position语句来自c#。我想知道是否有人可以帮我重新翻译思考过程。似乎我需要声明一个字符串数组才能使用它,但我不确定


感谢您的帮助

数组必须有长度,如果您想要未知大小,请使用
列表

List<int> player = new List<int>();
player.Add(1);
List player=新列表();
玩家。添加(1);

您可以创建一个已知大小为10的数组,如下所示:

string[] aStringArray = new string[10];
在顶部也加上这个

using System.Collections.Generic;

据我所知,您需要一个包含10个元素的数组,用于存储前10名的分数。因此,高于现有前10名的每个新分数都将被放置在前10名的正确位置

差不多

当前排名前10位=>2,3,5,7,8,9,12,15,18,20

新的前10名=>3,5,7,8,9,12,15,16,18,20

备选案文1:

string[] scoreArray = new string[10];

//Initializing the score array
for (int i = 0; i < 10; i++)
{
    scoreArray[i] = 0; 
}

//Somewhere in your code a new score will be assigned to this variable
int newScore;

//Checking potentially higher score

boolean replaceFlag = false;
for (int i = 0; i < 10; i++)
{
    if(newScore > scoreArray[i]) 
    {
        replaceFlag = true;
    }else{
        //newScore lower than lowest stored score
        if(i == 0)
        {
            //Stop the loop
            i = 11;
        }else if(replaceFlag){

            //The rest of the elements in the array move one position back
            for(int j = 0 ; j < i-1; j++ )
            {
                scoreArray[j] = scoreArray[j+1];
            }
            //Finally we insert the new score in the correct place
            scoreArray[i-1] = newScore;         
        }
    }
}
string[]scoreArray=新字符串[10];
//初始化分数数组
对于(int i=0;i<10;i++)
{
scoreArray[i]=0;
}
//在代码中的某个地方,将为该变量分配一个新的分数
国际新闻核心;
//检查可能更高的分数
布尔替换标志=false;
对于(int i=0;i<10;i++)
{
if(新闻核心>记分数组[i])
{
replaceFlag=true;
}否则{
//新闻核心低于最低存储分数
如果(i==0)
{
//停止循环
i=11;
}else if(替换标志){
//数组中的其余元素向后移动一个位置
对于(int j=0;j
选项2:使用列表

//Create and initialize list of scores
List<int> listScores = new List<int>{ 0,0,0,0,0,0,0,0,0,0};



// If later, at some point we have the following scores 2, 3, 5, 7, 8, 9, 12, 15, 18, 20

//When you get a new score (our list will have)
listScores.Add(newScore);

//After inserting 2, 3, 5, 7, 8, 9, 12, 15, 18, 20, 16

//Ordering the list in descending order
listScores.Sort()
//Now we have 2, 3, 5, 7, 8, 9, 12, 15, 16, 18, 20,


//Drop last element of the list to keep the fixed size of 10.
listScores.RemoveAt(0)
//Now we have 3, 5, 7, 8, 9, 12, 15, 16, 18, 20

//In case you want to have the list in descending order
listScores.Sort((a, b) => -1* a.CompareTo(b));
//创建并初始化分数列表
List listScores=新列表{0,0,0,0,0,0,0};
//如果稍后,在某一点上,我们有以下分数2,3,5,7,8,9,12,15,18,20
//当您获得新分数时(我们的列表将显示)
添加(新闻核心);
//插入2,3,5,7,8,9,12,15,18,20,16之后
//按降序排列列表
listScores.Sort()
//现在我们有2,3,5,7,8,9,12,15,16,18,20,
//删除列表的最后一个元素以保持10的固定大小。
listScores.RemoveAt(0)
//现在我们有3,5,7,8,9,12,15,16,18,20
//以防您希望列表按降序排列
排序((a,b)=>-1*a.CompareTo(b));

如果分数是数组,则将分数添加到此分数数组变量中,然后在需要更新高分时对该数组进行排序和反转

private void UpdateScore(int[] ScoreArray)
{
   Array.Sort(ScoreArray);
   Array.Reverse(ScoreArray);
} 

告诉您使用ToString()每一帧都是一个糟糕的想法。我甚至不知道有人怎么认为这个建议是适度的。@Krythic哦,我只是在youtube上看Brackey的教程,替代品是什么?我想如果你发现当前分数高于数组的任何条目,你需要输入该分数和数组的其他条目应该是下移(而不是替换)Hello Saif,我读了问题,但没有找到他想要使用列表(或已知大小的数组)的地方.在C#中,数组必须有长度,如果要使用未知长度,请使用列表,而不是。是的,我知道。但在要求中,哪里说明他需要未知长度数组?调整大小注释在做什么?似乎它使每个循环都变大了?更典型的是,您会使用列表:无需调整大小,只需向其中添加/删除元素即可列表。我不tink原创海报想要使用数组,他只想用c#做Pyton中的数组。改变语言,最好的建议是提供正确的工具来用新语言完成工作。只要我的2美分
private void UpdateScore(int[] ScoreArray)
{
   Array.Sort(ScoreArray);
   Array.Reverse(ScoreArray);
}