使用多个计算函数创建简单类()

时间:2015-06-30 03:22:26

标签: swift function class add

尝试通过我班上的一个函数运行一个简单的计算。我只想添加bill1 + bill2并打印花在账单上的总金额。所以(bill1 + bill2 =总计)。然后打印总金额。

当前错误状态 - "'返回'之后的代码将永远不会被执行。"现在,我的位置是否在错误的位置进行打印,或者我是否错误地声明了我的变量?我应该使用vars而不是let吗?

为了计算和打印结果,您对我的功能有什么建议?

class BillsCalculator
{
    let nameOfBill1: String = "Medical"
    let nameOfBill1: String = "Hulu"
    let monthlyBillAmount1: Double = 34.25
    let monthlyBillAmount2: Double = 7.99
    let calculateTotalsPerMonth: Double = 0.0

   //calculateTotalPerMonth ( = monthlyBillAmount_1 + monthlyBillAmount_2 + 3)

    func calculateTotalsPerMonth(monthlyBillAmount: Double, monthlyBillAmount2: Double) -> Double
    {
        //totalBillsPerMonth = add(monthlyBillAmount1 + monthlyBillAmount2)
        return totalBillsPerMonth(monthlyBillAmount1 + monthlyBillAmount2)
*Error        println("You spend \(totalBillsPerMonth)")

    }
}

4 个答案:

答案 0 :(得分:2)

首先 “'返回'后的代码永远不会被执行。”

是的,它不会,在您调用return后退出该函数并返回调用它的函数,您可能在XCode中发出警告警告您告诉您

第二次 “我应该使用vars代替let吗”

如果值发生变化,必须使用var,如果不是,应该使用let。

我可以在您的代码中看到一些问题:

class BillsCalculator
{
    //use _ in the beginning of the name for class variables
    //eg. _nameOfBill instead nameOfBill1
    //It is not wrong use nameOfBill1 is just not recommended 
    //if nameOfBill1 change use var
    let nameOfBill1: String = "Medical"
    //Why is this declare twice
    let nameOfBill1: String = "Hulu" 
    //Those values look like change should be var
    var monthlyBillAmount1: Double = 34.25
    var monthlyBillAmount2: Double = 7.99
    var calculateTotalsPerMonth: Double = 0.0
    func calculateTotalsPerMonth(monthlyBillAmount: Double, monthlyBillAmount2: Double) -> Double
    {
        totalBillsPerMonth = add(monthlyBillAmount1 + monthlyBillAmount2)
        //print before return
        println("You spend \(totalBillsPerMonth)")
        return totalBillsPerMonth(monthlyBillAmount1 + monthlyBillAmount2)

    }
}

答案 1 :(得分:0)

在这里,您应该打印总账单的价值或返回该值。因为你只想打印总账单金额所以我建议你只打印,而不是退货。您可以参考以下代码。

class BillsCalculator
{
    let nameOfBill1: String = "Medical"
    let nameOfBill1: String = "Hulu"
    let monthlyBillAmount1: Double = 34.25
    let monthlyBillAmount2: Double = 7.99
    let calculateTotalsPerMonth: Double = 0.0

   //calculateTotalPerMonth ( = monthlyBillAmount_1 + monthlyBillAmount_2 + 3)

    func calculateTotalsPerMonth(monthlyBillAmount: Double, monthlyBillAmount2: Double) -> Double
    {
        calculateTotalsPerMonth= add(monthlyBillAmount1 + monthlyBillAmount2)
             println("You spend : "+totalBillsPerMonth);

    }
}

答案 2 :(得分:0)

代码中的一个小错误

    let nameOfBill1: String = "Medical"
    let nameOfBill1: String = "Hulu"

这两个变量具有相同的名称,也许应该是:

    let nameOfBill2: String = "Hulu"

是的return总是函数的最后一行,所以返回后的所有代码都不会被执行。如果您只想获得两张账单,您可以这样做:

    func calculateTotalsPerMonth(monthlyBillAmount: Double, monthlyBillAmount2: Double) -> Double {
         //println("You spend \(totalBillsPerMonth)")
         return monthlyBillAmount1 + monthlyBillAmount2
     }

并使用您的帐单变量调用此函数,例如:

    let bill1 = 34.25
    let bill2 = 7.99
    let totalBill = calculateTotalsPerMonth(bill1, bill2)
    println("You spent \(totalBill)")

Swift是一种非常智能的语言,它是类型安全的。您可以根据需要删除类型,更像是个人编程风格的东西。

    let bill1: Double = 34.25
    let bill1 = 34.25

它们都是“Double”类型

答案 3 :(得分:0)

正如其他人所说,你需要在println之前放置return语句,因为返回结束了方法的执行;因此println永远不会被运行。

但是,我建议您对当前的方法进行一些更改:

// A bill is an object - why not encapsulate it in a struct.
struct Bill {
    let name: String
    let amount: Double
}

// Using structs is generally preferred, unless you need inheritance and/or
// references to your BillsCalculator objects.
struct BillsCalculator {
    let bill1: Bill
    let bill2: Bill

    // Using a read-only computed property means you don't need to set 
    // the total to have an initial value of zero.
    var totalBilled: Double {
        return bill1.amount + bill2.amount
    }
}

// Since you're probably going to want to reuse BillsCalculator, 
// don't have each bill set already. Instead, use BillsCalculator's 
// initialiser and pass in bills.
let bill1 = Bill(name: "Medical", amount: 34.25)
let bill2 = Bill(name: "Hulu",    amount: 7.99)
let cal = BillsCalculator(bill1: bill1, bill2: bill2)

print("You've spend \(cal.totalBilled) this month")