我试图制作一个收银机计算器应用程序,帮助我更快地从寄存器中赚钱。例如:我可以输入20美元的单数,而不是将所有20美元的票据相加,而应用程序可以计算总数。这是我的代码,我使用了Struct和函数,但运行代码时出错。感谢
struct Money {
oneCent: Double
fiveCent: Double
tenCent: Double
quater: Double
fiftyCent: Double
rollCent: Double
rollFive: Double
rollTen: Double
rollQuater: Double
dollarCoin: Double
dollar: Double
fiveDollar: Double
tenDollar : Double
twentyDollar: Double
fiftyDollar: Double
hundredDollar: Double
oneCent: Double
}
func moneyCalcualate (value at : Money, copies: Double){
var total : Double = 0.00
for i in at{
switch i {
case oneCent : total = 0.01 * copies
case fiveCent : total = 0.05 * copies
case tenCent : total = 0.10 * copies
case quater : total = 0.25 * copies
case fiftyCent : total = 0.50 * copies
case rollCent: total = 0.01 * copies
case rollFive: total = 0.01 * copies
case rollTen : total = 0.01 * copies
case rollQuater : total = 10.00 * copies
case dollarCoin : total = 1.00 * copies
case dollar : total = 1.00 * copies
case fiveDollar : total = 5.00 * copies
case tenDollar : total = 10.00 * copies
case twentyDollar : total = 20.00 * copies
case fiftyDollar : total = 50.00 * copies
case hundredDollar : total = 100.00 * copies
}
}
return total
}
moneyCalcualate(value: quater, copies: 2.0)
答案 0 :(得分:1)
这是enum
的典型案例。将所有货币类型声明为案例并为每个类型分配原始值。然后,您可以创建一个函数,该函数将返回当前类型的给定份数的总数。
以下是一个例子:
enum Money: Double {
case oneCent = 0.01
case fiveCent = 0.05
case tenCent = 0.1
case quarter = 0.25
func total(numberOfCopies: Int) -> Double {
return self.rawValue * Double(numberOfCopies)
}
}
你这样称呼它:
let moneyInFiveCents = Money.fiveCent
let fiveCents = moneyInFiveCents.total(numberOfCopies: 5)
print(fiveCents)
// prints 0.25
let moneyInQuarters = Money.quarter
let quarters = moneyInQuarters.total(numberOfCopies: 5)
print(quarters)
// prints 1.25
print(fiveCents + quarters)
// prints 1.5
答案 1 :(得分:0)
你可以这样试试..
enum Money {
case oneCent
case fiveCent
case tenCent
case quater
case fiftyCent
case rollCent
case rollFive
case rollTen
case rollQuater
case dollarCoin
case dollar
case fiveDollar
case tenDollar
case twentyDollar
case fiftyDollar
case hundredDollar
static let all : [Money] = [.oneCent , .fiveCent , .tenCent , .quater, .fiftyCent, .rollCent, .rollFive, .rollTen, .rollQuater, .dollarCoin, .dollar, .fiveDollar, .tenDollar, .twentyDollar, .fiftyDollar, .hundredDollar]
}
func moneyCalcualate (value at : Money, copies: Double) -> Double{
var total : Double = 0.00
switch at {
case .oneCent : total = 0.01 * copies
case .fiveCent : total = 0.05 * copies
case .tenCent : total = 0.10 * copies
case .quater : total = 0.25 * copies
case .fiftyCent : total = 0.50 * copies
case .rollCent: total = 0.01 * copies
case .rollFive: total = 0.01 * copies
case .rollTen : total = 0.01 * copies
case .rollQuater : total = 10.00 * copies
case .dollarCoin : total = 1.00 * copies
case .dollar : total = 1.00 * copies
case .fiveDollar : total = 5.00 * copies
case .tenDollar : total = 10.00 * copies
case .twentyDollar : total = 20.00 * copies
case .fiftyDollar : total = 50.00 * copies
case .hundredDollar : total = 100.00 * copies
}
return total
}
moneyCalcualate(value: .quater, copies: 2.0)
希望它有所帮助!!