開発環境
- OS: Windows 10 Pro
- IDE(統合開発環境): Visual Studio Community 2017
Head First C# ―頭とからだで覚えるC#の基本 (Andrew Stellman (著)、Jennifer Greene (著)、佐藤 嘉一 (監修, 監修)、木下 哲也 (翻訳)、オライリージャパン)の3章(オブジェクト指向になる! - わかりやすいコードにする)、エクササイズ(p. 113)を取り組んでみる。
コード
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 WindowsFormsApp7
{
public partial class Form1 : Form
{
Guy joe;
Guy bob;
int bank = 10000;
public Form1()
{
InitializeComponent();
joe = new Guy();
joe.name = "ジョー";
joe.cash = 5000;
bob = new Guy();
bob.name = "ボブ";
bob.cash = 10000;
UpdateForm();
}
private void UpdateForm()
{
joesCash.Text = joe.name + "の所持金は" + joe.cash + "円";
bobsCash.Text = bob.name + "の所持金は" + bob.cash + "円";
bankCash.Text = "銀行の所持金は" + bank + "円";
}
private void button1_Click(object sender, EventArgs e)
{
if (bank >= 1000)
{
bank -= joe.ReceiveCash(1000);
UpdateForm();
}
else
{
MessageBox.Show("銀行にお金がありません。");
}
}
private void button2_Click(object sender, EventArgs e)
{
bank += bob.GiveCash(500);
UpdateForm();
}
}
}
0 コメント:
コメントを投稿