- 最後登錄
- 2023-4-12
- 在線時間
- 1 小時
- 註冊時間
- 2007-6-9
- 閱讀權限
- 20
- 精華
- 0
- UID
- 1508193
- 帖子
- 434
- 積分
- 325 點
- 潛水值
- 16002 米
| 成為伊莉的版主,你將獲得更高級和無限的權限。把你感興趣的版面一步步地發展和豐盛,那種滿足感等著你來嚐嚐喔。 沒依題目指示要用陣列,反用了LIST來儲存座標
我創了一類別Shape- public class Shape
- {
- public Shape(int x1,int y1,int x2,int y2)
- {
- coordinate_X = x1;
- coordinate_Y = y1;
- coordinate_X2 = x2;
- coordinate_Y2 = y2;
- }
- int coordinate_X = 0;
- int coordinate_Y = 0;
- int coordinate_X2 = 1;
- int coordinate_Y2 = 1;
- public bool isRange(int pointX, int pointY)
- {
- bool range = false;
- if (compareX(pointX))
- {
- range = compareY(pointY);
- }
- return range;
- }
- private bool compareX(int pointX)
- {
- bool range = false;
- if (coordinate_X >= coordinate_X2)
- {
- range = coordinate_X2 <= pointX && coordinate_X >= pointX;
- }
- else
- {
- range = coordinate_X <= pointX && coordinate_X2 >= pointX;
- }
- return range;
- }
- private bool compareY(int pointY)
- {
- bool range = false;
- if (coordinate_Y >= coordinate_Y2)
- {
- range = coordinate_Y2 <= pointY && coordinate_Y >= pointY;
- }
- else
- {
- range = coordinate_Y <= pointY && coordinate_Y2 >= pointY;
- }
- return range;
- }
- }
複製代碼 用此類別負責矩形的各參數,並在類別內寫了比較
先比較X軸,若X軸沒在範圍內,Y軸就不進行比較- static void Main(string[] args)
- {
- List<Shape> listRectangle = new List<Shape>(10);
- listRectangle.Add(new Shape(1, 3, 4, 1));
- listRectangle.Add(new Shape(1, 4, 5, 1));
- listRectangle.Add(new Shape(1, 6, 6, 1));
- listRectangle.Add(new Shape(1, 8, 7, 1));
- listRectangle.Add(new Shape(1, 10, 8, 1));
- listRectangle.Add(new Shape(11, 12, 9, 1));
- listRectangle.Add(new Shape(13, 14, 10, 1));
- listRectangle.Add(new Shape(15, 16, 11, 1));
- listRectangle.Add(new Shape(17, 18, 12, 1));
- listRectangle.Add(new Shape(19, 20, 13, 1));
- Console.Write("請輸入2個整數座標(以空白分隔):");
- string[] point = Console.ReadLine().Split(' ');
- int pointX = int.Parse(point[0].Trim());
- int pointY = int.Parse(point[1].Trim());
- StringBuilder resultText = new StringBuilder();
- resultText.Append(String.Format("({0},{1}) 落入以下矩形: ", pointX, pointY));
- int rectangleIndex = 0;
- foreach (Shape rectangle in listRectangle)
- {
- rectangleIndex++;
- if (rectangle.isRange(pointX, pointY))
- {
- resultText.Append(rectangleIndex + " ");
- }
- }
- Console.Write(resultText.ToString());
- Console.ReadLine();
- }
複製代碼 當作是另一種寫法提供你參考... |
|