Finish: 01-if-else - 条件判断
This commit is contained in:
@@ -13,7 +13,7 @@ func exercise1() {
|
||||
|
||||
// 使用if语句,判断age是否大于等于18
|
||||
// 如果是,输出"你已经成年了"
|
||||
if ____ {
|
||||
if age >= 18 {
|
||||
fmt.Println("你已经成年了")
|
||||
} else {
|
||||
fmt.Println("你还未成年")
|
||||
@@ -30,11 +30,11 @@ func exercise2() {
|
||||
// 80-89: 良好
|
||||
// 60-79: 及格
|
||||
// 0-59: 不及格
|
||||
if ____ {
|
||||
if score >= 90 {
|
||||
fmt.Printf("分数: %d, 等级: 优秀\n", score)
|
||||
} else if ____ {
|
||||
} else if score >= 80 {
|
||||
fmt.Printf("分数: %d, 等级: 良好\n", score)
|
||||
} else if ____ {
|
||||
} else if score >= 60 {
|
||||
fmt.Printf("分数: %d, 等级: 及格\n", score)
|
||||
} else {
|
||||
fmt.Printf("分数: %d, 等级: 不及格\n", score)
|
||||
@@ -47,7 +47,7 @@ func exercise3() string {
|
||||
// 使用带初始化语句的if,将字符串"42"转换为整数
|
||||
// 如果转换成功,返回字符串"转换成功: 转换后的值"
|
||||
// 如果转换失败,返回字符串"转换失败"
|
||||
if num, err := strconv.____; ____ {
|
||||
if num, err := strconv.Atoi("42"); err == nil {
|
||||
return fmt.Sprintf("转换成功: %d", num)
|
||||
} else {
|
||||
return "转换失败"
|
||||
@@ -66,7 +66,7 @@ func exercise4() string {
|
||||
// 2. 密码是"123456"
|
||||
// 3. 账户是激活状态
|
||||
// 三个条件都满足时,返回"登录成功",否则返回"登录失败"
|
||||
if ____ && ____ && ____ {
|
||||
if username == "admin" && password == "123456" && isActive == true {
|
||||
return "登录成功"
|
||||
} else {
|
||||
return "登录失败"
|
||||
@@ -81,7 +81,7 @@ func exercise5() bool {
|
||||
|
||||
// 使用if语句,判断"没有权限但可以是管理员"的情况
|
||||
// 提示:使用 ! 或 ||
|
||||
if ____ {
|
||||
if hasPermission || isAdmin {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
@@ -94,7 +94,7 @@ func exercise6() string {
|
||||
|
||||
// 使用if语句判断email是否包含"@"
|
||||
// 提示:使用strings.Contains函数
|
||||
if ____ {
|
||||
if strings.Contains(email, "@") {
|
||||
return "有效的邮箱地址"
|
||||
} else {
|
||||
return "无效的邮箱地址"
|
||||
@@ -102,7 +102,7 @@ func exercise6() string {
|
||||
}
|
||||
|
||||
func main() {
|
||||
fmt.Println("=== if-else 练习 ===\n")
|
||||
fmt.Println("=== if-else 练习 ===")
|
||||
|
||||
exercise1()
|
||||
exercise2()
|
||||
|
||||
Reference in New Issue
Block a user