gpt4 book ai didi

multidimensional-array - F# 打印出一个二维字符串数组

转载 作者:行者123 更新时间:2023-12-04 08:40:55 24 4
gpt4 key购买 nike

我目前正在开发一款名为 Neutron 的棋盘游戏。

let boardArray = array2D [ ["*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"];
["*"; "1"; "*"; "2"; "*"; "3"; "*"; "4"; "*"; "5"; "*"];
["*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"];
["*"; "0"; "*"; "0"; "*"; "0"; "*"; "0"; "*"; "0"; "*"];
["*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"];
["*"; "0"; "*"; "0"; "*"; "N"; "*"; "0"; "*"; "0"; "*"];
["*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"];
["*"; "0"; "*"; "0"; "*"; "0"; "*"; "0"; "*"; "0"; "*"];
["*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"];
["*"; "A"; "*"; "B"; "*"; "C"; "*"; "D"; "*"; "E"; "*"];
["*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"; "*"] ]

我有这个字符串数组。我的问题是,我该如何打印这样的数组?

我最初的想法是使用嵌套的 for 循环打印出一些 boardArray.[row, column] ,但是,据我所知, row项目在
for row in boardArray do是 obj 类型;我不能完全通过需要 int 的 obj。我可以使用转换吗?我应该使用不同的方法?我是否严重误解了每个循环的 F# 用法?

最佳答案

通过使用 for..to 循环解决:

let initiateBoard = 
for r = 0 to Array2D.length1 boardArray - 1 do
for c = 0 to Array2D.length2 boardArray - 1 do
printfn "%A " boardArray.[r, c]

这将在新行上打印出每个元素,因此除此之外还需要一个单独的处理间距的函数。

关于multidimensional-array - F# 打印出一个二维字符串数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51014991/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com