<分区>
我正在用 html5 和 javascript 编写一个货币转换应用程序,并对转换率进行硬编码。问题是我希望能够在 33 种货币之间进行转换。这会产生一个问题,因为我能想到的唯一方法是使用 if 语句,这意味着我将不得不编写 1056 个 if 语句。 即
if (homeCountry = "uk" and destinationCountry = "france") {
rate = 1.32;
}
if (homeCountry = "uk" and destinationCountry = "japan") {
rate = 183.79;
}
对于所有英国转换,然后
if (homeCountry = "japan" and destinationCountry = "france") {
rate = 0.0074;
}
if (homeCountry = "japan" and destinationCountry = "china") {
rate = 0.053;
}
对于所有日语转换等
即使使用 switch 语句,这仍然需要输入大量代码。我想知道是否有人能想出一个解决方案来让它稍微不那么乏味。一段时间以来,我一直在尝试寻找解决方案,但我被卡住了。谁能帮忙?谢谢