Q1mi / BlogComments

9 stars 0 forks source link

Go语言基础之流程控制 | 李文周的博客 #22

Open Q1mi opened 5 years ago

Q1mi commented 5 years ago

https://www.liwenzhou.com/posts/Go/04_basic/

李文周的Blog

tianbaoo commented 5 years ago
package main

import (
    "fmt"
)

func main() {
    for i := 1; i < 10; i++ {
        for j := 1; j <= i; j++ {
            fmt.Printf("%v*%v=%v\t", j, i, i*j)
        }
        fmt.Println()
    }
}
gooney-zero commented 5 years ago

思考题 我认为是作用域不同 一个只能在if else里使用 另一个可以在函数内部任何地方使用

cuteculu commented 5 years ago

package main

import "fmt"

//编写代码打印9*9乘法表。
func main() {
    for i := 1; i <= 9; i++ {
        for j := 1; j <= i; j++ {
            fmt.Printf("%d * %d = %-2d  ", i, j, i*j)
        }
        fmt.Println()
    }
}
cg430 commented 5 years ago

package main

import "fmt"

// 打印99 乘法表 func main() { for i := 1; i < 10; i++ { for j := 1; j < 10; j++ { if i > j { continue } fmt.Printf("%v%v=%v\t", i, j, i*j) } fmt.Println() } }

ghost commented 5 years ago
package main

import "fmt"

func main() {
    for i := 1; i <= 9; i++ {
        for j := 1; j <= i; j++ {
            fmt.Printf(" %d * %d = %-2d  | ", j, i, i*j)
        }
        fmt.Println()
    }
}
yeahQing commented 4 years ago
func mpt() {
loop:
    for i := 1; i < 10; i++ {
        // for j := 1; j <= i; j++ {
        //  fmt.Printf("%v*%v=%v\t", j, i, i*j)
        // }
        for j := 1; j < 10; j++ {
            if i == j {
                fmt.Printf("%d*%d=%d\n", j, i, i*j)
                continue loop
            }
            fmt.Printf("%d*%d=%d\t", j, i, i*j)
        }
    }
}
Daivad commented 4 years ago
package main

import "fmt"

func main() {
        //顺序打印99乘法表 
    for i := 1; i < 10; i++ {
        for j := 1; j <= i; j++ {
            fmt.Printf("%d * %d = %d\t",  j,  i,  i*j)
        }
        fmt.Println()
    }
    fmt.Println()
        //倒序打印99乘法表
    for i := 9; i >= 1; i-- {
        for j := 1; j <= i; j++ {
            fmt.Printf("%d * %d = %d\t",  j,  i,  i*j)
        }
        fmt.Println()
    }
}
ccrice commented 4 years ago
package main

import "fmt"

func main() {
    for i := 1; i < 10; i++ {
        for j := i; j < 10; j++ {
            fmt.Printf("%d * %d = %d ", i, j, i*j)
        }
    }
}
YunMengs commented 4 years ago

@5guang 思考题 我认为是作用域不同 一个只能在if else里使用 另一个可以在函数内部任何地方使用

我也是觉得,写在ifelse代码块里面,执行完后清理内存,性能上优秀一点,然后可读性会高一些

YunMengs commented 4 years ago
first := 1
last := 1
for{
    fmt.Printf(" %d X %d = %d ", first, last, first*last)
    if last < first {
        last++
    }else {
        if first > 8 {
            break
        }
        fmt.Println()
        last = 1
        first++;
    }

}
jagaimotomato commented 4 years ago

/乘法表/ func times() { for i := 0; i < 10; i++ { fmt.Println() for j := 0; j < 10; j++ { if j >= i { fmt.Printf("%v x %v = %v\t", i, j, i*j) } } } }

auula commented 4 years ago

fmt.Println("倒序 九九乘法表")
    for i := 9; i > 0; i-- {
        for j := 1; j <= i; j++ {
            fmt.Printf("%d * %d = %d \t", j, i, i*j)
        }
        fmt.Println()
    }
mytea003 commented 4 years ago

1、目测只能找到变量的可见性的不同,其它没发现什么。 2、代码的关键是两个循环,而要美观的关键在于判断两个变量的大小跳出循环,使得不再打印重复的乘法公式。

