如何简化这个菜单

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

How do i simplify this menu

问题

所以我正在开始一个我想做很久的项目,首先我要做的是打印一个菜单。我想知道是否有更好的编码方式来实现这段代码。为了"美化"它,我会这样调用它:

我在想要做这样的事情:

fmt.Println("You have chosen option " + input)

但是我必须为每个选择的选项调用一个不同命名的函数,所以我不确定如何使其工作。

func main() {
	fmt.Println("Hello welcome")
	var input int
	fmt.Println("Please choose an option:")
	fmt.Scanln(&input)
	if input == 1 {
		fmt.Println("Option 1 chosen")
	} else if input == 2 {
		fmt.Println("Option 2 chosen")
	} else if input == 3 {
		fmt.Println("Option 3 chosen")
	} else if input == 4 {
		fmt.Println("Option 4 chosen")
	} else if input == 5 {
		fmt.Println("Option 5 chosen")
	} else if input == 6 {
		fmt.Println("Option 6 chosen")
	} else if input == 7 {
		fmt.Println("Option 7 chosen")
	} else if input == 8 {
		fmt.Println("Option 8 chosen")
	} else if input == 9 {
		fmt.Println("Option 9 chosen")
	} else if input == 10 {
		fmt.Println("Option 10 chosen")
	} else {
		fmt.Print("Not an option")
	}
}
英文:

So I'm starting a project that I've wanted to do for a while and the first thing I have to do is print a menu
I was wondering whether there was a nicer / better way of coding this code.
To "prettify" it I'd call it
I was thinking of doing something like

fmt.Println("You have chosen option " + input)

But I have to call a different named function for each option chosen, so I'm not sure how I'd make this work

func main() {
	fmt.Println("Hello welcome")
	var input int
	fmt.Println("Please choose an option:")
	fmt.Scanln(&input)
	if input == 1 {
		fmt.Println("Option 1 chosen")
	} else if input == 2 {
		fmt.Println("Option 2 chosen")
	} else if input == 3 {
		fmt.Println("Option 3 chosen")
	} else if input == 4 {
		fmt.Println("Option 4 chosen")
	} else if input == 5 {
		fmt.Println("Option 5 chosen")
	} else if input == 6 {
		fmt.Println("Option 6 chosen")
	} else if input == 7 {
		fmt.Println("Option 7 chosen")
	} else if input == 8 {
		fmt.Println("Option 8 chosen")
	} else if input == 9 {
		fmt.Println("Option 9 chosen")
	} else if input == 10 {
		fmt.Println("Option 10 chosen")
	} else {
		fmt.Print("Not an option")
	}
}

答案1

得分: 3

我建议创建一个选项号到函数的映射表,像这样:

func func1() {
	fmt.Print("选择了选项1")
}

func func2() {
	fmt.Print("选择了选项2")
}

func main() {
	funcs := map[int]func() {
		1: func1,
		2: func2,
	}
	fmt.Println("欢迎!")
	var input int
	fmt.Println("请选择一个选项:")
	fmt.Scanln(&input)
	f, ok := funcs[input]
	if !ok {
		fmt.Print("不是一个有效选项")
	} else {
		f()
	}
}
英文:

I would suggest making a map of options number to functions, like this:

func func1() {
fmt.Print("Option 1 Chosen")
}
func func2() {
fmt.Print("Option 2 Chosen")
}
func main() {
funcs := map[int]func() {
1: func1,
2: func2,
}
fmt.Println("Hello welcome")
var input int
fmt.Println("Please choose an option:")
fmt.Scanln(&input)
f, ok := funcs[input]
if !ok {
fmt.Print("Not an option")
} else {
f()
}
}

huangapple
  • 本文由 发表于 2021年9月28日 21:28:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/69362671.html
匿名

发表评论

匿名网友

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

确定