2017-10-06 95 views
-2

我試圖製作一個收銀員計算器應用程序,該應用程序可以幫助我更快地從寄存器中進行計數。例如:不是將所有20美元的票據加起來,我只需輸入20美元的單曲數,我可以計算出總數。這是我的代碼,我使用了一個Struct和一個函數,但是運行代碼時出錯。由於在使用Struct和函數時在swift 3中遇到問題

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

此代碼甚至不進行編譯;這裏有很多錯誤。告訴我們哪個錯誤,具體來說,你需要幫助,如果適用,你已經嘗試了什麼。 –

+0

如何從moneyCalcualate函數中的'value at:Money'變量開始for循環? –

+0

你需要使用出租或VAR –

回答

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) 

希望它可以幫助!

+0

感謝特工史密斯。靜態的目的是什麼?這是我第一次看到它。它是否將我們的金錢枚舉中的所有值變成了數組? – Perewillz

+0

我正在製作所有枚舉值的數組,以便我們可以遍歷它們。 @Perewillz –

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 
相關問題