关于在Go语言中的三维数组和赋值的问题

huangapple go评论80阅读模式
英文:

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{}

现在我遇到了几个问题:

  1. 当我尝试将值88.6赋给数组时,出现错误:“'88.6'(未指定类型的浮点数)无法表示为类型[4]float32”。
  2. 我无法理解任务的要求。我需要将每个数组的[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:

  1. 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"
  2. 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]]]

huangapple
  • 本文由 发表于 2022年7月16日 19:08:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/73003667.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定