using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
namespace MoneyChange
{
public partial class Form1 : Form
{
Guy joe;
Guy bob;
int bank = 100;
public void UpdateForm() 透過此類別可以即時更新標籤。
{
joesCash.Text = joe.Name + "has $" + joe.Cash;
bobsCash.Text = bob.Name + "has $" + bob.Cash;
bankCash.Text = "The bank has $" + bank;
}
public Form1()
{
InitializeComponent();
bob = new Guy();
bob.Name = "Bob";
bob.Cash = 100;
joe = new Guy();
joe.Name = "Joe";
joe.Cash = 50;
UpdateForm();
}
private void button2_Click(object sender, EventArgs e)
{
bank += bob.GiveCash(5);
UpdateForm();
}
private void button1_Click(object sender, EventArgs e)
{
if (bank >= 10)
{
bank -= joe.ReceiveCash(10);
UpdateForm();
}
else
{
MessageBox.Show("銀行沒錢錢囉!");
}
}
private void joeGivesToBob_Click(object sender, EventArgs e)
{
joe.Cash -= joe.GiveCash(10);
bob.Cash += bob.ReceiveCash(10);
UpdateForm();
}
private void bobGivesToJoe_Click(object sender, EventArgs e)
{
bob.Cash -= bob.GiveCash(5);
joe.Cash += joe.ReceiveCash(5);
UpdateForm();
}
}
}
物件Guy程式碼
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
namespace MoneyChange
{
public class Guy
{
public string Name;
public int Cash;
public int GiveCash(int amount)
{
if (amount <= Cash && amount > 0)
{
Cash -= amount;
return amount;
}
else
{
MessageBox.Show("我沒錢給你!!");
return 0;
}
}
public int ReceiveCash(int amount)
{
if (amount > 0)
{
Cash += amount;
return amount;
}
else
{
MessageBox.Show("我沒收到錢!!");
return 0;
}
}
}
}
編譯:
沒有留言:
張貼留言