promotion image of download ymail app
Promoted

關於C#匯率轉換疑問

https://docs.google.com/document/d/1EAdTewqCky-qA2...

程式語言在此↑↑↑

請問一下 為什麼到最後會只有新台幣可以轉成其他國家幣值

美金 日幣 歐元 澳幣都不能轉??

請問程式哪邊出了錯誤..

已更新項目:

抱歉,還是不行...

還是我把整個資料夾壓縮用信箱寄給您

請您幫我診斷哪邊出錯??

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 補充:

    https://docs.google.com/document/d/1cRrGojdjbEdTjL...

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

    • Commenter avatar登入以對解答發表意見
  • 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" })

    {

    listBox1.Items.Add(s);

    listBox2.Items.Add(s);

    }

    }

    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();

    currency.Add(new ExchangeRate("TWD", "USD", 0.03378));

    currency.Add(new ExchangeRate("TWD", "EUR", 0.024736));

    // ... 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 void Add(ExchangeRate rate)

    {

    rates.Add(rate);

    }

    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;

    }

    }

    }

    • Commenter avatar登入以對解答發表意見
還有問題?馬上發問,尋求解答。