c#打印方框_c#编写一个程序,打印用星号(*)绘制的方框(正方形),每条边5个*...

该C#代码实现了一个程序,根据用户输入的边长和位置,使用星号(*)在控制台打印出一个正方形方框。程序包括Point类用于存储坐标,Class1类用于绘制方框,Main方法接收用户输入并调用绘制方法。
摘要由CSDN通过智能技术生成

匿名用户

1级

2008-11-27 回答

using System;

using System.Collections.Generic;

using System.Text;

namespace c

{

class Point

{

public int x;

public int y;

public Point(int nx, int ny)

{

Set(nx, ny);

}

public Point(Point n)

{

Set(n);

}

public void Set(int nx, int ny)

{

x = nx;y = ny;

}

public void Set(Point n)

{

x = n.x;

y = n.y;

}

public int getx()

{

return x;

}

public int gety()

{

return y;

}

}

class Class1

{

private int side;

private Point location;

public Class1(Point location,int length)

{

this.side = length;

this.location = location;

graph(location, length);

}

public void Class(Point center,int length)

{ }

public void graph(Point center,int length)

{

for(int i = 0; i < center.y; i++) Console.WriteLine("");

for(int i = 0; i < length; i++)

{

string line = "";

for(int j = 0; j < center.x * 2 + length * 2 - 1; j++)

{

if(i == 0 || i == length - 1)

{

if(j >= center.x * 2 && (j - center.x * 2) % 2 == 0) line+="*";

else line+=" ";

}

else

{

if(j == center.x * 2 || j == center.x * 2 + length * 2 - 2) line+="*";

else line+=" ";

}

}

Console.WriteLine(line);

}

}

public int getarea()

{

return side*side;

}

public int getperimeter()

{

return side*4;

}

public int getlength()

{

return side;

}

public Point getlocation()

{

return location;

}

}

class program

{

static void Main(string[] args)

{

int x, y, a;

Console.WriteLine("请输入正方形的边长及位置:");

Console.WriteLine("边长:");

a = int.Parse(Console.ReadLine());

Console.WriteLine("位置:");

x = int.Parse(Console.ReadLine());

y = int.Parse(Console.ReadLine());

Class1 sd = new Class1(new Point(x, y), a);

Console.WriteLine("Square:");

Console.WriteLine("Length=" + sd.getlength());

Console.WriteLine("Location=" + sd.getlocation().getx() + "," + sd.getlocation().gety());

Console.WriteLine("Perimeter=" + sd.getperimeter());

Console.WriteLine("Area=" + sd.getarea());

}

}

}

运行时输入边长:5 位置:5

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值