【程式設計】【習題講解】BMI計算
題目:建立三個陣列,用來存放六人的姓名、身高、體重資料,在表單載入時,使用迴圈列出六人的資料(含姓名、身高、體重和BMI)及平均值。BMI為身體質量指數=體重(公斤) / 身高^2(公尺^2)。
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { public Form1() { InitializeComponent(); } string[] 姓名 = new string[] {"張志成", "廖美昭", "蔡文龍", "周家旬", "蔡汶璇", "蔡汶妤"}; int[] 身高 = new int[] { 170, 168, 165, 164, 172, 160 }; int[] 體重 = new int[] { 60, 55, 72, 59, 55, 54 }; private void Form1_Load(object sender, EventArgs e) { label1.Text = " 姓名 身高 體重 BMI" + Environment.NewLine; int BMI加總 = 0, 身高加總 = 0, 體重加總 = 0; int BMI = 0; for (int i = 0; i <= 5; i++) { BMI = (int)(體重[i] / (身高[i] / 100.0 * 身高[i] / 100.0)); label1.Text += " " + 姓名[i] + " " + 身高[i] + " " + 體重[i] + " " + BMI + Environment.NewLine; 身高加總 += 身高[i]; 體重加總 += 體重[i]; BMI加總 += BMI; } label1.Text += " 平均" + " " + 身高加總 / 6 + " " + 體重加總 / 6 + " " + BMI加總/6 + Environment.NewLine; } private void button1_Click(object sender, EventArgs e) { this.Close(); } } }