//九九乘法表。
    for i := 1; i <= 9; i++ {
        fmt.Println()
        for j := 1; j <= 9; j++ {
            if j > i {
                continue
            }
            fmt.Printf("%dx%d=%d\t", j, i, j*i)  //公式的排序关键看这里
        }
    }
wcc4869 commented 4 years ago
package main

import "fmt"

func main() {

    for i := 1; i < 10; i++ {
        for j := 1; j <= i; j++ {
            fmt.Printf("%d*%d = %v \t",j,i,j*i)
        }
        fmt.Println()
    }
}
Artsmp commented 4 years ago

我的是这样的

func main() {
    for i := 1; i < 10; i++ {
        for j := 1; j < 10; j++ {
            // j每次从1开始,如果当前j的值大于i,则结束本轮,并执行换行操作,开始下一轮循环
            if j > i {
                break
            }
            fmt.Printf("%d x %d = %d\t", j, i, i*j)
        }
        fmt.Println()
    }
}
Cc360428 commented 4 years ago
package main

import "fmt"

func main() {
    for i := 1; i < 10; i++ {
        for j := 1; j <= i; j++ {
            fmt.Print(j, "*", i, "=", i*j, "   ")
        }
        fmt.Println()
    }
    for i := 9; i >= 1; i-- {
        for j := 1; j <= i; j++ {
            fmt.Print(j, "*", i, "=", i*j, "   ")
        }
        fmt.Println()
    }
}
SmeagolYe commented 4 years ago

done

tangjoin commented 4 years ago
package main

import (
    "fmt"
)
//编写代码打印9*9乘法表。
func main() {
    str := ""
    for i := 1; i < 10; i++ {
        for j := 1; j < 10; j++ {
            if i < j {
                break
            }
            str = str + fmt.Sprintf("%d x %d = %d " , j , i , i * j)
        }
        str = str +"\n"
    }
    println(str)
}
wallacefw1987 commented 4 years ago

//9 * 9 乘法表 func test2() { for a :=1; a<10;a++ { for b :=1; b<10;b++ { fmt.Println(a," * ",b," = ",(a*b)) } } }

CN-HF commented 4 years ago

for x := 1; x < 10; x++ { for y := 1; y <= x; y++ { if x != y { fmt.Printf("%d %d = %d\t", y, x, xy) } else { fmt.Printf("%d %d = %d\n", y, x, xy) } } }

qdc2015 commented 4 years ago

package main

import ( "fmt" )

func chengfabiao() { loop1: for i := 1; i < 10; i++ { for j := 1; j < 10; j++ { fmt.Printf(fmt.Sprintf("%v%v=%v", i, j, ij)) fmt.Printf(" ") if i == j { fmt.Println() continue loop1 } } } }

func main() { chengfabiao() }

Romeo3119 commented 4 years ago

func switchDemo5() { s := "a" switch { case s == "a": fmt.Println("a") fallthrough case s == "b": fmt.Println("b") case s == "c": fmt.Println("c") default: fmt.Println("...") } }

有谁可以帮我解释一下,为什么还会输出b呢?s不是已经被赋值为"a"了吗? "a" !="b",为什么还会输出b呢

Romeo3119 commented 4 years ago
    //练习:编写乘法口诀表
    for i := 1; i < 10; i++ {
        for j := 1; j <= i; j++ {
            fmt.Printf("%d x %d = %d   ", i, j, i*j)

        }
        fmt.Printf("\n")
    }
yangfan16 commented 4 years ago

// 编写代码打印9*9乘法表

func MultiTablePrint() {
    for i := 1; i < 10; i++ {
        for j := 1; j < 10; j++ {
            if j > i {
                continue
            }
            fmt.Printf("%d * %d = %d\t", i, j, i*j)
        }
        fmt.Println()
    }
}
LnkSaber commented 4 years ago

func homeWorkTwo(){ for i :=1; i<=9; i++{ for j := 1; j<=i; j++{ fmt.Printf("%d %d = %d ", i, j, ij) } fmt.Println() } }

heyhawaii commented 4 years ago

for x := 1; x < 10; x++ { for y := 1; y < 10; y++ { if y <= x { fmt.Printf("%d x %d = %d ", x, y, x*y) } if x == y { fmt.Println() } } }

jacobwwang commented 4 years ago

breakDemo1:外层循环也退出了。 continueDemo:相当于 break,中断本次内循环。

请教一下沙河老师,switch 代码块,什么场景下需要配合 break 来使用? switch 中命中 case 语句后,不也退出了该 switch 块了么?

