func chekGrade(_ grade: Int) -> String {
switch grade {
case 90...100:
return "优秀"
case 80...89:
return "良好"
case 70...79:
return "中等"
case 60...69:
return "及格"
case 0...59:
return "不及格"
default: return "请输入正确的成绩"
}
}
print(chekGrade(89))
print(chekGrade(50))
print(chekGrade(101))
enum VendingMachineError: Error {
case invalidSelection
case insufficientFunds(coinsNeeded: Int)
case outOfStock
}
struct Item {
var price: Int
var count: Int
}
class VendingMachine {
var inventory = [
"Candy Bar": Item(price: 12, count: 7),
"Chips": Item(price: 10, count: 4),
"Pretzels": Item(price: 7, count: 11)
]
var coinsDeposited = 0
func vend(itemNamed name: String) throws {
guard let item = inventory[name] else {
throw VendingMachineError.invalidSelection
}
guard item.count > 0 else {
throw VendingMachineError.outOfStock
}
guard item.price <= coinsDeposited else {
throw VendingMachineError.insufficientFunds(coinsNeeded: item.price - coinsDeposited)
}
coinsDeposited -= item.price
var newItem = item
newItem.count -= 1
inventory[name] = newItem
print("Dispensing \(name)")
}
}
var hello = VendingMachine()
hello.coinsDeposited = 10
try hello.vend(itemNamed: "Chips")
print(hello.coinsDeposited)