我正在研究编程原则和实践'在使用if else编码货币转换后,我正在使用switch语句编写一个。
我有5种货币可以转换,其中4种货币完美运行,而1(欧元,E)则没有。我不明白为什么,虽然我确定这很简单。
这是我的代码;
#include <iostream>
int main(){
constexpr double yen_to_dollars = 0.0091;
constexpr double euro_to_dollars = 1.071;
constexpr double pound_to_dollars = 1.279;
constexpr double yuan_to_dollars = 0.1452;
constexpr double kroner_to_dollars =0.1174;
double amount = 1;
char currency = ' ';
std::cout<<"Please enter an amount, followed by the type of currency you want to convert (Y, E, P, Z, K): \n";
std::cin >> amount >> currency;
switch(currency){
case 'Y':
std::cout << amount << " Yen = " << yen_to_dollars*amount << " dollars\n";
break;
case 'E':
std::cout << amount << " Euros = " << euro_to_dollars*amount << " dollars\n";
break;
case 'P':
std::cout << amount << " Pounds = " << pound_to_dollars*amount << " dollars\n";
break;
case 'Z':
std::cout << amount << " Yuan = " << yuan_to_dollars*amount << " dollars\n";
break;
case 'K':
std::cout << amount << " Krone = " << kroner_to_dollars*amount << " dollars\n";
break;
default:
std::cout << "That currency is unrecognised\n";
break;
}
}
如果还有其他建议让我的代码更好,请告诉我。感谢