英文:
how to use a `switch` statement in Go
问题
package main
import "fmt"
func main() {
var age int
fmt.Scanf("%d", &age)
// 在这里编写你的 switch 或者 if...else-if 语句。
switch age {
case 1:
age <= 14
fmt.Println("Toy Story 4")
case 2:
age <= 18
fmt.Println("The Matrix")
case 3:
age <= 25
fmt.Println("John Wick")
case 4:
age <= 35
fmt.Println("Constantine")
case 5:
age > 35
fmt.Println("Speed")
default:
fmt.Println("Error: Unknown age")
}
}
我在IDE中的scanf
处得到了一个黄色标记,标记出一个未处理的错误。此外,所有的case
语句的第一行都被标记为红色,导致无法编译。我得到的错误是age <= 14
被评估,但未被使用。对于所有的语句都是如此。我在网上搜索并查看了示例,从我所看到的材料和示例中,代码是正确的。这是来自 Go Land (JetBrains) 的屏幕截图:
有人有什么想法吗?另外,如果你回答,请记住,这是一个 Go 课程的一部分,我必须使用 scanf
。我可以将 switch
改为 else if
,这可能会解决问题,但我有点好奇为什么会发生这种情况,以及如何解决它。
英文:
package main
import "fmt"
func main() {
var age int
fmt.Scanf("%d", &age)
// Code your switch or if...else-if statement here.
switch age {
case 1:
age <= 14
fmt.Println("Toy Story 4")
case 2:
age <= 18
fmt.Println("The Matrix")
case 3:
age <= 25
fmt.Println("John Wick")
case 4:
age <= 35
fmt.Println("Constantine")
case 5:
age > 35
fmt.Println("Speed")
default:
fmt.Println("Error: Unknown age")
}
}
I get a yellow marker in the IDE for the scanf
, flagging an unhandled error. Also, all the cases have the first line flagged red, preventing to compile. The error I get is age <= 14 is evaluated, but not used
. The same is true for all statements. I have searched the web and looked at examples, and from what I can see, the code is, as the materials and examples state. Here is a screenshot from Go Land (JetBrains):
Does anybody have an idea? Also, if you answer, please keep in mind, that this is a lesson from a Go course and I have to use scanf. I could change the switch to an else if, which is likely going to fix the issue, but I am kind of curious, about why this is happening and how I can fix it.
答案1
得分: 1
age <= 14
不是一个有效的 Go 表达式,尽管它可以作为一个表达式的一部分使用。Go 对于所有的值都要求被消耗非常严格。
这不是使用 <=
运算符编写 case
的正确方式。
switch age {
case 1:
age <= 14
fmt.Println("Toy Story 4")
...
}
这个 case
会在 age
的值为 1
时匹配。你应该使用以下的 switch 样式:
switch {
case age <= 14:
fmt.Println("Toy Story 4")
case age <= 18:
...
...
}
英文:
age <= 14
is not a valid Go expression, though it could be used as part of one. Go is very particular about all values being consumed.
This isn't the correct way to write a case
with <=
operators.
switch age {
case 1:
age <= 14
fmt.Println("Toy Story 4")
...
}
That case matches if the value of age
is 1
. You want to use a switch style like this:
switch {
case age <= 14:
fmt.Println("Toy Story 4")
case age <= 18:
...
...
}
答案2
得分: 1
为了补充Daniel所写的内容 - 基本上,switch语句(除非是类型switch,但我们不要离题)有两种形式:
switch { // 注意这里没有任何表达式
case bool_expr_1:
...
case bool_expr_2:
...
和
switch any_expr {
case value_to_match_1:
...
case value_to_match_2:
...
在第一种形式中,每个case
分支中的表达式从上到下进行评估,并且首个评估为true
的表达式会“胜出” - 也就是说,执行该分支中的代码。
在第二种形式中,表达式被评估为产生一个值,然后将该值与每个case分支中的值进行精确比较。
你试图将这两种形式结合起来,但这是行不通的。
你可能认为case分支中的那些数字是分支应该被检查的顺序,但实际上不是这样的。
英文:
To add to what Daniel wrote — basically the switch statement (unless it's a type switch but let's not digress) has two forms:
switch { // note the absense of any expression here
case bool_expr_1:
...
case bool_expr_2:
...
and
switch any_expr {
case value_to_match_1:
...
case value_to_match_2:
...
In the first form, expressions in each case
branch are evaluated top-to-bottom, and the first which evaluates to true
, "wins"—that is, the code in that branch is executed.
In the second form, the expression is evaluated to produce a value which is then compared exactly with the value in each case branch.
You have tried to sort-of combine both forms, which won't work.
You might have thought that those numbers in the case branches is the order of how the branches should be checked but no, that's not it.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论