Q1mi commented 4 years ago

@JimmyLincole breakDemo1:外层循环也退出了。 continueDemo:相当于 break,中断本次内循环。

请教一下沙河老师,switch 代码块,什么场景下需要配合 break 来使用? switch 中命中 case 语句后,不也退出了该 switch 块了么?

沙河是个地名,不是我的名字哈。。。 for循环中嵌套switch语句的场景可能需要break+label的组合。

wunairen commented 4 years ago

package main

import ( "fmt" )

func main(){ for i := 1; i <= 9; i++ { for s := 1; s <= i; s++ { fmt.Printf("%v %v = %v\n", i, s, is) } } }

wangbin1696 commented 4 years ago

func NewThree() { for i := 1; i < 10 ;i++{ for j := 1;j <= i ; j++{ fmt.Printf("%d × %d = %d ",j,i,(i*j)) } fmt.Println("") }

}

glassy-sky-lisong commented 4 years ago

package main

import ( "fmt" )

func main() {

for i := 1; i < 10; i++ {
    for j := 1; j < i + 1; j++ {
        fmt.Printf("%d * %d  ", i,j)
    }
    fmt.Println()
}

}

wangdaxian201 commented 4 years ago

package main

import ( "fmt" )

func testCode() {

fmt.Println("程序开始。。。")
for i := 1; i < 10; i++ {
    for j := 1; j < i; j++ {
        fmt.Printf("%d * %d = %d   ", j, i, i*j)
    }
    fmt.Println("")
}
fmt.Println("程序执行结束。。。。。。")
return

}

excaliburdraco commented 4 years ago

func main() {

for i :=1;i <= 9; i++{
    for j := 1;j <= 9; j++{
        fmt.Printf("%d * %d = %d  ",j,i,j*i)
        if i == j{
            fmt.Println("")
            break
        }
    }
}

}

jyhgit commented 4 years ago

package main

import "fmt"

func main() { // 乘法

for i := 1; i < 10; i++ {
    for j := 1; j < 10; j++ {
        if i == j {
            fmt.Printf("%dx%d=%d\n", j, i, j*i)
            break
        }
        fmt.Printf("%dx%d=%d\t", j, i, j*i)
    }
}

for i := 9; i >= 1; i-- {
    for j := 1; j < 10; j++ {
        if i == j {
            fmt.Printf("%dx%d=%d\n", j, i, j*i)
            break
        }
        fmt.Printf("%dx%d=%d\t", j, i, j*i)
    }

}

}

xjb714 commented 3 years ago

for i:=1;i<=9;i++{
        for j:=1;j<=i+1;j++{
            if(j==i+1){fmt.Println()} else {fmt.Printf("%d*%d=%d ",i,j,i*j)}
        }
    }
Master348264747 commented 3 years ago

func main() {

for q := 1; q <= 9; q++ {
    for w := 1; w <= 9; w++ {
        if w == 9 {
            fmt.Printf("%v*%v=%v\n", q, w, q*w)
        } else if q < w {
            fmt.Printf("%v*%v=%v ", q, w, q*w)
        }

        continue
    }
}

}

brianchiang-tw commented 3 years ago

hw1:
编写代码打印9*9乘法表。

package main

import (
    "fmt"
)

func multiplicationTable() {

    // compute and print 9 x 9 multiplication table to standard output

    for i := 1; i < 10; i++ {
        for j := 1; j < 10; j++ {

            fmt.Printf("   %2v * %2v = %2v", i, j, i*j)

        }
        fmt.Printf("\n")
    }

    return
}

func main() {

    multiplicationTable()

    return

}

