Menu
CoddyTech

تقسيم الفاتورة

جزء من قسم الأساسيات في رحلة Swift على Coddy. الدرس 40 من 86.

challenge icon

التحدي

سهل

في الدرس السابق، حسبت مبلغ الإكرامية والإجمالي شاملًا الإكرامية. الآن، أضف الحساب النهائي لتحديد المبلغ الذي يدفعه كل شخص عند تقسيم الفاتورة.

باستخدام totalWithTip الذي حسبته بالفعل وnumberOfPeople، احسب المبلغ الذي ينبغي أن يدفعه كل شخص بقسمة الإجمالي على عدد الأشخاص.

تذكّر تحويل numberOfPeople من Int إلى Double قبل إجراء القسمة.

حدّث مخرجاتك لتطبع بهذا التنسيق بالضبط:

Welcome to the Bill Split Calculator!
Bill: [billTotal]
Tip: [tipPercentage]%
People: [numberOfPeople]
Tip amount: [tipAmount]
Total with tip: [totalWithTip]
Each person pays: [amountPerPerson]

على سبيل المثال، إذا كانت المدخلات هي 100.0 و20 و4، فينبغي أن تكون المخرجات:

Welcome to the Bill Split Calculator!
Bill: 100.0
Tip: 20%
People: 4
Tip amount: 20.0
Total with tip: 120.0
Each person pays: 30.0

جرّب بنفسك

print("Welcome to the Bill Split Calculator!")

if let billInput = readLine(), let billTotal = Double(billInput) {
    if let tipInput = readLine(), let tipPercentage = Int(tipInput) {
        if let peopleInput = readLine(), let numberOfPeople = Int(peopleInput) {
            let tipAmount = billTotal * Double(tipPercentage) / 100
            let totalWithTip = billTotal + tipAmount
            
            print("Bill: \(billTotal)")
            print("Tip: \(tipPercentage)%")
            print("People: \(numberOfPeople)")
            print("Tip amount: \(tipAmount)")
            print("Total with tip: \(totalWithTip)")
        }
    }
}

جميع دروس الأساسيات

تدرّب بنفسك: Playground لـ Swift