使用Golang将int数组转换为char数组?

I have this error :

# command-line-arguments
.\cheking.go:14: cannot use strconv.Itoa(i + 64) + strconv.Itoa(j + 48) (type st
ring) as type [8]int in assignment

code:

package main

import (
    "fmt"
    "strconv"
)

func main() {

    var board [8][8]int

    for i := 1; i <= 8; i++ { // initialize  array
        for j := 1; j <= 8; j++ {
            board[(j-1)+8*(i-1)] = (strconv.Itoa(i+64) + "" + strconv.Itoa(j+48)) // int to char

            fmt.Printf("%s 
", board[i][j])
        }
    }
}

2个回答

strconv.Itoa is shorthand for FormatInt(int64(i), 10):

FormatInt returns the string representation of i in the given base, for 2 <= base <= 36. The result uses the lower-case letters 'a' to 'z' for digit values >= 10.

so the result of strconv.Itoa(i+64) is string, and the board is not (this is the error).

I think you are trying to do something like this working sample code (let me know if not):

package main

import "fmt"

func main() {
    board := [8][8]string{}
    for i := 0; i < 8; i++ { // initialize  array
        for j := 0; j < 8; j++ {
            board[i][j] = string(i+65) + string(j+49) // int to char
            fmt.Printf("%s ", board[i][j])
        }
        fmt.Println()
    }
}

output:

A1 A2 A3 A4 A5 A6 A7 A8 
B1 B2 B3 B4 B5 B6 B7 B8 
C1 C2 C3 C4 C5 C6 C7 C8 
D1 D2 D3 D4 D5 D6 D7 D8 
E1 E2 E3 E4 E5 E6 E7 E8 
F1 F2 F3 F4 F5 F6 F7 F8 
G1 G2 G3 G4 G5 G6 G7 G8 
H1 H2 H3 H4 H5 H6 H7 H8

if my guess is fine, you may do it this way too:

package main

import "fmt"

func main() {
    board := [8][8]string{
        {"A1", "A2", "A3", "A4", "A5", "A6", "A7", "A8"},
        {"B1", "B2", "B3", "B4", "B5", "B6", "B7", "B8"},
        {"C1", "C2", "C3", "C4", "C5", "C6", "C7", "C8"},
        {"D1", "D2", "D3", "D4", "D5", "D6", "D7", "D8"},
        {"E1", "E2", "E3", "E4", "E5", "E6", "E7", "E8"},
        {"F1", "F2", "F3", "F4", "F5", "F6", "F7", "F8"},
        {"G1", "G2", "G3", "G4", "G5", "G6", "G7", "G8"},
        {"H1", "H2", "H3", "H4", "H5", "H6", "H7", "H8"},
    }

    // print the board:
    for i := 0; i < 8; i++ {
        fmt.Println(board[i])
    }
}

output:

[A1 A2 A3 A4 A5 A6 A7 A8]
[B1 B2 B3 B4 B5 B6 B7 B8]
[C1 C2 C3 C4 C5 C6 C7 C8]
[D1 D2 D3 D4 D5 D6 D7 D8]
[E1 E2 E3 E4 E5 E6 E7 E8]
[F1 F2 F3 F4 F5 F6 F7 F8]
[G1 G2 G3 G4 G5 G6 G7 G8]
[H1 H2 H3 H4 H5 H6 H7 H8]
douqiu0796
douqiu0796 非常感谢 ! 那就是我所需要的。
大约 4 年之前 回复

First of all, if you want to initialize each board position with a string, you need to change the declaration of your board array:

var board [8][8]string

since strconv.Itoa returns a string.

Then, if you simply need to access each board location sequentially, you can simply update your inner loop:

// You don't need to iterate through the array like C using index arithmatic
for i := 0; i < 8; i++ { // initialize  array
        for j := 0; j < 8; j++ {
            // use whatever logic you are using to init each value
            board[i][j] = (strconv.Itoa(i+64) + "" + strconv.Itoa(j+48)) // int to char

            fmt.Printf("%s 
", board[i][j])
        }
    }
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