output:
1 1 = 1 1 2 = 2 1 3 = 3 1 4 = 4 1 5 = 5 1 6 = 6 1 7 = 7 1 8 = 8 1 9 = 9
2
1 = 2 2 2 = 4 2 3 = 6 2 4 = 8 2 5 = 10 2 6 = 12 2 7 = 14 2 8 = 16 2 9 = 18
3 1 = 3 3 2 = 6 3 3 = 9 3 4 = 12 3 5 = 15 3 6 = 18 3 7 = 21 3 8 = 24 3 9 = 27
4
1 = 4 4 2 = 8 4 3 = 12 4 4 = 16 4 5 = 20 4 6 = 24 4 7 = 28 4 8 = 32 4 9 = 36
5 1 = 5 5 2 = 10 5 3 = 15 5 4 = 20 5 5 = 25 5 6 = 30 5 7 = 35 5 8 = 40 5 9 = 45
6
1 = 6 6 2 = 12 6 3 = 18 6 4 = 24 6 5 = 30 6 6 = 36 6 7 = 42 6 8 = 48 6 9 = 54
7 1 = 7 7 2 = 14 7 3 = 21 7 4 = 28 7 5 = 35 7 6 = 42 7 7 = 49 7 8 = 56 7 9 = 63
8
1 = 8 8 2 = 16 8 3 = 24 8 4 = 32 8 5 = 40 8 6 = 48 8 7 = 56 8 8 = 64 8 9 = 72
9 1 = 9 9 2 = 18 9 3 = 27 9 4 = 36 9 5 = 45 9 6 = 54 9 7 = 63 9 8 = 72 9 * 9 = 81

helloHupc commented 3 years ago
// 1x1
// 1x2 2x2
// 1x3 2x3 3x3
//

func multi_table_99() {
    for i := 1; i < 10; i++ {
        for j := 1; j <= i; j++ {
            fmt.Printf("%dx%d=%d ", j, i, i*j)
        }
        fmt.Println()
    }
}
harleyxu-xhl commented 3 years ago

gotoDemo2中的return 没什么用吧??

xiaoliu-qaq commented 3 years ago

package main

import "fmt"

func main() { for i:=1;i<10;i++{ for j:=1;j<=i;j++ { fmt.Printf("%d%d=%d\t",i,j,ji) } fmt.Println() }

}

yahaodada commented 3 years ago

for i:=1;i<=9;i++{ for o:=1;o<=i;o++ { sum :=oi fmt.Printf("%d%d=%d",o,i,sum) if sum <= 9 { fmt.Printf(" ") }else{ fmt.Printf(" ") } } fmt.Println() }

xing-blip commented 3 years ago

package main

import "fmt"

func main() { for i := 1; i < 10; i++ { for j := 1; j < 10; j++ { if i > j { fmt.Printf("%vx%v=%v ", i, j, ij) } else if i < j { break } else { fmt.Printf("%vx%v=%v\n", i, j, ij) } } } }

windf17 commented 3 years ago
package main

import "fmt"

func main() {

    for i := 1; i < 10; i++ {
        for j := i; j < 10; j++ {
            fmt.Printf("%d * %d = %d\t", i, j, i*j)
        }
        fmt.Println()
    }
}
tangguangpu commented 3 years ago

func printMultiplicationTable() { for i := 1; i < 10; i++ { for j := 1; j < 10; j++ { if j > i { continue } fmt.Printf("%d %d = %d ", j, i, ij) } fmt.Println() } }

QuYingying0628 commented 3 years ago
//打印九九乘法表
func printMultiplicationTable() {
    for i := 1; i <= 9; i++ { //乘法的后一位
        if i != 1 { //是否换行
            fmt.Print("\n")
        }

        for j := 1; j <= i; j++ { //乘法的前一位
            if j != 1 { //是否加空格
                fmt.Print(" ")
            }

            fmt.Print(j, "*", i, "=", i*j)
        }
    }
}
Elgar17 commented 3 years ago
func printTable() {
    for i := 1; i < 10; i++ {
        for j := 1; j < i+1; j++ {
            fmt.Print(j, "*", i, "=", i*j, " ")
        }
        fmt.Println(" ")
    }
}
Mrszhao112 commented 3 years ago

func f_three() { for i := 0; i < 10; i++ { for j := i; j < 10; j++ { fmt.Print(i, " ", j, " = ", ij, " ") } fmt.Println() } }

sdlyingyong commented 2 years ago
func multiTable(){
    for i:=1;i<=9;i++ {
        forloop1:
        for j:= 1;j <= 9;j++{
            if j > i {
                continue forloop1
            }
            ret := fmt.Sprintf("%v x %v = %v ",i,j,i*j)
            fmt.Print(ret)
        }
        fmt.Println()
    }
}
Master348264747 commented 2 years ago

邮件已经查看,我会尽快回复您.

venuszero commented 2 years ago

package main

import ( "fmt" )

func main() { for i := 1; i <= 9; i++ { for z := 1; z <= i; z++ { fmt.Printf("%d%d=%d ", z, i, zi) } fmt.Println() } }