编程真的很新,所以我希望这个问题不会太烦人,但我已经碰壁了。我必须在c#中制作货币转换器,其中有5种货币,每种货币都有一个规定值。控制台应该询问用户他们想要转换多少,他们想要转换成什么,然后显示“你输入25欧元转换为27美元
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类型和字符串的运算符”是我收到的错误消息。
如何解决此问题?
答案 0 :(得分:2)
实际上,此代码将尝试将货币的价值与汇率进行比较。虽然这可能是一个有趣的想法,但我认为这不是你想要的。首先,currency
可能应该是string
,所以我会采用这个假设。您的比较应该针对strings
,而不是双倍,但您键入的内容会将currency
与您之前定义的双倍比较,以表示汇率。你可能想要的是:
if (currency == "USD")
Console.WriteLine("You have entered {0} EUR which converts to {1} {2}", amount, amount * currency, currency);
等等。但是,在这种情况下,使用switch语句会使代码看起来更好:
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;
}
编辑1:
要解释为什么不编译,问题是你有语句
currency == USD
此语句表示将变量currency
与变量USD
进行比较。 USD
为双倍,currency
为string
。编译器不知道如何比较double
和string
(至少使用==
运算符)。在"USD"
之类的常量周围加上双引号,告诉编译器改为生成具有该常量值的临时string
。当然,==
运算符在strings
之间工作,所以
double USD = 1;
String currency = "1";
bool result = currency == USD;
bool result2 = currency == 1;
生成两个编译器错误,
String USD = "1";
String currency = "1";
bool result = currency == USD;
bool result2 = currency == "1";
两种陈述都是如此。
答案 1 :(得分:1)
货币变量应为字符串,比较应如if (currency == "USD")
答案 2 :(得分:1)
好吧,我在Windows手机上的床上读这个,这让我很烦。所以现在我已经深夜打字了!
问题是您正在以货币形式读取包含USD,GBP:
的字符串currency = Console.ReadLine();
读取数量为字符串并转换为double - 例如300.0:
amount = double.Parse(Console.ReadLine());
然后在乘法中使用两个变量来获得转换量:
Console.WriteLine("You have entered {0} EUR which converts to {1} {2}", amount, amount * currency, currency);
使用金额*货币。
您有两种不能相乘的类型。字符串和双!因此,您会收到设计时错误。您将在Visual Studio错误窗口中看到:
Error 1 Operator '*' cannot be applied to operands of type 'double' and 'string'
您基本上想要输入的货币为“USD”,“GBP”,并从您名为USD或GBP的变量中获取相应的转换因子。
因此,为了使您的代码“正常”,您需要执行以下操作:
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变量。
为了让你的代码更好,摆脱那个开关。我更喜欢一个字典,你可以基本上存储你的字符串(英镑,美元等),并与每个字符串有一个相关的转换因子。但是你会被介绍到泛型世界,但只是掌握一些基础知识将是你最好的编程入门。但是你走吧!
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);
}
答案 3 :(得分:0)
使用以下代码。
overflow-x:hidden