我尝试随机化值,如果它们没有改变,但它不会让我在构造函数中使用随机数发生器,并且当我使用其他函数时会给出错误。C#构造函数中的错误
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Randomizer {
class Apartment {
public int height;
public int bas;
public int hasPent;
public Apartment(int b = 100, int h = 100, int p = 100) {
height = h;
bas = b;
hasPent = p;
public Room[,,] rooms = new Room[bas, bas, height];
finCon(bas, height, hasPent, rooms);
}
void finCon(int b, int h, int p, Room[,,] ro) {
Random r = new Random();
if (b==100) {
b = r.Next(2,4);
}
if (h==100) {
h = r.Next(4,15);
}
if (p==100) {
p = r.Next(0,20);
}
}
}
class Room {
int some = 37;
}
class Program {
static void Main(string[] args)
{
Apartment ap = new Apartment();
ap.finCon(ap.bas,ap.height,ap.hasPent,ap.rooms);
Console.WriteLine("{0}{1}",ap.bas,ap.height);
}
}
}
错误:
(1:1)命名空间不能直接包含成员如字段或方法
(16点25)}预期
(18时13分)方法必须具有返回类型
(18:23)标识符预计
(18时31)标识符预期
(18:40)标识符预期
(18时47分)标识符预期
(21:9)命名空间不能直接包含成员如字段或方法
(21点47分)标识符预期
(21点48分)标识符预期
(2 1:51)预期类,预期的委托,枚举接口,或结构
(22时28分)类,委托,枚举接口,或结构
(33:5)输入或命名空间定义,或档案结尾预期
(46:1)类型或命名空间定义,或 - 的文件结束预定
你应该在你的问题中包含错误的细节。 – mason
如果您遇到错误,它总是**有帮助将它们包含在您的帖子中。不要让我们猜测和搜索你的代码的问题。 –
您不能在构造函数中放置'public room [,,] rooms = new Room [bas,bas,height];''。你需要'public room [,,] rooms;'和其余的字段(例如,直接在'public int hasPent;'之下),然后在构造函数中,你需要'rooms = new Room [bas,bas,height]; '分配给'bas'和'height'后。 – Quantic