题主要的代码如下,有帮助麻烦点个采纳【本回答右上角】,谢谢~~有其他问题可以继续交流~,如需工程文件可以站内发邮箱。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace 长方体
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
double length = double.Parse(txtLength.Text);
double width = double.Parse(txtWidth.Text);
double height = double.Parse(txtHeight.Text);
lbResult.Text = new Cuboid {Length=length,Width=width,Height=height }.ToString();
}
}
class Cuboid
{
public double Length { get; set; }
public double Width { get; set; }
public double Height { get; set; }
public double Volume()
{
return Length * Width * Height;
}
public double Area()
{
return (Length * Width + Length * Height + Width * Height) * 2;
}
public new string ToString()
{
return string.Format("表面积是{0},体积是{1}",this.Area(),this.Volume());
}
}
}
窗体样式都贴图给你了,
加减乘除法应该会吧?
大学好好读书。
class Cuboid
{
public Cuboid()
{
}
double _length = 10;
public double Length
{
get { return _length; }
set
{
if (value < 0)
value = 0;
_length = value;
}
}
double _width = 5;
public double Width
{
get { return _width; }
set
{
if (value < 0)
value = 0;
_width = value;
}
}
double _height = 5;
public double Height
{
get { return _height; }
set
{
if (value < 0)
value = 0;
_height = value;
}
}
public double GetArea()
{
return 2 * (_length * _width + _width * _height + _height * _length);
}
public double GetVolume()
{
return _length * _width * _height;
}
}
使用 :
Cuboid cuboid = new Cuboid() { Length = 20, Width = 10, Height = 5 };
double ar = cuboid.GetArea();
double vo = cuboid.GetVolume();
float length =float.Parse(this.textBox1.Text.ToString());
float width = float.Parse(this.textBox2.Text.ToString());
float height = float.Parse(this.textBox3.Text.ToString());
float biaomianji = 2 * length * width + 2 * length * height + 2 * width * height;
float tiji = length * width * height;
string s = biaomianji.ToString() + " " + tiji.ToString();
this.textBox4.Text = s;