3.1-分支语句

3.1 分支语句

  • 对于需要先做判断,再选择如何处理的问题就要使用分支语句。

3.1.1 单分支语句

语法:

if 布尔表达式 { //左花括号必须与表达式同行
    // 在布尔表达式为true的时候执行下面的代码块
}

示例代码

package main

import "fmt"

func main() {
   fmt.Println("分支语句")
   fmt.Println("1) if 单分支语句")
   var name = "arthur"
   fmt.Println(name == "arthur")
   if name == "arthur" {
      // if为真执行的代码块
      fmt.Println("姓名匹配成功。")
   }
   fmt.Println("END")
}

3.1.2 双分支语句

  • 两条分支,根据布尔表达式结果,二选一执行

示例代码 1

package main

import "fmt"

func main() {
   fmt.Println("分支语句")
   fmt.Println("1) if 单分支语句")
   var name = "arthur"
   fmt.Println(name == "arthur")
   if name == "arthur" {
      // if为真执行的代码块
      fmt.Println("姓名匹配成功。")
   }
   fmt.Println("END")

   fmt.Println("2) if-else 双分支语句")

      //示例1:
      var age int
      fmt.Println("请输入您的年龄:")
      fmt.Scan(&age)
      if age >= 18 {
         // 表达式为真执行的代码块
         fmt.Println("恭喜,已成年。")
      } else {
         // 表达式为假执行的代码块
         fmt.Println("遗憾,还未成年。")
      }
      
}

示例代码 2

package main

import "fmt"

func main() {
   fmt.Println("2) if-else 双分支语句")
   // 示例2:
   var username, password string
   fmt.Println("请输入用户名和密码,以空格分隔:")
   fmt.Scan(&username, &password)
   if username == "arthur" && password == "123" {
      fmt.Println("登录成功。")
   } else {
      fmt.Println("用户名或密码错误。")
   }

}

3.1.3 if 多分支语句

  • 不管多少条分支,最终只能执行其中一条分支。

案例 1

package main

import (
   "fmt"
)

func main() {
   /*
      案例:
      成绩评级
      成绩数值范围0~100
      大于等于90 优秀
      大于等于80 良好
      大于等于60 及格
      小于60      不及格
   */
   var score int
   fmt.Println("请输入您的成绩:")
   fmt.Scan(&score)

   if score < 0 || score > 100 {
      fmt.Println("成绩数值输入范围不合法!请重新输入!")
   } else if score >= 90 {
      fmt.Println("成绩优秀")
   } else if score >= 80 {
      fmt.Println("成绩良好")
   } else if score >= 60 {
      fmt.Println("成绩及格")
   } else {
      fmt.Println("成绩不及格")
   }
}

案例 2

  • 分支嵌套案例
package main

import (
   "fmt"
)

func main() {
   var username, password string
   fmt.Println("请输入用户名和密码:")
   fmt.Scan(&username, &password)
   if username == "root" && password == "123" {
      // 登录成功执行如下代码块
      var score int
      fmt.Println("请输入您的成绩:")
      fmt.Scan(&score)
      if score < 0 || score > 100 {
         fmt.Println("输入成绩数值不合法,有效范围0~100,请重新输入!")
      } else if score >= 90 {
         fmt.Println("成绩优秀!")
      } else if score >= 80 {
         fmt.Println("成绩良好!")
      } else if score >= 60 {
         fmt.Println("成绩及格!")
      } else {
         fmt.Println("成绩不合格!")
      }
      fmt.Println("成绩查询结束!END")
   } else {
      fmt.Println("用户名或密码错误!")
   }

}

案例 3

package main

import (
   "fmt"
   "strconv"
   "strings"
)

