基本货币转换器,为什么不编译? [英] basic currency converter, why doesn't it compile?

查看:98
本文介绍了基本货币转换器,为什么不编译?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对编程来说真的是新手,所以我希望这个问题不会太烦人,但是我碰壁了。我必须在c#中创建一个货币转换器,其中有5种货币,每种货币具有规定的价值。控制台应询问用户他们要转换多少,要转换为什么,然后显示类似您输入了25欧元,转换为27美元

Really new to programming so I hope this question isn't too annoying but I have hit a wall. I have to make a currency converter in c#, where there is 5 currencies with a stated value each. The console should ask thee user how much they want to convert, what they want to convert to and then display something like "you entered 25 EUR which converts to 27 USD

        double amount;
        double USD = 1.39;
        double GBP = .82;
        /*double CHF = 1.22;
        double AUD = 1.48;
        double CAD = 1.51;*/
        string currency;

        Console.WriteLine("Please enter the amount of Euro you wish to be converted:");
        amount = double.Parse(Console.ReadLine());

        Console.WriteLine("");
        Console.WriteLine("Please choose the currency you wish to convert to:");
        Console.WriteLine("USD");
        Console.WriteLine("GBP");
        Console.WriteLine("CHF");
        Console.WriteLine("AUD");
        Console.WriteLine("CAD"); 
        Console.WriteLine("");
        currency = Console.ReadLine();

        switch (currency)
        {
            case "USD":
            Console.WriteLine("You have entered {0} EUR which converts to {1} {2}", amount,      amount * currency, currency);
                break;
            case "GBP":

            default:
            Console.WriteLine("You did not enter a valid currency");
                break;
            }
            Console.ReadLine();

运算符*不能应用于double和string类型的运算符是Im收到的错误消息。

"Operator * cannot be applied to operators of type double and string" is the error message Im receiving.

我该如何解决?

推荐答案

我正在阅读放在我Windows手机的床上,这很困扰我。所以,现在我要在深夜里输入以下内容!

Well I was reading this in bed on my windows phone and it was bugging me. So now I am up late at night typing this!

问题是您正在以货币形式读取包含美元,英镑的字符串:

The problem is you are reading in currency as a string which will contain USD, GBP:

currency = Console.ReadLine();

然后将金额作为字符串读取并转换为双精度-例如300.0:

And reading amount in as a string and converting to a double - for example 300.0:

amount = double.Parse(Console.ReadLine());

然后在乘法中使用两个变量来获取转换后的金额:

You are then using both variables in a multiplication to get the converted amount:

Console.WriteLine("You have entered {0} EUR which converts to {1} {2}", amount,      amount * currency, currency);

有金额*货币。

您有2种类型不能相乘。弦和双!因此,您会收到一个设计时错误。您将在Visual Studio错误窗口中看到以下内容:

You have 2 types which cannot be multiplied together. String and Double! Thus you get a design-time error. Which you will see in the Visual Studio errors window:

Error   1   Operator '*' cannot be applied to operands of type 'double' and 'string'

您基本上想使用输入的货币 USD, GBP ,然后从您已命名为USD或GBP的变量中获取适当的转换因子。

You basically want to take the entered currency which is "USD", "GBP" and get the appropriate conversion factor from the variable you have named USD or GBP.

因此,要使代码起作用,您需要执行以下操作:

So in order to make your code "work" you need to do something like as follows:

switch (currency)
{
    case "USD":
        Console.WriteLine("You have entered {0} EUR which converts to {1} {2}", amount,      amount * USD, currency);  // <-- here is the change
            break;
    ...
}

在交换机中...如果您知道什么是货币价值。上面的例子是 USD,因此您需要引用USD变量。

In the switch... case you know what the currency value is. The above is case "USD" so you need to reference the USD variable.

为使您的代码更好,请摆脱该开关。我希望有一个字典,您可以在其中实质上存储您的字符串(GBP,USD等),并且每个字符串都具有关联的转换因子。但是您将被引入泛型领域,但是只要掌握一些基础知识就是您最好的编程入门。

To make your code better, get rid of that switch. I would prefer a Dictionary where you could essentially store your strings (GBP, USD, etc) and have an associated conversion factor with each. But you would be getting introduced to the world of generics, but just get a grasp of some of the basics would be your best introduction to programming. But here you go!

        double amount;
        /* Dont need to declare these explicitly!            
        double USD = 1.39;
        double GBP = .82;
        double CHF = 1.22;
        double AUD = 1.48;
        double CAD = 1.51;*/
        string currency;
        Dictionary<string, double> factors = new Dictionary<string, double>();
        factors.Add("GBP", 0.82D);
        factors.Add("USD", 1.39D);
        // ... add other factors

        Console.WriteLine("Please enter the amount of Euro you wish to be converted:");
        amount = double.Parse(Console.ReadLine());

        Console.WriteLine("");
        Console.WriteLine("Please choose the currency you wish to convert to:");
        Console.WriteLine("USD");
        Console.WriteLine("GBP");
        Console.WriteLine("CHF");
        Console.WriteLine("AUD");
        Console.WriteLine("CAD");
        Console.WriteLine("");
        currency = Console.ReadLine();

        double factor;
        if (factors.TryGetValue(currency, out factor))
        {
            Console.WriteLine("You have entered {0} EUR which converts to {1} {2}", amount, amount * factor, currency);
        }
        else
        {
            Console.WriteLine("You did not enter a recognised currency {1}", currency);
        }

这篇关于基本货币转换器,为什么不编译?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