163 lines
3.6 KiB
Go
163 lines
3.6 KiB
Go
|
|
package main
|
|||
|
|
|
|||
|
|
import (
|
|||
|
|
"fmt"
|
|||
|
|
)
|
|||
|
|
|
|||
|
|
func main() {
|
|||
|
|
arr01 := []int{1, 2, 3, 4, 5}
|
|||
|
|
// slices for
|
|||
|
|
for index, value := range arr01 {
|
|||
|
|
// 是值的副本,不能修改(改变不了外部的值, 不能修改这个值来修改array)
|
|||
|
|
fmt.Println(index, arr01[index])
|
|||
|
|
fmt.Println(value)
|
|||
|
|
|
|||
|
|
// 改变slice
|
|||
|
|
arr01[index] = 10
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
m01 := map[string]int{"a": 1, "b": 2, "c": 3}
|
|||
|
|
// map for
|
|||
|
|
for key, value := range m01 {
|
|||
|
|
// 是值的副本,不能修改(改变不了外部的值, 不能修改这个值来修改map)
|
|||
|
|
fmt.Println(key, m01[key])
|
|||
|
|
fmt.Println(value)
|
|||
|
|
|
|||
|
|
// 修改map
|
|||
|
|
m01[key] = 10
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// n * m 乘法表
|
|||
|
|
// n 从1到9(乘数), m 从1到n(被乘数)
|
|||
|
|
for m := 1; m <= 9; m++ {
|
|||
|
|
// m: 1, n: 1
|
|||
|
|
// m: 2, n: 1, 2
|
|||
|
|
// m: 3, n: 1, 2, 3
|
|||
|
|
// m: 4, n: 1, 2, 3, 4
|
|||
|
|
// ...
|
|||
|
|
for n := 1; n <= m; n++ {
|
|||
|
|
fmt.Printf("%d×%d=%-2d ", n, m, m*n)
|
|||
|
|
}
|
|||
|
|
fmt.Println()
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
fmt.Println("2-100之间的素数:")
|
|||
|
|
// 遍历 2 到 100
|
|||
|
|
for num := 2; num <= 100; num++ {
|
|||
|
|
isPrime := true // 假设是素数
|
|||
|
|
|
|||
|
|
// 检查能否被2到√num之间的数整除
|
|||
|
|
// 再通过循环检查 从2到num的数, 是否能整除(求模有没有余数: %); 9 (1 * 5, 2 *4, 3 * 3, 4 * 2, 5 * 1)
|
|||
|
|
for divisor := 2; divisor*divisor <= num; divisor++ {
|
|||
|
|
if num%divisor == 0 {
|
|||
|
|
isPrime = false
|
|||
|
|
break // 找到一个因数就可以退出了
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
if isPrime {
|
|||
|
|
fmt.Printf("%d ", num)
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
fmt.Println()
|
|||
|
|
|
|||
|
|
// 不使用标签(GOTO):只跳出内层循环
|
|||
|
|
fmt.Println("---- 不使用标签 ----")
|
|||
|
|
for i := 1; i <= 3; i++ {
|
|||
|
|
fmt.Printf("外层: %d\n", i)
|
|||
|
|
for j := 1; j <= 3; j++ {
|
|||
|
|
fmt.Printf(" 内层: %d\n", j)
|
|||
|
|
if j == 2 {
|
|||
|
|
break // 只跳出内层循环
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 使用标签:跳出外层循环
|
|||
|
|
fmt.Println("---- 使用标签 ----")
|
|||
|
|
OuterLoop: // 定义标签 (定义程序的锚点,使用 goto或者break 跳转的锚点)
|
|||
|
|
for i := 1; i <= 3; i++ {
|
|||
|
|
fmt.Printf("外层: %d\n", i)
|
|||
|
|
for j := 1; j <= 3; j++ {
|
|||
|
|
fmt.Printf(" 内层: %d\n", j)
|
|||
|
|
if j == 2 {
|
|||
|
|
break OuterLoop // 跳出到标签处,即外层循环
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
fmt.Println("---- 跳过偶数 ----")
|
|||
|
|
for i := 1; i <= 10; i++ {
|
|||
|
|
if i%2 == 0 {
|
|||
|
|
continue // 跳过偶数
|
|||
|
|
}
|
|||
|
|
fmt.Printf("%d ", i)
|
|||
|
|
}
|
|||
|
|
// 输出: 1 3 5 7 9
|
|||
|
|
|
|||
|
|
var count int = 1
|
|||
|
|
|
|||
|
|
LOOP: // 定义标签
|
|||
|
|
if count <= 5 {
|
|||
|
|
fmt.Printf("计数: %d\n", count)
|
|||
|
|
count++
|
|||
|
|
goto LOOP // 跳回标签
|
|||
|
|
}
|
|||
|
|
fmt.Println("结束")
|
|||
|
|
|
|||
|
|
fmt.Println("---- 统计学生成绩 ----")
|
|||
|
|
// 统计学生成绩
|
|||
|
|
scores := []int{85, 92, 78, 95, 88, 73, 90, 85, 79, 91}
|
|||
|
|
|
|||
|
|
var sum, max, min int
|
|||
|
|
max = scores[0]
|
|||
|
|
min = scores[0]
|
|||
|
|
|
|||
|
|
for _, score := range scores {
|
|||
|
|
sum += score
|
|||
|
|
if score > max {
|
|||
|
|
max = score
|
|||
|
|
}
|
|||
|
|
if score < min {
|
|||
|
|
min = score
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
average := float64(sum) / float64(len(scores))
|
|||
|
|
fmt.Printf("平均分: %.2f\n", average)
|
|||
|
|
fmt.Printf("最高分: %d\n", max)
|
|||
|
|
fmt.Printf("最低分: %d\n", min)
|
|||
|
|
|
|||
|
|
fmt.Println("---- 清洗用户输入的电话号码列表 ----")
|
|||
|
|
// 清洗用户输入的电话号码列表
|
|||
|
|
phoneNumbers := []string{"138-1234-5678", "159-xxxx-9999", "186-8888-8888", "invalid", "177-9999-0000"}
|
|||
|
|
|
|||
|
|
validPhones := []string{}
|
|||
|
|
for _, phone := range phoneNumbers {
|
|||
|
|
// 简单验证:长度为13,包含两个短横线
|
|||
|
|
if len(phone) != 13 {
|
|||
|
|
continue // 跳过无效号码
|
|||
|
|
}
|
|||
|
|
if phone[3] != '-' || phone[8] != '-' {
|
|||
|
|
continue
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
// 判断所有字符都是数字,不是字母
|
|||
|
|
if !IsNumber(phone[:3]) || !IsNumber(phone[4:8]) || !IsNumber(phone[9:13]) {
|
|||
|
|
continue
|
|||
|
|
}
|
|||
|
|
validPhones = append(validPhones, phone)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
fmt.Printf("有效电话号码: %v\n", validPhones)
|
|||
|
|
}
|
|||
|
|
|
|||
|
|
func IsNumber(s string) bool {
|
|||
|
|
for _, char := range s {
|
|||
|
|
if char < '0' || char > '9' {
|
|||
|
|
return false
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
return true
|
|||
|
|
}
|