1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > GO-输入输出函数和流程控制语句

GO-输入输出函数和流程控制语句

时间:2019-10-13 09:18:03

相关推荐

GO-输入输出函数和流程控制语句

目录

1. 流程控制语句

1.1 分支语句

1.1.1 双分支语句

1.1.2 单分支

1.1.3 多分支

1.1.4switch-多分支语句

1.1.5 分支嵌套

1.2 流程循环语句

1.2.1 for循环语句

1.2.2 三要素for循环

1.2.3for循环嵌套if判断

1.2.4 退出循环

1.2.5 循环嵌套

2. 输入输出函数

2.1 输出函数

2.2 输入函数

1. 流程控制语句

程序是由语句构成,而流程控制语句 是用来控制程序中每条语句执行顺序的语句。可以通过控制语句实现更丰富的逻辑以及更强大的功能。几乎所有编程语言都有流程控制语句,功能也都基本相似。其流程控制方式有:

顺序结构

分支结构

循环结构

1.1 分支语句

顺序结构的程序虽然能解决计算、输出等问题,但不能做判断再选择。对于要先做判断再选择的问题就要使用分支结构。

1.1.1 双分支语句

双分支语句格式:if 布尔表达式 {结果为ture指定的代码块}else {结果为false执行的代码块} package mainimport ("fmt")func main() {fmt.Println("请输入你的年龄")var age intfmt.Scan(&age)if age >= 18 {fmt.Println("欢迎观看")} else {fmt.Println("未成年禁止look")}}

1.1.2 单分支

单分支判断语句格式:if 布尔表达式 {条件为true执行的代码}package mainimport ("fmt")func main() {name := "li"if name == "liu" {fmt.Println("yes")}}

1.1.3 多分支

