英文:
The question about three-dimensional array and assigning in Go
问题
我最近开始学习Go语言。
我有一个来自JetBrains的任务:“声明一个大小为4x4x4的float32类型的三维数组,将88.6赋值给其[1][0][2]元素,并最后将数组打印到控制台。”
我声明了一个数组,代码如下:
var array = [4][4][4]float32{}
现在我遇到了几个问题:
- 当我尝试将值88.6赋给数组时,出现错误:“'88.6'(未指定类型的浮点数)无法表示为类型[4]float32”。
- 我无法理解任务的要求。我需要将每个数组的[0][1][2]元素赋值为88.6,还是只需要将[0][1][2]维度的数组赋值为“88.6”?
希望我的问题清楚明了!
英文:
I recently started to learn Go.
I have a task from JetBrains: "Declare a three-dimensional array of the float32 type with the size of 4 by 4 by 4 elements, assign 88.6 to its [1][0][2] element, and finally print the array to the console."
I declared an array like this:
var array = [4][4][4]float32{}
Now i have few problems:
- When i'm trying to assign a value 88.6 to the array i get an error: "'88.6' (type untyped float) cannot be represented by the type [4]float32"
- I can't understand task. Do I need to assign [0][1][2] elements in every array to 88.6 or [0][1][2] dimensional arrays should be assigned to "88.6"
I hope my question is clear!
答案1
得分: 1
我认为这个任务要求你只给数组中的一个元素赋值:
package main
import "fmt"
func main() {
var array [4][4][4]float32
array[1][0][2] = 88.6
fmt.Println(array)
}
输出结果:
[[[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] [[0 0 88.6 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
英文:
I think the task is asking you to assign to only one element in the array:
package main
import "fmt"
func main() {
var array [4][4][4]float32
array[1][0][2] = 88.6
fmt.Println(array)
}
Output:
[[[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] [[0 0 88.6 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]] [[0 0 0 0] [0 0 0 0] [0 0 0 0] [0 0 0 0]]]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论