目前,我有一个已经分配了随机值的2D数组.我知道如何用这样的东西找到最大的值:
public static int Largest( int[,] C)
{
int largest = 0;
for (int row = 0; row < C.GetLength(0); row++)
{
for (int col = 0; col < C.GetLength(1); col++)
{
if( largest < C[ row, col])
{
largest = C[ row, col];
}
}
}
return largest;
}
Run Code Online (Sandbox Code Playgroud)
但我想找到三个最大的值并返回总和.我不一定对我如何找到它有任何限制,但它是否会成为我写的最大方法的另一种方法?
继承还是比较新的,所以我需要一些帮助.目前,我有一项任务是创建一个使用3武器继承的基本游戏.我的基类:
public class Weapons : MonoBehaviour
{
public int rateOfFire;
public string myName;
public Weapons(string Name)
{
myName = Name;
}
}
Run Code Online (Sandbox Code Playgroud)
其中一个子课程:
public class Rifle : Weapons
{
public Rifle(string Name)
: base(Name)
{
myName = Name;
rateOfFire = 5;
}
}
Run Code Online (Sandbox Code Playgroud)
我的第一个问题是,如果我做得对吗?
我的第二个问题是如何将这些实例化为我的Unity场景?我写了一个切换武器的方法,这是我的播放器脚本上的一个数组,所以我会在这里插入一个实例化的行还是创建另一个方法?缺少一些线条,但这是正确的:
public GameObject[] weapons;
public int currentweapon = 0;
private int invweap;
void Start()
{
//Managing weapons
invweap = weapons.Length;
SwitchWeapon(currentweapon);
}
void SwitchWeapon(int index)
{
for (int i = 0; i<invweap;i++)
{
if (i == …Run Code Online (Sandbox Code Playgroud)