c#类的构造函数错误
本文关键字:错误 构造函数 | 更新日期: 2023-09-27 18:13:53
我尝试随机化值,如果它们没有改变,但它不会让我在构造函数中使用随机化器,当我使用我的其他函数时,它会给出一个错误。
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)标识符预期
(21:51)期望的类、委托、枚举、接口或结构
(22:28)期望的类、委托、枚举、接口或结构
(33:5)类型或命名空间定义,或文件结束
(46:1)类型或命名空间定义,或文件结束
我已经让它编译了:
namespace Randomizer
{
public class Apartment
{
public int height;
public int bas;
public int hasPent;
public Room[,,] rooms;
public Apartment(int b = 100, int h = 100, int p = 100)
{
height = h;
bas = b;
hasPent = p;
rooms = new Room[bas, bas, height];
finCon(bas, height, hasPent, rooms);
}
public 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);
}
}
}
public 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);
}
}
}
你的问题是试图在构造函数中声明一个property
(你不能这样做)。我也把所有的类都公开了。
希望对你有帮助。