如何使用Golang接收2D数组的输入?

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

How to take input in 2d-array using Golang?

问题

package main

import (
	"fmt"
)

func main() {
	numbers := [4][4]int{}
	even := [4]int{}
	odd := [4]int{}
	row := 4
	col := 4

	fmt.Println("在二维数组中输入数据")
	for row = 0; row < 4; row++ {
		for col = 0; col < 4; col++ {
			fmt.Scanf("%d", &numbers[row][col])
			if numbers[row][col]%2 == 0 {
				even[row] = numbers[row][col]
			} else {
				odd[row] = numbers[row][col]
			}
		}
	}

	fmt.Println("打印二维数组", numbers)
	fmt.Println("打印偶数数组", even)
	fmt.Println("打印奇数数组", odd)

}
//我的输出如下
//打印二维数组 [[1 0 2 0] [3 0 4 0] [5 0 6 0] [7 0 8 0]]
//打印偶数数组 [0 0 0 0]
//打印奇数数组 [1 3 5 7]

在你的代码中,你正确地声明了一个二维数组numbers、一个偶数数组even和一个奇数数组odd。然后,你使用嵌套的循环来接收用户输入的数据,并根据数据的奇偶性将其存储在相应的数组中。

然而,你遇到的问题是在输入过程中跳过了一些索引,并且偶数数组even没有正确地获取偶数值。

这是因为你在每次循环迭代时都将rowcol设置为4,这导致了索引的跳过。你可以将循环条件修改为row < 4col < 4,以确保在正确的范围内进行迭代。

此外,你只为每一行的第一个偶数值赋值了even[row],而没有为后续的偶数值赋值。你可以使用切片来动态地存储偶数值,而不是使用固定大小的数组。

下面是修改后的代码:

package main

import (
	"fmt"
)

func main() {
	numbers := [4][4]int{}
	var even []int
	odd := [4]int{}
	row := 4
	col := 4

	fmt.Println("在二维数组中输入数据")
	for row = 0; row < 4; row++ {
		for col = 0; col < 4; col++ {
			fmt.Scanf("%d", &numbers[row][col])
			if numbers[row][col]%2 == 0 {
				even = append(even, numbers[row][col])
			} else {
				odd[row] = numbers[row][col]
			}
		}
	}

	fmt.Println("打印二维数组", numbers)
	fmt.Println("打印偶数数组", even)
	fmt.Println("打印奇数数组", odd)

}

这样修改后,你将能够正确地接收输入并将偶数值存储在切片even中。

英文:

Golang
as u can see in this code(.go) am trying to input 2d arrays and storing even integers in even array and for odd in odd array. But we can see in its output as i mentioned below after code in comments it skips indexes during input? and even array can't get the even values. What am doing wrong?

 package main
    import (
	&quot;fmt&quot;
    )
    func main() {
	numbers := [4][4]int{}
	even := [4]int{}
	odd := [4]int{}
	row := 4
	col := 4

	fmt.Println(&quot;Enter data in your 2-D Array&quot;)
	for row = 0; row &lt; 4; row++ {
		for col = 0; col &lt; 4; col++ {
			fmt.Scanf(&quot;%d&quot;, &amp;numbers[row][col])
			if numbers[row][col]%2 == 0 {
				even[row] = numbers[row][col]
			} else {
				odd[row] = numbers[row][col]
			}
		}
	}

	fmt.Println(&quot;Printing 2D Arrays&quot;, numbers)
	fmt.Println(&quot;Print Even Array&quot;, even)
	fmt.Println(&quot;Print Odd Array&quot;, odd)

    }
    //My output is as follow
    //Printing 2D Arrays [[1 0 2 0] [3 0 4 0] [5 0 6 0] [7 0 8 0]]
    //Print Even Array [0 0 0 0]
    //Print Odd Array [1 3 5 7]

答案1

得分: 1

你的输入中有16个元素,你尝试将这些元素存储在长度为4的even数组和长度为4的odd数组中。显然需要跳过8个元素。你可以使用append函数将元素添加到evenodd数组中。

注意:当声明二维数组[rows][columns]int{}时,要注意在迭代过程中避免越界错误。

package main
import (
	"fmt"
)
func main() {
	numbers := [4][4]int{}
	var even []int
	var odd []int

	fmt.Println("在二维数组中输入数据")
	for row := 0; row < 4; row++ {
		for col := 0; col < 4; col++ {
			fmt.Scanf("%d", &numbers[row][col])
			if numbers[row][col]%2 == 0 {
				even = append(even, numbers[row][col])
			} else {
				odd = append(odd, numbers[row][col])
			}
		}
	}

	fmt.Println("打印二维数组", numbers)
	fmt.Println("打印偶数数组", even)
	fmt.Println("打印奇数数组", odd)

    //打印二维数组 [[1 2 3 4] [5 6 7 8] [9 10 11 12] [13 14 15 16]]
	//打印偶数数组 [2 4 6 8 10 12 14 16]
	//打印奇数数组 [1 3 5 7 9 11 13 15]

}
英文:

You have 16 elements in your input and you try to store those elements in length 4 even array and length 4 odd array. then it is obvious to skip 8 elements there. You can use append to even and odd arrays.

Note :- when you declare 2D array, [rows][columns]int{}. Be aware when iterating through this, otherwise it can be a Out of bound panic.

package main
import (
	&quot;fmt&quot;
)
func main() {
	numbers := [4][4]int{}
	var even []int
	var odd []int
	//row := 2
	//col := 4

	fmt.Println(&quot;Enter data in your 2-D Array&quot;)
	for row := 0; row &lt; 4; row++ {
		for col := 0; col &lt; 4; col++ {
			fmt.Scanf(&quot;%d&quot;, &amp;numbers[row][col])
			if numbers[row][col]%2 == 0 {
				even = append(even, numbers[row][col])
			} else {
				odd = append(odd, numbers[row][col])
			}
		}
	}

	fmt.Println(&quot;Printing 2D Arrays&quot;, numbers)
	fmt.Println(&quot;Print Even Array&quot;, even)
	fmt.Println(&quot;Print Odd Array&quot;, odd)

    //Printing 2D Arrays [[1 2 3 4] [5 6 7 8] [9 10 11 12] [13 14 15 16]]
	//Print Even Array [2 4 6 8 10 12 14 16]
	//Print Odd Array [1 3 5 7 9 11 13 15]

}

huangapple
  • 本文由 发表于 2021年6月3日 16:11:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/67817786.html
匿名

发表评论

匿名网友

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

确定