我创建了这个程序
package main
import "fmt"
var bankAccount = 12342
func withdraw(withdrawAmnt int) int { //withdraw function, return int
bankAccount -= withdrawAmnt
if bankAccount < 0 {
bankAccount += withdrawAmnt //bankaccount cannot go negative
return 1
} else { return 0 }
}
func deposit(depositAmnt int) {
bankAccount += depositAmnt
}
func main() {
var choice int
var withdraw int
var deposit int
fmt.Printf("%d \n 1. withdraw \n 2. deposit \n 3. exit")
fmt.Scanln(&choice)
switch choice {
case 1:
fmt.Scanln(&withdraw)
if withdraw(withdraw) != 0 {
fmt.Println("Not succesful: not enough money (you're broke)")
}
case 2:
fmt.Scanln(&deposit)
deposit(deposit)
case 3:
os.Exit()
}
}
Run Code Online (Sandbox Code Playgroud)
我不断收到此错误:无法调用非功能存款(int 类型)并且无法调用非功能存款(int 类型)。
它的意思正是它所说的:你不能调用非函数。只能调用函数。
这里:
deposit(deposit)
Run Code Online (Sandbox Code Playgroud)
您正在尝试调用deposit,但deposit类型为int:
var deposit int
Run Code Online (Sandbox Code Playgroud)
此变量声明deposit隐藏了包范围内的同名函数。
要解决您的问题,请为变量或函数使用不同的名称。
| 归档时间: |
|
| 查看次数: |
4077 次 |
| 最近记录: |