C#计算面积和周长周长、面积

2023-09-03 07:21:48 作者:没有仙气的仙女

所以使用Microsoft Visual C#2010例preSS IM。 我尝试做一个程序,计算面积和周长。 我有2个文本框的长度和宽度。 我有2个只读文本框的面积和周长。 所以我把对面积和周长的数字和我得到在该地区的答案,周长只读箱子当我点击计算按钮。 什么是code这种计算?

so im using Microsoft Visual C# 2010 Express. im trying to make a program that calculates area and perimeter. i have 2 text boxes for length and width. i have 2 readonly text boxes for area and perimeter. so i put in the numbers for area and perimeter and i get the answers in the area and perimeter readonly boxes when i click the "calculate" button. what's the code for this calculation?

推荐答案

那么这将是简单。使用基本几何公式:面积=高x宽和 Perimiter =(身高+宽)×2 的

Well that would be simple. Using basic geometrical formulas: Area = height x width and Perimiter = (height + width) x 2

然后,只需做到这一点:

Then simply do this:

int pmtr = (Convert.ToInt32(textBox1.Text) + Convert.ToInt32(textBox2.Text)) * 2;
int area = Convert.ToInt32(textBox1.Text) * Convert.ToInt32(textBox2.Text);

textBox3.Text = pmtr.ToString();
textBox4.Text = area.ToString();

此外,您可能想看看的TryParse 使codea的位更稳定,避免无效值异常。

Also, you might want to look at TryParse to make the code a bit more stable and avoid exceptions on invalid values.