# 關於C#匯率轉換疑問

2 個已更新項目:

### 2 個解答

• wei
Lv 6
6 年前
最佳解答

If的括號位置打錯了，變成必須是新台幣時，才繼續判斷下面

而且歐元兌換有些問題，我看看

2013-12-13 20:28:39 補充：

害我找好久...

在ListBox1選擇歐元那一段內的

if (listBox1.SelectedItem.Equals(listBox2.Items[0]))

應為

if (listBox2.SelectedItem.Equals(listBox2.Items[0]))

你比較錯誤了

2013-12-13 21:04:11 補充：

2013-12-13 21:26:10 補充：

wei199887@yahoo.com.tw

2013-12-13 21:46:33 補充：

因為你 if 的錯誤仍然沒改啊

• 登入以對解答發表意見
• John
Lv 7
6 年前

字數太多，請移玉步, 到參考資料.

我的提議。

using System;

using System.Collections.Generic;

using System.Windows.Forms;

namespace YahooProject

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

foreach (String s in new string[] { "TWD", "USD", "EUR", "JPY", "AUD" })

{

}

}

private void button1_Click(object sender, EventArgs e)

{

string source = listBox1.SelectedItem.ToString();

string target = listBox2.SelectedItem.ToString();

double baseAmount = Convert.ToDouble(textBox1.Text);

Currency currency = new Currency();

// ... continue ...

double amount = currency.Converter(source, target, baseAmount);

textBox2.Text = string.Format("{0:C}", amount);

}

}

public class Currency

{

private IList<ExchangeRate> rates = new List<ExchangeRate>();

{

}

public double Converter(string source, string target, double baseAmount)

{

double result = 0;

if (source.Equals(target)) return baseAmount;

foreach (ExchangeRate rate in rates)

{

result = rate.GetAmount(source, target, baseAmount);

if (result > 0) break;

}

return result;

}

}

public class ExchangeRate

{

public string Source { get; private set; }

public string Target { get; private set; }

public double Rate { get; private set; }

public ExchangeRate(string source, string target, double rate)

{

Source = source;

Target = target;

Rate = rate;

}

public double GetAmount(string source, string target, double amount)

{

double result = 0;

if (this.Source == source && this.Target == target)

{

result = this.Rate * amount;

}

return result;

}

}

}

• 登入以對解答發表意見