Kodewick
开始学 →
小挑战←→0 / 70

电影分级

#017小挑战条件字符串约 6 分钟

电影院按分级放人:"G" 谁都能看;"PG-13" 要满 13 岁,或者有家长陪(withParent);"18" 一定要满 18 岁,家长陪也不行。写 CanWatch(age, rating, withParent),能看返回 true。分级只会是这三种。

示例
CanWatch(8, "G", false)→True
CanWatch(10, "PG-13", true)→True
CanWatch(16, "18", true)→False
函数func CanWatch(age int, rating string, withParent bool) bool
main.go
package main

func CanWatch(age int, rating string, withParent bool) bool {
	if rating == "G" {
		return true
	}
	// 在这里写:"PG-13" 和 "18" 各写一个 if,能看就返回 true
	return false
}
Ctrl + Enter 也能运行
结果

写好了先点「运行」试试示例,没问题再点「提交」。