选择不同的图形呈现不同的输入界面;
根据输入数据计算相应的面积和周长,并呈现在相应的文本框中
我写出来的效果是,圆能够出结果,但是矩形和正方形输入长、宽,没有结果显示

代码如下:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
Radius.Show();
txtRadius.Show();
length.Hide();
txtLength.Hide();
Weight.Hide();
txtWeight.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
double r = 0;
double.TryParse(txtRadius.Text, out r);
double l = 0;
double.TryParse(txtLength.Text, out l);
double w = 0;
double.TryParse(txtWeight.Text, out w);
if (txtRadius.Text != null)
{
Circle circle = new Circle(r);
double s = circle.area();
double c = circle.circumference();
txtResult1.Text = s.ToString();
txtResult2.Text = c.ToString();
}
if (txtLength.Text != null && txtWeight == null)
{
Rectangle rectangle = new Rectangle(l, w);
double s = rectangle.area();
double c = rectangle.circumference();
txtResult1.Text = s.ToString();
txtResult2.Text = c.ToString();
}
if(txtLength.Text == null && txtWeight != null)
{
Square square = new Square(w);
double s = square.area();
double c = square.circumference();
txtResult1.Text = s.ToString();
txtResult2.Text = c.ToString();
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
length.Show();
txtLength.Show();
Weight.Show();
txtWeight.Show();
Radius.Hide();
txtRadius.Hide();
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
Weight.Show();
txtWeight.Show();
length.Hide();
txtLength.Hide();
Radius.Hide();
txtRadius.Hide();
}
}
}
abstract class Figure
{
public abstract double area();
public abstract double circumference();
}
class Rectangle : Figure
{
private double Length, Weight;
public Rectangle(double l, double w)
{
Length = l;
Weight = w;
}
public override double area()
{
return Length* Weight; ; //方法定义了返回值类型,没有返回值就会导致此错误:并非所有的代码路径都返回值
}
public override double circumference()
{
return 2 * (Length + Weight);
}
}
class Square : Rectangle
{
public Square(double w) : base(w, w)
{
}
}
class Circle : Figure
{
double r;
public Circle(double a)
{
r = a;
}
public override double area()
{
return Math.PI * r * r;
}
public override double circumference()
{
return Math.PI * 2 * r;
}
根据输入数据计算相应的面积和周长,并呈现在相应的文本框中
我写出来的效果是,圆能够出结果,但是矩形和正方形输入长、宽,没有结果显示

代码如下:
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
Radius.Show();
txtRadius.Show();
length.Hide();
txtLength.Hide();
Weight.Hide();
txtWeight.Hide();
}
private void button1_Click(object sender, EventArgs e)
{
double r = 0;
double.TryParse(txtRadius.Text, out r);
double l = 0;
double.TryParse(txtLength.Text, out l);
double w = 0;
double.TryParse(txtWeight.Text, out w);
if (txtRadius.Text != null)
{
Circle circle = new Circle(r);
double s = circle.area();
double c = circle.circumference();
txtResult1.Text = s.ToString();
txtResult2.Text = c.ToString();
}
if (txtLength.Text != null && txtWeight == null)
{
Rectangle rectangle = new Rectangle(l, w);
double s = rectangle.area();
double c = rectangle.circumference();
txtResult1.Text = s.ToString();
txtResult2.Text = c.ToString();
}
if(txtLength.Text == null && txtWeight != null)
{
Square square = new Square(w);
double s = square.area();
double c = square.circumference();
txtResult1.Text = s.ToString();
txtResult2.Text = c.ToString();
}
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
length.Show();
txtLength.Show();
Weight.Show();
txtWeight.Show();
Radius.Hide();
txtRadius.Hide();
}
private void radioButton3_CheckedChanged(object sender, EventArgs e)
{
Weight.Show();
txtWeight.Show();
length.Hide();
txtLength.Hide();
Radius.Hide();
txtRadius.Hide();
}
}
}
abstract class Figure
{
public abstract double area();
public abstract double circumference();
}
class Rectangle : Figure
{
private double Length, Weight;
public Rectangle(double l, double w)
{
Length = l;
Weight = w;
}
public override double area()
{
return Length* Weight; ; //方法定义了返回值类型,没有返回值就会导致此错误:并非所有的代码路径都返回值
}
public override double circumference()
{
return 2 * (Length + Weight);
}
}
class Square : Rectangle
{
public Square(double w) : base(w, w)
{
}
}
class Circle : Figure
{
double r;
public Circle(double a)
{
r = a;
}
public override double area()
{
return Math.PI * r * r;
}
public override double circumference()
{
return Math.PI * 2 * r;
}