package main import "fmt" func main() { /* 统计所有小于非负整数 n 的质数的数量。 示例: 输入: 10 输出: 4 解释: 小于 10 的质数一共有 4 个, 它们是 2, 3, 5, 7 。 除了1和该数自身外,无法被其他自然数整除的数。 */ fmt.Println(">>>") var x int fmt.Scanln(&x) for i := 2; i <= x; i++ { //标志位 flag := 0 //2是质数 if i == 2 { fmt.Printf("%v是质数 ", i) } for j := 2; j < i; j++ { if i%j == 0 { //只要有一次 i%j == 0 ,flag就会加1 //只要这里的flag被加1,就说明i不是质数 flag += 1 break } } //如果flag没有被加1,i就是质数 if flag == 0 { fmt.Printf("%v是质数 ", i) } } }