for循环的新功能 [英] new at using for loops

查看:70
本文介绍了for循环的新功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到的所有评论都推荐了

All of the comments I received recommended this

package main

import (
    "fmt"
)

func getWord(word string) string {
    value:=[]rune(word)
    for i := 0; i < len(word); i++ {
        j := i + 1
        fmt.Println("positions", i, j)
    }
}

但是当我想减去2个位置的值

but when I want to subtract the values in the 2 positions

if value[i] - value[j] == 0 || value[i] - value[j] == 1 {
    return value
} else {
    return " "
}

这些是输出内容

0 1
panic: runtime error: index out of range

goroutine 1 [running]:

https://play.golang.org/p/VAW6AhB1lev

推荐答案

您的第二个for循环贯穿所有迭代,阻塞了第一个直到完成.

Your second for loop runs through all iterations blocking the first until it completes.

这就是为什么我打印1直到j达到10的原因

This is why i prints 1 until j reaches 10

for (int i = 0; i < 10; i++){
   printf("positions %d %d\n", i, i+1);
}

这篇关于for循环的新功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