폭 직사각형의 어레이를 생성 (크기 20) 20 개 사각형 배열채우고, 각각 임의의 길이
칠이 각각 임의의 길이와 폭을 갖는다. 그런 다음 배열의 내용을 인쇄하십시오.
이 내 주어진 사각형 클래스입니다 :
class Rectangle
{
private int length, width;
private int area, perimeter;
public Rectangle()
{
}
public Rectangle(int l, int w)
{
length = l;
width = w;
}
public void SetDimension(int l, int w)
{
length = l;
width = w;
}
public void InputRect()
{
length = int.Parse(Console.ReadLine());
width = int.Parse(Console.ReadLine());
}
public void ComputeArea()
{
area = length * width;
}
public void ComputePerimeter()
{
perimeter = 2 * (length + width);
}
public void Display()
{
Console.WriteLine("Length: {0} \tWidth: {1} \tArea: {2} \tPerimeter: {3}", length, width, area, perimeter);
}
}
이 내가 난수를 얻을 프로그램의 시작이다. 나는 여기 붙어있다.
배열의 동일한 색인에 정확히 2 개의 숫자를 입력하려면 어떻게해야합니까?
class Program
{
static void Main(string[] args)
{
Rectangle r1 = new Rectangle();
int[] x = new int[20];
Random rand = new Random();
for (int i = 0; i < x.Length; i++)
{
int width = rand.Next(45, 55);
int length = rand.Next(25, 35);
}
//r1.InputRect(width, length);
Console.WriteLine("The following rectanglesn are created: ");
//r1.Display(x);
}
}
int 배열을 사용하는 이유는 무엇입니까? 왜 사각형 배열을 만들지 않겠습니까? 'Rectangle [] rectangle = new Rectangle [20]' 그리고 나서'new Rectangle (rand.Next (45,55), rand.Next (25,35)'를 for 루프의 각 단계? – sydan
아래에 제공된 답변 중 하나라도 문제를 해결하는 데 도움이 되었다면 – Kai