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

使用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]
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题