douju2012
2016-08-01 04:17
浏览 391
已采纳

使用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条回答 默认 最新

  • dongyu1614 2016-08-01 04:28
    已采纳

    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]
    
    已采纳该答案
    评论
    解决 无用
    打赏 举报
  • dqf35839 2016-08-01 04:31

    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])
            }
        }
    
    评论
    解决 无用
    打赏 举报

相关推荐 更多相似问题