+ 2
Can anyone tell me what's wrong with this code. I'm new to GO.
We need to take 3 inputs within range 0-10 and should output the relevent word. Example If we input 2 then it outputs "Two". https://code.sololearn.com/c5Di598eYcf2/?ref=app
6 Respostas
+ 3
The following works instead:
package main
import "fmt"
func main() {
//your code goes here
for x:=0; x<3; x++ {
var num int
fmt.Scanln(&num)
switch num {
case 0:
fmt.Println("Zero")
case 1:
fmt.Println("One")
case 2:
fmt.Println("Two")
case 3:
fmt.Println("Three")
case 4:
fmt.Println("Four")
case 5:
fmt.Println("Five")
case 6:
fmt.Println("Six")
case 7:
fmt.Println("Seven")
case 8:
fmt.Println("Eight")
case 9:
fmt.Println("Nine")
case 10:
fmt.Println("Ten")
}
}
}
2 changes were needed:
- move { to end of the switch line like: switch num {
- replace "case num==0:" with "case 0:"
+ 2
Yeah, maybe they took that pickiness from Python. Python cares a lot about indentation and Go doesn't. Go cares about where the brackets go but Python just has no brackets.
I wish new languages didn't try so hard to be different when they don't need to be. It adds extra learning curve for no clear reason.
+ 1
Thank you Josh. Then, the place of the braces is important in GO. I thought we can put anywhere. 😁
+ 1
Yep, GO has many things similar to java, c++... It looks like a mix of features from all these languages.
+ 1
Here is a shorter solution instead of using a switch:
package main
import "fmt"
func main() {
words := [11]string{"Zero", "One", "Two", "Three", "Four",
"Five", "Six", "Seven", "Eight", "Nine", "Ten"}
for i:=0; i < 3; i++ {
var num int
fmt.Scanln(&num)
fmt.Println(words[num])
}
}
+ 1
I think that's the best answer. It makes code more simpler . If we use switch we need to write more lines. So using arrays we can do it simply. I think I need to practice again. Thank you Josh.