# go语言Exercise: Slices

xiongwei1209 · · 1418 次点击 · · 开始浏览

## Implement `Pic`. It should return a slice of length `dy`, each element of which is a slice of `dx` 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values.

The choice of image is up to you. Interesting functions include `x^y`,`(x+y)/2`, and `x*y`.

(You need to use a loop to allocate each `[]uint8` inside the `[][]uint8`.)

(Use `uint8(intValue)` to convert between types.)

package main

import "tour/pic"

func Pic(dx, dy int) [][]uint8 {
image := make([][]uint8, dy)
for i := 0; i < dy; i++ {
image[i] = make([]uint8, dx)
}
for i:= 0; i < dy; i++ {
for j := 0; j < dx; j++ {
image[i][j] = (uint8)(i*j)%255  // 这里的函数用要求给的三个函数可以得到图像的不同的效果
}
}
return image
}

func main() {
pic.Show(Pic)
}

0 回复

• 请尽量让自己的回复能够对别人有帮助
• 支持 Markdown 格式, **粗体**、~~删除线~~、``单行代码``
• 支持 @ 本站用户；支持表情（输入 : 提示），见 Emoji cheat sheet
• 图片支持拖拽、截图粘贴等方式上传

## Implement `Pic`. It should return a slice of length `dy`, each element of which is a slice of `dx` 8-bit unsigned integers. When you run the program, it will display your picture, interpreting the integers as grayscale (well, bluescale) values.

The choice of image is up to you. Interesting functions include `x^y`,`(x+y)/2`, and `x*y`.

(You need to use a loop to allocate each `[]uint8` inside the `[][]uint8`.)

(Use `uint8(intValue)` to convert between types.)

package main

import "tour/pic"

func Pic(dx, dy int) [][]uint8 {
image := make([][]uint8, dy)
for i := 0; i < dy; i++ {
image[i] = make([]uint8, dx)
}
for i:= 0; i < dy; i++ {
for j := 0; j < dx; j++ {
image[i][j] = (uint8)(i*j)%255  // 这里的函数用要求给的三个函数可以得到图像的不同的效果
}
}
return image
}

func main() {
pic.Show(Pic)
}