Head First C#
頭とからだで覚えるC#の基本
(オライリージャパン)
Andrew Stellman (著), Jennifer Green (著)
佐藤 嘉一 (監修), 木下 哲也 (翻訳)
開発環境
- Microsoft Windows 8.1 Pro (VMware Fusion 6, OS X Mavericks - Apple) (OS)
- C# (プログラミング言語)
- Microsoft Visual Studio Express 2013 for Windows Desktop (統合開発環境, IDE)
Head First C#―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Green (著)、佐藤 嘉一 (監修)、木下 哲也 (翻訳)、オライリージャパン)の4章(型と参照: 10時です。データがどこにあるかわかりますか?)、プールパズル(p.159)を解いてみる。
プールパズル(p.159)
コード
Form1.cs
using System;
using System.Windows.Forms;
class Triangle
{
double area;
int height;
int length;
static void Main()
{
string results = "";
int x = 0;
Triangle[] ta = new Triangle[4];
while (x < 4)
{
ta[x] = new Triangle();
ta[x].height = (x + 1) * 2;
ta[x].length = x + 4;
ta[x].setArea();
results += "三角形 " + x + ", 面積";
// 1つ目の空白 面積 = 28
results += " = " + ta[x].area + "\n";
x = x + 1;
}
int y = x;
x = 27;
Triangle t5 = ta[2];
ta[2].area = 343;
// 2つ目の空白 y = 4,
results += "y = " + y;
// 2つ目の空白の続き , T5 面積 = 343
MessageBox.Show(results + ", t5 面積 = " + t5.area);
}
void setArea()
{
area = (height * length) / 2;
}
}
0 コメント:
コメントを投稿