我正在制作一个找到方形区域的程序。计算在另一个类上完成。我必须使用值为1-10的数组。我必须使用其他类的属性在数组中找到这些数字的方块。我很困惑如何做到这一点。这就是我到目前为止所做的。
using System;
using Square;
namespace DemoSquares
{
public class DemoSquares
{
static void Main()
{
int[] numbers = new int[10];
Squares asquare = new Squares();
asquare.Length = numbers[0];
foreach (int i in numbers)
{
Console.WriteLine("{0}", i, asquare.Area);
}
}
}
} 这是班级。 使用System;
namespace Square
{
class Squares
{
private int length;
private int area;
public int Length
{
get
{
return length;
}
set
{
length = value;
CalcArea();
}
}
public int Area
{
get
{
return area;
}
}
private void CalcArea()
{
area = Length * Length;
}
}
}
答案 0 :(得分:1)
首先使用一些值填充数组,可能是这样的:
int[] numbers = new int[10];
int counter = 1;
for (int i = 0; i < numbers.Length; i++) {
numbers[i] = counter;
counter++;
}
然后你就可以找到每个方格的区域:
foreach (int i in numbers)
{
Squares asquare = new Squares();
asquare.Length = i;
Console.WriteLine("{0}", i, asquare.Area);
}
另一种选择
int[] numbers = {
1,2,3,4,5,6,7,8,9,10 // enter your numbers here
};
numbers.ToList().ForEach(n => {
Squares asquare = new Squares();
asquare.Length = n;
Console.WriteLine("{0}", n, asquare.Area);
});
注意 - 如果您决定使用后者,请确保导入:
using System.Linq;
答案 1 :(得分:-1)
如果我理解你,我不会感到害羞,但这可能有用:
foreach (int i in numbers)
{
asquare.Length = i;
asquare.CalcArea();
Console.WriteLine("Area for {0}: {1}", i, asquare.Area);
}