func main() {
   /*
      案例:
      简单的根据生日判断星座
      不判断平年闰年,多层分支嵌套,没有太多实际意义,只为练习if多分支语句
      12.22~1.19 摩羯座
      1.20~2.18  水瓶座
      2.19~3.20  双鱼座
      3.21~4.19  白羊座
      4.20~5.20  金牛座
      5.21~6.21  双子座
      6.22~7.22  巨蟹座
      7.23~8.22  狮子座
      8.23~9.22  处女座
      9.23~10.23 天秤座
      10.24~11.22    天蝎座
      11.23~12.21    射手座
   */
   var birth string
   fmt.Println("请输入您的生日,按\"年-月-日\"的格式输入,如1992-7-1:")
   fmt.Scan(&birth)
   birth_Slice := strings.Split(birth, "-")
   birth_Year, _ := strconv.Atoi(birth_Slice[0])
   birth_Month, _ := strconv.Atoi(birth_Slice[1])
   birth_Day, _ := strconv.Atoi(birth_Slice[2])
   fmt.Println(birth_Year)
   if birth_Month == 1 {
      if birth_Day >= 1 && birth_Day <= 19 {
         fmt.Println("您是摩羯座!")
      } else if birth_Day >= 20 && birth_Day <= 31 {
         fmt.Println("您是水瓶座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 2 {
      if birth_Day >= 1 && birth_Day <= 18 {
         fmt.Println("您是水瓶座!")
      } else if birth_Day >= 19 && birth_Day <= 29 {
         fmt.Println("您是双鱼座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 3 {
      if birth_Day >= 1 && birth_Day <= 20 {
         fmt.Println("您是双鱼座!")
      } else if birth_Day >= 21 && birth_Day <= 31 {
         fmt.Println("您是白羊座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 4 {
      if birth_Day >= 1 && birth_Day <= 19 {
         fmt.Println("您是白羊座!")
      } else if birth_Day >= 20 && birth_Day <= 30 {
         fmt.Println("您是金牛座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 5 {
      if birth_Day >= 1 && birth_Day <= 21 {
         fmt.Println("您是金牛座!")
      } else if birth_Day >= 22 && birth_Day <= 31 {
         fmt.Println("您是双子座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 6 {
      if birth_Day >= 1 && birth_Day <= 22 {
         fmt.Println("您是双子座!")
      } else if birth_Day >= 23 && birth_Day <= 30 {
         fmt.Println("您是巨蟹座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 7 {
      if birth_Day >= 1 && birth_Day <= 22 {
         fmt.Println("您是巨蟹座!")
      } else if birth_Day >= 23 && birth_Day <= 31 {
         fmt.Println("您是狮子座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 8 {
      if birth_Day >= 1 && birth_Day <= 22 {
         fmt.Println("您是狮子座!")
      } else if birth_Day >= 23 && birth_Day <= 31 {
         fmt.Println("您是处女座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 9 {
      if birth_Day >= 1 && birth_Day <= 22 {
         fmt.Println("您是处女座!")
      } else if birth_Day >= 23 && birth_Day <= 30 {
         fmt.Println("您是天秤座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 10 {
      if birth_Day >= 1 && birth_Day <= 23 {
         fmt.Println("您是天秤座!")
      } else if birth_Day >= 24 && birth_Day <= 31 {
         fmt.Println("您是天蝎座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 11 {
      if birth_Day >= 1 && birth_Day <= 22 {
         fmt.Println("您是天蝎座!")
      } else if birth_Day >= 23 && birth_Day <= 30 {
         fmt.Println("您是射手座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   } else if birth_Month == 12 {
      if birth_Day >= 1 && birth_Day <= 21 {
         fmt.Println("您是射手座!")
      } else if birth_Day >= 22 && birth_Day <= 31 {
         fmt.Println("您是摩羯座!")
      } else {
         fmt.Println("12月日期输入不合法,请重新输入。")
      }
   } else {
      fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
   }
}

3.1.4 switch 多分支语句

  • switch 语句也是多分支选择语句

  • 执行哪一个代码块,取决于 switch 后的值与哪一个 case 匹配成功,则执行该 case 后的代码块。

  • switch 比 if else 更为简洁;

  • 执行效率更高。switch...case 会生成一个跳转表来指示实际的 case 分支的地址,而这个跳转表的索引号与 switch 变量的值是相等的。从而,switch...case 不用像 if..else 那样遍历条件分支直到命中条件,而只需访问对应索引号的表项从而到达定位分支的目的。

  • 到底使用哪一个分支选择语句,和代码使用需求环境有关。

    • 如果是范围取值,则使用 if...else 语句更为快捷;
    • 如果是确定取值,则使用 switch 是更优方案。
  • switch 支持多条件确定值匹配

switch{
    case 1,2:
        ...
    default:
        ...
}

语法:

switch var{
    case val1:
        ...
    case val2:
        ...
    case val3:
        ...
    default:
        ...
}

代码示例:

package main

import "fmt"

func main() {
   /*
      多分支语句案例:
      输入数字,打印星期几 0是星期日
   */
   var week int
   fmt.Println("请输入星期的数字(0~6):")
   fmt.Scan(&week)
   /*
      // if - else if - else if ... else 多分支语句
      if week == 0 {
         fmt.Println("星期日")
      } else if week == 1 {
         fmt.Println("星期一")
      } else if week == 2 {
         fmt.Println("星期二")
      } else if week == 3 {
         fmt.Println("星期三")
      } else if week == 4 {
         fmt.Println("星期四")
      } else if week == 5 {
         fmt.Println("星期五")
      } else if week == 6 {
         fmt.Println("星期六")
      } else {
         fmt.Println("输入数字不合法,请重新输入。")
      }
   */

   // switch 多分支语句
   if week >= 0 && week <= 6 {
      switch week {
      case 1:
         fmt.Println("星期一")
      case 2:
         fmt.Println("星期二")
      case 3:
         fmt.Println("星期三")
      case 4:
         fmt.Println("星期四")
      case 5:
         fmt.Println("星期五")
      case 6:
         fmt.Println("星期六")
      case 0:
         fmt.Println("星期日")
      }
   } else {
      fmt.Println("输入数字不合法,请重新输入!")
   }
}

案例改写:

  • 上述 if...else 判断星座案例使用 switch 嵌套 if...else 改写
package main

import (
   "fmt"
   "strconv"
   "strings"
)

func main() {
   /*
      案例:
      简单的根据生日判断星座
      不判断平年闰年,多层分支嵌套,没有太多实际意义,只为练习if多分支语句
      12.22~1.19 摩羯座
      1.20~2.18  水瓶座
      2.19~3.20  双鱼座
      3.21~4.19  白羊座
      4.20~5.20  金牛座
      5.21~6.21  双子座
      6.22~7.22  巨蟹座
      7.23~8.22  狮子座
      8.23~9.22  处女座
      9.23~10.23 天秤座
      10.24~11.22    天蝎座
      11.23~12.21    射手座
   */
   var birth string
   fmt.Println("请输入您的生日,按\"年-月-日\"的格式输入,如1992-7-1:")
   fmt.Scan(&birth)
   birth_Slice := strings.Split(birth, "-")
   birth_Year, _ := strconv.Atoi(birth_Slice[0])
   birth_Month, _ := strconv.Atoi(birth_Slice[1])
   birth_Day, _ := strconv.Atoi(birth_Slice[2])
   fmt.Println(birth_Year)

   switch birth_Month {
   case 1:
      if birth_Day >= 1 && birth_Day <= 19 {
         fmt.Println("您是摩羯座!")
      } else if birth_Day >= 20 && birth_Day <= 31 {
         fmt.Println("您是水瓶座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 2:
      if birth_Day >= 1 && birth_Day <= 18 {
         fmt.Println("您是水瓶座!")
      } else if birth_Day >= 19 && birth_Day <= 29 {
         fmt.Println("您是双鱼座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 3:
      if birth_Day >= 1 && birth_Day <= 20 {
         fmt.Println("您是双鱼座!")
      } else if birth_Day >= 21 && birth_Day <= 31 {
         fmt.Println("您是白羊座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 4:
      if birth_Day >= 1 && birth_Day <= 19 {
         fmt.Println("您是白羊座!")
      } else if birth_Day >= 20 && birth_Day <= 30 {
         fmt.Println("您是金牛座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 5:
      if birth_Day >= 1 && birth_Day <= 21 {
         fmt.Println("您是金牛座!")
      } else if birth_Day >= 22 && birth_Day <= 31 {
         fmt.Println("您是双子座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 6:
      if birth_Day >= 1 && birth_Day <= 22 {
         fmt.Println("您是双子座!")
      } else if birth_Day >= 23 && birth_Day <= 30 {
         fmt.Println("您是巨蟹座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 7:
      if birth_Day >= 1 && birth_Day <= 22 {
         fmt.Println("您是巨蟹座!")
      } else if birth_Day >= 23 && birth_Day <= 31 {
         fmt.Println("您是狮子座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 8:
      if birth_Day >= 1 && birth_Day <= 22 {
         fmt.Println("您是狮子座!")
      } else if birth_Day >= 23 && birth_Day <= 31 {
         fmt.Println("您是处女座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 9:
      if birth_Day >= 1 && birth_Day <= 22 {
         fmt.Println("您是处女座!")
      } else if birth_Day >= 23 && birth_Day <= 30 {
         fmt.Println("您是天秤座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 10:
      if birth_Day >= 1 && birth_Day <= 23 {
         fmt.Println("您是天秤座!")
      } else if birth_Day >= 24 && birth_Day <= 31 {
         fmt.Println("您是天蝎座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 11:
      if birth_Day >= 1 && birth_Day <= 22 {
         fmt.Println("您是天蝎座!")
      } else if birth_Day >= 23 && birth_Day <= 30 {
         fmt.Println("您是射手座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   case 12:
      if birth_Day >= 1 && birth_Day <= 21 {
         fmt.Println("您是射手座!")
      } else if birth_Day >= 22 && birth_Day <= 31 {
         fmt.Println("您是摩羯座!")
      } else {
         fmt.Printf("%d月日期输入不合法,请重新输入。\n", birth_Month)
      }
   default:
      fmt.Println("月份输入不合法,月份范围1~12,请重新输入。")
   }
}
posted @ 2023-01-02 12:06  Arthur666  阅读(25)  评论(0编辑  收藏  举报