英文:
Why doesn't Scanf() work correctly for me?
问题
我正在尝试从Python转向Go语言,并且在我有限的知识下,我试图制作一个基本的计算器。然而,由于某种原因,我无法正确使用Scanf函数。它似乎只接受第一个scanf,而完全忽略了第二个scanf。
package main
import (
"fmt"
)
var x int
var y int
var result int
var input float64
func add(x int, y int) int {
sum := x + y
return sum
}
func sub(x int, y int) int {
sum := x - y
return sum
}
func div(x int, y int) int {
sum := x / y
return sum
}
func mul(x int, y int) int {
sum := x * y
return sum
}
func main() {
fmt.Println("Which type?\n1: Add\n2: Subtract\n3: Divide\n4: Multiply")
fmt.Scanf("%d", &input)
fmt.Println("Input numbers separated by space")
fmt.Scanf("%d", &x, &y)
switch input {
case 1:
result = add(x, y)
case 2:
result = sub(x, y)
case 3:
result = div(x, y)
case 4:
result = mul(x, y)
}
fmt.Println(result)
}
以上是你提供的代码。
英文:
I'm trying to move from Python to GO and with my minimal knowledge I tried to make a basic calculator. However i for some reason can't get Scanf to work properly. It only seems to accept the first scanf but the second one is completely ignored
package main
import (
"fmt"
)
var x int
var y int
var result int
var input float64
func add(x int, y int) int {
sum := x + y
return sum
}
func sub(x int, y int) int {
sum := x - y
return sum
}
func div(x int, y int) int {
sum := x / y
return sum
}
func mul(x int, y int) int {
sum := x * y
return sum
}
func main() {
fmt.Println("Which type?\n1: Add\n2: Subtract\n3: Divide\n4:
Multiply")
fmt.Scanf("%d", &input)
fmt.Println("Input numbers seperated by space")
fmt.Scanf("%d", x, y)
switch input {
case 1:
result = add(x, y)
case 2:
result = sub(x, y)
case 3:
result = div(x, y)
case 4:
result = mul(x, y)
}
fmt.Println(result)
}
答案1
得分: 0
第二次调用Scanf函数,Scanf("%d", x, y)
只提供了一个转换说明符,但给出了两个变量。
此外,这次调用只传递了变量的值,而不是它们的地址。
正确的调用应该是Scanf("%d %d", &x, &y)
。
在第一次调用Scanf时,你说:Scanf("%d", &input)
。第二个参数的语法& variable
表示对命名变量的引用。
input
被声明为全局变量,但只有在它的声明之后才可见。由于input
在main
函数内部是可见的,但在Scanf
函数内部不可见,为了让Scanf
函数能够改变另一个作用域中的值,必须将其地址作为参数传递,而不是它的值。
地址的接收者(在这里是Scanf
函数)可以在仍然在作用域内的帧中更改变量的值;在这种情况下,是main
函数。
请参阅Go语言的文档以获取类似的解释:https://golang.org/ref/spec#Address_operators
英文:
The second call to Scanf, Scanf("%d", x, y)
only provides one conversion specifier but was given two variables.
Moreover, this second call only passes the variables' values, not their addresses.
It seems the correct call would be Scanf("%d %d", &x, &y)
In the first call to Scanf you said: Scanf("%d", &input)
. The second argument's syntax, & variable
, denotes a reference to the named variable.
input
was declared global, but is only visible after its declaration. Since input
is in scope within main
but not within Scanf
, in order for Scanf
to change the value in another scope, the address must be given as an argument, rather than its value.
The recipient of the address (here Scanf
) can then change the value of the variable in the frame in which it is still in scope; in this case, main
.
See Go's documentation for a similar explanation: https://golang.org/ref/spec#Address_operators
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论