欢迎光临散文网 会员登陆 & 注册

iOS移动应用程序开发 | 第四讲-函数和枚举:Reusing Code ...

2023-01-10 08:28 作者:陈鹏宇老师  | 我要投稿


01:23:07


第四次作业

作业一:完成Playgrounds App《学习编程2》第4、5章


作业二:完成下面的练习题

1. Write a billSplit function to help calculate how to evenly split a dining bill. The function should take parameters for the total cost of the meal (a Double), a tip percentage (an Int), and a number of diners (an Int). It should print how much each diner will need to pay.


2. Write a better version of the billSplit function to help calculate how to evenly split a dining bill. This time, the function should have sensible defaults for some of the values: 2 diners, 20% tip. It should still print how much each diner will need to pay.


3 (i) Create a new enumeration to represent movie genres. It should contain a handful of movie genres, of your choice Once you’ve created it, make a switch statement, and switch on a variable storing a genre (maybe call it favoriteGenre?). Print out a sassy comment for each genre. 

(ii) With that working, try creating a new enumeration that represents cocktails. For each cocktail, store a raw string value describing the cocktail. With that done, iterate through the cases of the enum using a loop, and print the description of each cocktail.


4 (i) Write a closure that takes a single integer as its parameter, and returns that integer multiplied by itself inside a string.

(ii) Write some code to use that closure, by passing in the value 10 (it should print "The result is 100" or similar).

(iii) Now, update the closure and make it take two integers as parameters. Test that closure with 10 and 50 (it should return "The result is 500" or similar).


5. BE the Swift Compiler

Each of the Swift code snippets below represents something about closures. Your job is to play Swift compiler, determine whether each one is valid, and explain why.


iOS移动应用程序开发 | 第四讲-函数和枚举:Reusing Code ...的评论 (共 条)

分享到微博请遵守国家法律