Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/274.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C#如何声明多个对象?_C# - Fatal编程技术网

C#如何声明多个对象?

C#如何声明多个对象?,c#,C#,我创建了一个Room类(长度、宽度、高度为整数),但我想创建两个不同的对象(因为一个声明的类代表一个房间,程序会问用户“您想要多少个房间?”或类似的问题……) 我怎样才能从Room类中创建2个或3个不同的房间 以下是我所拥有的: using System; using System.Collections.Generic; using System.Linq; namespace Feladatok1_20 { public class Exercise55TEST {

我创建了一个Room类(长度、宽度、高度为整数),但我想创建两个不同的对象(因为一个声明的类代表一个房间,程序会问用户“您想要多少个房间?”或类似的问题……)

我怎样才能从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);

            }

        }
    }
}
1.重命名你的类 旁注:
wallArea
应为

public int wallArea { get { return  2 * ((length * height) + (width * height)); } }
天花板的面积等于地板的面积,所以一个适当的面积就足够了

2.声明房间列表而不是整数
List-roomList=新列表();
3.创建您的房间
for(int i=0;i
创建“房间”类对象的列表,然后在需要添加时添加一个对象,例如:

List<Room> rooms = new List<Room>();
    Console.WriteLine("How many rooms do you want to have?");
    int count = Console.ReadLine(Convert.ToIn16());
 for(int i=0l i<count; i++)
{
rooms.Add(new Room());
}
列出房间=新建列表();
控制台。WriteLine(“您想要多少个房间?”);
int count=Console.ReadLine(Convert.ToIn16());

对于(int i=0l i为什么您的列表是int列表,而不是
Room
?谢谢!我正在考虑使用列表,但我不知道如何将其组合在一起…谢谢!:)谢谢!因此,制作一个Room列表,称之为“rooms”,然后根据用户输入向其中添加尽可能多的“Room”对象。
public int wallArea { get { return  2 * ((length * height) + (width * height)); } }
List<Room> roomList = new List<Room>();
for (int i = 0; i < numberOfRooms; i++)
{
    //Console.ReadLine and int.Parse here 
    roomList.Add(new Room(){ length = inputLength, width = inputWidth, height = inputHeight});
List<Room> rooms = new List<Room>();
    Console.WriteLine("How many rooms do you want to have?");
    int count = Console.ReadLine(Convert.ToIn16());
 for(int i=0l i<count; i++)
{
rooms.Add(new Room());
}