英文:
Say The Numbers - Sololearn Go Challenge
问题
我目前正在使用SoloLearn应用程序学习Go语言。挑战是:输入3个在0-10范围内的数字,并输出相应的英文文本。测试用例#1、测试用例#3、测试用例#4和测试用例#5都成功了。但是我在总结测试用例#2和测试用例#6时遇到了问题。提前感谢您的帮助。
package main
import "fmt"
func main() {
for i:=0;i<3;i++ {
var x int
fmt.Scanln(&x)
if (x>=0 && x<10) {
switch x {
case 0:
fmt.Print("Zero")
case 1:
fmt.Print("One")
case 2:
fmt.Print("Two")
case 3:
fmt.Print("Three")
case 4:
fmt.Print("Four")
case 5:
fmt.Print("Five")
case 6:
fmt.Print("Six")
case 7:
fmt.Print("Seven")
case 8:
fmt.Print("Eight")
case 9:
fmt.Print("Nine")
case 10:
fmt.Print("Ten")
default:
fmt.Println("This is not a number or a number between 0 and 10")
}
}
if i<2 {
fmt.Println("")
}
}
}
英文:
I'm currently learning Go using the SoloLearn App.<br>
The Challenge was :<br> Taking 3 numbers in the range of 0-10 as input and output the corresponding texts in English.<br>
Test Case #1, Test Case #3, Test Case #4 and #Test Case #5 were successful.<br>
But I have problems concluding Test Case #2 and Test Case #6.<br>
Thank you in advance for any help.
package main
import "fmt"
func main() {
for i:=0;i<3;i++ {
var x int
fmt.Scanln(&x)
if (x>=0 && x<10) {
switch x {
case 0:
fmt.Print("Zero")
case 1:
fmt.Print("One")
case 2:
fmt.Print("Two")
case 3:
fmt.Print("Three")
case 4:
fmt.Print("Four")
case 5:
fmt.Print("Five")
case 6:
fmt.Print("Six")
case 7:
fmt.Print("Seven")
case 8:
fmt.Print("Eight")
case 9:
fmt.Print("Nine")
case 10:
fmt.Print("Ten")
default:
fmt.Println("This is not a number or a number between 0 and 10")
}
}
if i<2 {
fmt.Println("")
}
}
}
</details>
# 答案1
**得分**: 0
也许通过分享测试用例,我们可以更容易地理解发生了什么。
但是乍一看,我会将`x < 10`替换为`x <= 10`,将所有的`fmt.Print`替换为`fmt.Println`并删除这些部分。
if i < 2 {
fmt.Println("")
}
如果保留验证输入数字在0到10之间的条件,那么默认情况下的消息将永远不会被打印出来。
还让我分享另一种解决方案的方法,它与之前的解决方案相同,但有一些良好的实践方法:
package main
import "fmt"
var validWrittenNumbers = map[int]string{
1: "One",
2: "Two",
3: "Three",
4: "Four",
5: "Five",
6: "Six",
7: "Seven",
8: "Eight",
9: "Nine",
10: "Ten",
}
func printValidWrittenNumber(candidateNumber int) {
if stringNumber, ok := validWrittenNumbers[candidateNumber]; ok {
fmt.Println(stringNumber)
}
}
func main() {
inputTimes := 3
for index := 0; index < inputTimes; index++ {
var candidateNumber int
_, _ = fmt.Scanln(&candidateNumber)
printValidWrittenNumber(candidateNumber)
}
}
希望这个能够工作,对于没有提供更多帮助我感到抱歉!
<details>
<summary>英文:</summary>
Maybe by sharing the test cases would be easier for us to understand whats happening.
But at a first sight I would replace the `x<10` with `x<=10` and all the `fmt.Print` with `fmt.Println` and remove this parts.
if i<2 {
fmt.Println("")
}
If you keep the condition that validates that the input number is between 0 and 10 the message of the default case would never be printed.
And also let me share you another approach of the solution, is the same but with a couple of good practices:
package main
import "fmt"
var validWrittenNumbers = map[int]string{
1: "One",
2: "Two",
3: "Three",
4: "Four",
5: "Five",
6: "Six",
7: "Seven",
8: "Eight",
9: "Nine",
10: "Ten",
}
func printValidWrittenNumber(candidateNumber int) {
if stringNumber, ok := validWrittenNumbers[candidateNumber]; ok {
fmt.Println(stringNumber)
}
}
func main() {
inputTimes := 3
for index := 0; index < inputTimes; index++ {
var candidateNumber int
_, _ = fmt.Scanln(&candidateNumber)
printValidWrittenNumber(candidateNumber)
}
}
I hope this works and sorry for not being more helpful!
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论