以下是使用ifelse制作人民币兑其他国家货币的货币转换程序的示例代码:

currency_type <- readline(prompt = '请输入货币类型(CNY/USD/EUR/JPY):')
amount <- as.numeric(readline(prompt = '请输入兑换金额:'))

if (currency_type == 'CNY') {
  USD <- amount * 0.14
  EUR <- amount * 0.12
  JPY <- amount * 15.02
  cat(sprintf('兑换成美元为%.2f,兑换成欧元为%.2f,兑换成日元为%.2f', USD, EUR, JPY))
} else if (currency_type == 'USD') {
  CNY <- amount * 7.10
  EUR <- amount * 0.85
  JPY <- amount * 106.02
  cat(sprintf('兑换成人民币为%.2f,兑换成欧元为%.2f,兑换成日元为%.2f', CNY, EUR, JPY))
} else if (currency_type == 'EUR') {
  CNY <- amount * 8.16
  USD <- amount * 1.18
  JPY <- amount * 125.06
  cat(sprintf('兑换成人民币为%.2f,兑换成美元为%.2f,兑换成日元为%.2f', CNY, USD, JPY))
} else if (currency_type == 'JPY') {
  CNY <- amount * 0.066
  USD <- amount * 0.0094
  EUR <- amount * 0.0080
  cat(sprintf('兑换成人民币为%.2f,兑换成美元为%.2f,兑换成欧元为%.2f', CNY, USD, EUR))
} else {
  cat('请输入正确的货币类型')
}

程序首先让用户输入要兑换的货币类型和金额,然后使用ifelse语句根据货币类型计算出兑换成其他货币的金额,并使用sprintf和cat函数输出结果。如果用户输入的货币类型不在预设的四种货币类型之内,则输出错误提示信息。

人民币兑其他货币转换器:使用ifelse实现

原文地址: https://www.cveoy.top/t/topic/oOuq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录