C#如何声明1个以上的对象?

Its*_*LOL 0 c#

我做了一个房间类,(它的长度、宽度、高度都是整数)但我想制作 2 个不同的对象(因为 1 个声明的类是 1 个房间,程序会询问用户“你想要多少个房间拥有?”或类似的东西......)

如何从 Room 类制作 2 或 3 个不同的房间?

这是我所拥有的:

using System;
using System.Collections.Generic;
using System.Linq;

namespace Feladatok1_20
{
    public class Exercise55TEST
    {
        public static void exercise()
        {
            System.Console.WriteLine("How many rooms would you like to work in?");
            int numberOfRooms = Int32.Parse(Console.ReadLine());

            List<int> room = new List<int>();

            for (int i = 0; i < numberOfRooms; i++)
            {
                System.Console.WriteLine("How long is the room? (length)");
                int length = Int32.Parse(Console.ReadLine());
                //room.Add(length);

                System.Console.WriteLine("How high is the room? (height)");
                int height = Int32.Parse(Console.ReadLine());
                //room.Add(height);

                System.Console.WriteLine("How wide is the room? (width)");
                int width = Int32.Parse(Console.ReadLine());
                //room.Add(width);

                int area = width*length;
                int wallArea = length*height;
                int ceilingArea = width*length;

                room.Add(area);
                room.Add(wallArea);
                room.Add(ceilingArea);

            }

        }
    }
}
Run Code Online (Sandbox Code Playgroud)

这是我制作的课程:

namespace Feladatok1_20
{
    public class Exercise55
    {
        public int length;

        public int height;
        public int width;


    }
}
Run Code Online (Sandbox Code Playgroud)

fub*_*ubo 5

1. 重命名你的班级

public class Room
{
    public int length { get; set; }
    public int height { get; set; }
    public int width { get; set; }
    // those can be calculated so I would declare readonly properties with get-only
    public int area { get { return width * length; } }
    public int wallArea { get { return  length * height; } }
}
Run Code Online (Sandbox Code Playgroud)

旁注:wallArea应该是

public int wallArea { get { return  2 * ((length * height) + (width * height)); } }
Run Code Online (Sandbox Code Playgroud)

并且天花板的面积等于地板的面积,所以一个属性area就足够了

2. 声明一个房间列表而不是整数

List<Room> roomList = new List<Room>();
Run Code Online (Sandbox Code Playgroud)

3.创建你的房间

for (int i = 0; i < numberOfRooms; i++)
{
    //Console.ReadLine and int.Parse here 
    roomList.Add(new Room(){ length = inputLength, width = inputWidth, height = inputHeight});
Run Code Online (Sandbox Code Playgroud)