多分支语句格式:if 布尔表达式 {结果为ture指定的代码块}else if 布尔表达式 {结果为true执行的代码块}else if 布尔表达式 {结果为true执行的代码块}else {以上所有布尔表达式条件都不满足执行此代码块}package mainimport ("fmt""time")func main() {fmt.Print("输入你的成绩:")var score intfmt.Scan(&score)if score < 0 || score > 100 {fmt.Println("请输入正确的成绩")} else if score == 100 {fmt.Println("NB")} else if score >= 90 {fmt.Println("优秀")} else if score >= 70 {fmt.Println("良好")} else if score >= 60 {fmt.Println("差点挨揍")} else {fmt.Println("你是猪吗,那么笨 回家等着挨揍吧")}time.Sleep(time.Second * 2) //时间函数 睡眠模块}

1.1.4switch-多分支语句

// 语法:switch后面的变量匹配下面的case,是通过hash计算的,并不会挨个匹配// switch...case会生成一个跳转表来指示实际的case分支的地址,而这个跳转表的索引号与switch变量的值是相等的。从而,switch...case不用像if...else那样遍历条件分支直到命中条件,而只需访问对应索引号的表项从而到达定位分支的目的switch var {case val1:...case val2:...default:...}/*给出如下选项,并根据选项进行效果展示:输入1:则输出"普通攻击"输入2:则输出"超级攻击"输入3:则输出"使用道具"输入3:则输出"逃跑"*/var choice intfmt.Println("请输入选择:")fmt.Scanln(&choice)//fmt.Println(choice,reflect.TypeOf(choice))switch choice {case 1:fmt.Println("普通攻击")case 2:fmt.Println("超级攻击")case 3:fmt.Println("使用道具")case 4:fmt.Println("逃跑")default:fmt.Println("输入有误!")}

1.1.5 分支嵌套

//if switch嵌套格式if n == 1 {//根据成绩判断 成绩等级if cj >= 90 && <=100 {fmt.Println("nb")}}else if n == 2 {switch yue {case 1:if ri <= 21 {fmt.Println()}else {} }}else {if username == "" && password == "" {//结果为真 执行}else {//结果为假 执行}} package mainimport ("fmt""strconv""strings")func main() {fmt.Print("1.成绩等级\n", "2.星座判断\n", "3.用户密码\n", "执行选择:")var n intfmt.Scan(&n)if n >= 1 && n <= 3 {switch n {case 1:fmt.Print("请输出成绩:")var cj intfmt.Scan(&cj)if cj >= 0 && cj <= 100 {if cj == 100 {fmt.Println("NB")} else if cj >= 90 {fmt.Println("优秀")} else if cj >= 70 {fmt.Println("良好")} else if cj >= 60 {fmt.Println("差点挨揍")} else {fmt.Println("你是猪吗 这么笨")}}case 2:fmt.Print("请按照指定格式输入出生日期xxxx-xx-xx:")var a stringfmt.Scan(&a)a1 := strings.Split(a, "-")yue, _ := strconv.Atoi(a1[1])ri, _ := strconv.Atoi(a1[2])if yue == 3 && ri >= 21 && ri <= 31 {fmt.Println("白羊座")} else if yue == 4 && ri <= 20 {fmt.Println("白羊座")} else if yue == 4 && ri >= 21 {fmt.Println("金牛座")} else if yue == 5 && ri <= 20 {fmt.Println("金牛座")} else if yue == 5 && ri >= 21 {fmt.Println("双子座")} else if yue == 6 && ri <= 21 {fmt.Println("双子座")} else if yue == 6 && ri >= 22 {fmt.Println("巨蟹座")} else if yue == 7 && ri <= 22 {fmt.Println("巨蟹座")} else if yue == 7 && ri >= 23 {fmt.Println("狮子座")} else if yue == 8 && ri <= 22 {fmt.Println("狮子座")} else if yue == 8 && ri >= 23 {fmt.Println("处女座")} else if yue == 9 && ri <= 22 {fmt.Println("处女座")} else if yue == 9 && ri >= 23 {fmt.Println("天秤座")} else if yue == 10 && ri <= 22 {fmt.Println("天秤座")} else if yue == 10 && ri >= 23 {fmt.Println("天蝎座")} else if yue == 11 && ri <= 21 {fmt.Println("天蝎座")} else if yue == 11 && ri >= 22 {fmt.Println("射手座")} else if yue == 12 && ri <= 21 {fmt.Println("射手座")} else if yue == 12 && ri >= 22 {fmt.Println("魔羯座")} else if yue == 1 && ri <= 19 {fmt.Println("魔羯座")} else if yue == 1 && ri >= 20 {fmt.Println("水瓶座")} else if yue == 2 && ri <= 18 {fmt.Println("水瓶座")} else if yue == 2 && ri >= 19 {fmt.Println("双鱼座")} else if yue == 3 && ri <= 20 {fmt.Println("双鱼座")} else {fmt.Println("去 滚")}case 3:fmt.Print("输入用户名 密码")var username, password stringfmt.Scan(&username, &password)if username == "liuzhusong" && password == "123" {fmt.Println("输入正确")} else {fmt.Println("用户名或密码错误")}}}}

1.2 流程循环语句

1.2.1 for循环语句

与多数语言不同的是,Go语言中的循环语句只支持 for 关键字,而不支持 while 和 do-while 结构

for 后面的条件表达式不需要用圆括号()括起来

左花括号{必须与 for 处于同一行。

无限循环的场景for true {},可以简写for {}

count := 0 // 初始化语句for count < 10 { // 条件判断fmt.Println("hello yuan!")count++ // 步进语句}fmt.Println("end")

count := 10// 初始化语句for count > 0 { // 条件判断fmt.Println(count)count-- // 步进语句}fmt.Println("end")

1.2.2 三要素for循环

init: 初始化语句,一般为赋值表达式,给控制变量赋初值;

condition:条件判断,一般是关系表达式或逻辑表达式,循环控制条件;

post: 步进语句,一般为赋值表达式,给控制变量增量或减量。

for i := 1; i < 10; i++ {fmt.Println(i)}执行:> (1)初始语句>> (2)条件判断,布尔值为真则执行一次循环体,为假则退出循环>> (3)执行一次循环体语句结束后,再执行步进语句,然后回到步骤(2),依次循环

1.2.3for循环嵌套if判断

package mainimport ("fmt")func main() {fmt.Println(`1.平A2.技能13.技能24.大招`)for true {fmt.Print("输入使用攻击方式:")var sc intfmt.Scan(&sc)if sc >= 1 && sc <= 4 {switch sc {case 1:fmt.Println("平A")case 2:fmt.Println("技能1")case 3:fmt.Println("技能2")case 4:fmt.Println("大招")}} else {fmt.Println("看好攻击序号输入")}}}

1.2.4 退出循环

如果想提前结束循环(在不满足结束条件的情况下结束循环),可以使用break或continue关键字。

break 用来跳出整个循环语句,也就是跳出所有的循环次数;

continue 用来跳出当次循环,也就是跳过当前的一次循环。

break

// 当 break 关键字用于 for 循环时,会终止循环而执行整个循环语句后面的代码。break 关键字通常和 if 语句一起使用,即满足某个条件时便跳出循环,继续执行循环语句下面的代码。for i := 0; i < 10; i++ {if i==8{break}fmt.Println(":",i)}

continue

// break 语句使得循环语句还没有完全执行完就提前结束,与之相反,continue 语句并不终止当前的循环语句的执行,仅仅是终止当前循环变量 i 所控制的这一次循环,而继续执行该循环语句。continue 语句的实际含义是“忽略 continue 之后的所有循环体语句,回到循环的顶部并开始下一次循环”for i := 0; i < 10; i++ {if i==8{continue}fmt.Println(":",i)}

1.2.5 循环嵌套

// 独立嵌套for i := 0; i < 5; i++ {for j:=0;j<5;j++ {fmt.Print("*")}fmt.Print("\n")}// 关联嵌套package mainimport "fmt"func main() {// 遍历, 决定处理第几行for y := 1; y <= 9; y++ {// 遍历, 决定这一行有多少列for x := 1; x <= y; x++ {fmt.Printf("%d*%d=%d ", x, y, x*y)}// 手动生成回车fmt.Println()}}

2. 输入输出函数

2.1 输出函数

// 使用fmt包下的println或print等package mainimport ("fmt""reflect""strings")func main() {var (name = "liuzhusong"age = 22xb = true)fmt.Print(name, age) //print不换行打印输出fmt.Println(name, age) //打印输出并换行fmt.Println("姓名:", name, "年龄:", age) //打印多个值得时候用逗号分割fmt.Printf("姓名: %s 年龄: %d 性别: %t \n", name, age, xb) //printf格式化输出不自动换行 %s占位符代表字符串类型 %d占位符代表整数 %t占位符代表布尔类型,在将对应得变量名按照格式得顺序进行排列输出。fmt.Sprintf("姓名: %s 年龄: %d 性别: %t \n", name, age, xb) //不向终端打印格式化输出。s := fmt.Sprintf("姓名: %s 年龄: %d 性别: %t", name, age, xb)//格式化输出 赋值在变量中fmt.Println(s)}

2.2 输入函数

package mainimport ("fmt")func main() {//输入函数 Scan 的特点为 括号内有多少个输入的赋值的变量,就必须要输入多少个值,否则一直停留在IO堵塞状态fmt.Print("输入你的名字")var name string //创建一个字符串变量,赋值为空fmt.Scan(&name) //Fmt.Scan函数为输入函数,向程序内部发送指令,&为占位符,&name才可以修改外部name得值fmt.Println(name, reflect.TypeOf(name))//用户登录fmt.Print("请输入姓名 年龄")var name5 stringvar age2 intfmt.Scan(&name5, &age2)fmt.Println(name5, age2)fmt.Println("输入出生日期xxxx-xx-xx")var year stringfmt.Scan(&year)year2 := strings.Split(year, "-")fmt.Printf("%s年%s月%s日 \n", year2[0], year2[1], year2[2])//Scanln 输入特点 不管在Scanln中有多少个被赋值的变量,只要回车就停止赋值,直接运行程序。fmt.Print("请输入姓名 年龄")var name6 stringvar age3 intfmt.Scanln(&name6, &age3)fmt.Println(name5, age2)//Scanf 输入特点 按照指定格式输入var x1 intvar x2 intfmt.Print("请按照 x + x 的格式输入")fmt.Scanf("%d + %d", &x1, &x2)fmt.Println(x1 + x2)}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。