Menu
Coddy logo textTech
flag Ar iconالعربيةdown icon

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

جزء من قسم الأساسيات في رحلة 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)")
        }
    }
}

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