var total: Int = 0 // 定义一个用于存储总金额的变量,并初始化为0
// 遍历所有货币面值
for coin in [500, 200, 100, 50, 20, 10, 5, 2, 1] {
print("Do you have \(coin)鈧 coins?") // 打印询问消息
if let input = readLine(), let qty = Int(input) { // 读取用户输入,转换为整数
total += coin * qty // 将用户输入的数量和当前面值相乘,加到总金额上
}
}
print("Total: \(total)鈧") // 打印总金额
以上代码会遍历所有货币面值,询问用户是否拥有该面值的硬币,然后读取用户输入的数量,并将其乘以面值,加到总金额上。最后,打印总金额。
上一篇:Askifthisnumberisoddoreven,asmanytimesyouwant
下一篇:Askingtheusertoreviewtheappafteracertainnumberoflaunches