C# 有没有方法声明具有固定int大小的浮点数组?
有没有一种方法可以声明一个包含浮点变量且大小固定为int的数组C# 有没有方法声明具有固定int大小的浮点数组?,c#,arrays,floating-point,declaration,C#,Arrays,Floating Point,Declaration,有没有一种方法可以声明一个包含浮点变量且大小固定为int的数组 int balls = 5; float array posX[balls]; private float posY[] = 0; 基本上,我想创建一个数组,这样每个球都可以有自己的XY坐标,而无需创建少量新变量。我得到的错误是,我有错误的数组声明(第3行),并且posX和Balls在当前上下文中不存在(第2行)。上面的代码是我的一些尝试和错误。我正在制作一个xamarin表单应用程序,也许这与此有关?我的完整代码如下: usin
int balls = 5;
float array posX[balls];
private float posY[] = 0;
基本上,我想创建一个数组,这样每个球都可以有自己的XY坐标,而无需创建少量新变量。我得到的错误是,我有错误的数组声明(第3行),并且posX和Balls在当前上下文中不存在(第2行)。上面的代码是我的一些尝试和错误。我正在制作一个xamarin表单应用程序,也许这与此有关?我的完整代码如下:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
using SkiaSharp;
using SkiaSharp.Views.Forms;
using System.Runtime.InteropServices.ComTypes;
using System.Runtime.CompilerServices;
using System.Linq.Expressions;
using Xamarin.Forms.Internals;
namespace Ball_Bounce
{
public partial class MainPage : ContentPage
{
int balls = 5;
float array posX[balls];
private float posY[] = 0;
float dx = 5;
float dy = 5;
int ballD = 100;
bool gravity = false;
float time = 1f / 60;
float mass = 10;
float g = -9.8f;
float forceOnWalls = 0;
int bounces = 0;
SKPaint blackFillPaint = new SKPaint
{
Style = SKPaintStyle.Fill,
Color = SKColors.Black
};
SKPaint Ball = new SKPaint
{
Color = SKColors.Black,
};
public MainPage()
{
InitializeComponent();
Device.StartTimer(TimeSpan.FromSeconds(1f / 60), () =>
{
CanvasView.InvalidateSurface();
return true;
});
}
private void CanvasView_PaintSurface(object sender, SKPaintSurfaceEventArgs e)
{
SKSurface surface = e.Surface;
SKCanvas canvas = surface.Canvas;
canvas.Clear(SKColors.SteelBlue);
float width = e.Info.Width;
float height = e.Info.Height;
canvas.Translate(width / 2, height / 2);
posX += dx;
posY += dy;
if (gravity == false)
{
bounces = 0;
if (posX >= width / 2 - ballD|| posX <= -width / 2 + ballD)
{
dx = -dx;
}
else if (posY >= height / 2 - ballD|| posY <= -height / 2 + ballD)
{
dy = -dy;
}
}
else if (gravity == true)
{
if (bounces >= 8)
{
dy = 0;
posY = height / 2 - ballD;
}
dy = Gravity(dy, time);
if (posX >= width / 2 - ballD || posX <= -width / 2 + ballD)
{
dx = -dx;
}
else if (posY >= height / 2 - ballD || posY <= -height / 2 + ballD)
{
dy = -dy+dy/5;
bounces += 1;
}
//forceOnWalls = mass * Math.Abs(dy);
//if (posY < height / 2 + 11*ballD/10)
//{
// dy = Gravity(dy, time);
// if (posX >= width / 2 - ballD || posX <= -width / 2 + ballD)
// {
// dx = -dx;
// }
// else if (posY > (height / 2) - (ballD + 10))
// {
// dy = -dy + 2f; //slows bouncing
// }
}
//else if (posY >= height / 2 - 9 * ballD / 10 && posY <= height / 2 - 2 * ballD && (forceOnWalls < 1 && forceOnWalls > 0))
//{
// if (posX >= width / 2 - ballD || posX <= -width / 2 + ballD)
// {
// dx = -dx;
// }
// posY = height / 2 - ballD;
// dy = 0;
canvas.DrawCircle(posX, posY, ballD, blackFillPaint);
}
void OnToggled(object sender, ToggledEventArgs e)
{
if (gravity == true)
{
dy = -5;
gravity = false;
}
else
{
gravity = true;
}
}
void OnSpeedValueChanged(object sender, ValueChangedEventArgs args)
{
double value = args.NewValue;
float valueF = Convert.ToSingle(value);
float signX = Math.Sign(dx);
float signY = Math.Sign(dy);
dx = valueF * signX;
dy = valueF * signY;
}
void OnGravityValueChanged(object sender, ValueChangedEventArgs args) //slider that controls
{
double value = args.NewValue;
float valueF = Convert.ToSingle(value);
g = valueF;
}
public static float Gravity(float vOld, float time) //calculates the dy changes
{
float g = 15f;
float vNew = vOld + g * time;
return vNew;
}
}
}
使用系统;
使用System.Collections.Generic;
使用系统组件模型;
使用System.Linq;
使用系统文本;
使用System.Threading.Tasks;
使用Xamarin.Forms;
使用SkiaSharp;
使用SkiaSharp.Views.Forms;
使用System.Runtime.InteropServices.ComTypes;
使用System.Runtime.CompilerServices;
使用System.Linq.Expressions;
使用Xamarin.Forms.Internals;
球反弹
{
公共部分类主页:ContentPage
{
int=5;
浮点数组posX[球];
私有浮动posY[]=0;
浮点数dx=5;
浮动dy=5;
int ballD=100;
布尔重力=假;
浮动时间=1f/60;
浮子质量=10;
浮动g=-9.8f;
浮力墙=0;
int反弹=0;
SKPaint BLICKFILLPAINT=新SKPaint
{
Style=SKPaintStyle.Fill,
颜色=黑色
};
SKPaint Ball=新SKPaint
{
颜色=黑色,
};
公共主页()
{
初始化组件();
设备启动计时器(时间跨度从秒开始(1f/60),()=>
{
CanvasView.InvalidateSurface();
返回true;
});
}
私有void CanvasView_PaintSurface(对象发送器,SKPaintSurfaceEventArgs e)
{
SKSurface=e.表面;
SKCanvas canvas=surface.canvas;
帆布。透明(SKColors。钢蓝色);
浮动宽度=e.Info.width;
浮动高度=e.Info.height;
画布。平移(宽度/2,高度/2);
posX+=dx;
posY+=dy;
如果(重力==假)
{
反弹=0;
如果(posX>=width/2-ballD | | posX=height/2-ballD | | posY=8)
{
dy=0;
posY=高度/2-球形;
}
dy=重力(dy,时间);
如果(posX>=width/2-ballD | | posX=height/2-ballD | | posY=width/2-ballD | | posX(height/2)-(ballD+10))
// {
//dy=-dy+2f;//减缓反弹
// }
}
//否则如果(posY>=高度/2-9*ballD/10&&posY 0))
//{
//如果(posX>=width/2-ballD | | posX,这是一个固定大小(10)的浮点数组
float[] array = new float[10];
为了防止错误,请在构造函数中执行赋值
namespace Ball_Bounce
{
public partial class MainPage : ContentPage
{
int balls;
float[] array;
private float posY;
....
public MainPage()
{
balls = 5;
array = new float[balls];
posY = 0;
}
“整数的固定大小”是什么意思?您同时声明一个数组及其大小,如下所示:private float[]posY=new float[10];
(在本例中,对于10个float的数组)
。您还可以使用集合初始化模式声明和初始化它们。使用列表而不是array@MatthewWatson在有10的地方,我想使用balls变量。如果数组是一个字段(比如你的posY
),我这样做时,它会显示坏数组声明(CS0650)然后在初始化它时必须使用const
,因此尝试将balls声明为const int balls=5;