C#计算面积和周长 [英] C# calculating area and perimeter

查看:827
本文介绍了C#计算面积和周长的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以使用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.

这篇关于C#计算面积和周长的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