英文:
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
没有正确地获取偶数值。
这是因为你在每次循环迭代时都将row
和col
设置为4,这导致了索引的跳过。你可以将循环条件修改为row < 4
和col < 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 (
"fmt"
)
func main() {
numbers := [4][4]int{}
even := [4]int{}
odd := [4]int{}
row := 4
col := 4
fmt.Println("Enter data in your 2-D Array")
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("Printing 2D Arrays", numbers)
fmt.Println("Print Even Array", even)
fmt.Println("Print Odd Array", 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
函数将元素添加到even
和odd
数组中。
注意:当声明二维数组[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 (
"fmt"
)
func main() {
numbers := [4][4]int{}
var even []int
var odd []int
//row := 2
//col := 4
fmt.Println("Enter data in your 2-D Array")
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("Printing 2D Arrays", numbers)
fmt.Println("Print Even Array", even)
fmt.Println("Print Odd Array", 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]
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论