1. 首页
  2. 主题
  3. Go问与答

咨询一个循环问题:

hellsam · · 1706 次点击
让最终形成: 2:4 3:5 4:6 5:7 6:8 ~~~ func main() { new_start := 2 new_end := 6 old_start := 4 old_end := 8 for i := new_start; i <= new_end; i++ { for j := old_start; j <= old_end; j++ { fmt.Println(i, ":", j) } } /* 让最终形成: 2:4 3:5 4:6 5:7 6:8 */ } ~~~
![7EC2BF62-9F17-4957-8953-E7FB202471AF.png](https://static.golangjob.cn/231214/8f8af649fc8dc4234a55232a81fc6728.png)
#2
更多评论
package main import &#34;fmt&#34; func main() { new_start := 2 new_end := 6 old_start := 4 old_end := 8 for i := new_start; i &lt;= new_end; i++ { // 计算 j 的偏移量,使其与 i 对应 offset := i - new_start j := old_start + offset if j &gt; old_end { break // 避免 j 超过 old_end } fmt.Println(i, &#34;:&#34;, j) } }
#3

用户登录

没有账号?注册

今日阅读排行

    加载中

一周阅读排行

    加载中