说出数字 – Sololearn Go 挑战

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

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 &quot;fmt&quot;

func main() {
   
   for i:=0;i&lt;3;i++ {
   var x int
   fmt.Scanln(&amp;x)

   if (x&gt;=0 &amp;&amp; x&lt;10) {
  
       switch x {
          case 0:
            fmt.Print(&quot;Zero&quot;)
          case 1:
            fmt.Print(&quot;One&quot;)
          case 2:
            fmt.Print(&quot;Two&quot;)
          case 3:
            fmt.Print(&quot;Three&quot;)
          case 4:
            fmt.Print(&quot;Four&quot;)
          case 5:
            fmt.Print(&quot;Five&quot;)
          case 6:
            fmt.Print(&quot;Six&quot;)
          case 7:
            fmt.Print(&quot;Seven&quot;)
          case 8:
            fmt.Print(&quot;Eight&quot;)
          case 9:
            fmt.Print(&quot;Nine&quot;)
          case 10:
            fmt.Print(&quot;Ten&quot;)
          default:
            fmt.Println(&quot;This is not a number or a number between 0 and 10&quot;)
         }
        }
        if i&lt;2 {
           fmt.Println(&quot;&quot;)
        }
       }
   
  }

</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&lt;10` with `x&lt;=10` and all the `fmt.Print` with `fmt.Println` and remove this parts.

        if i&lt;2 {
           fmt.Println(&quot;&quot;)
        }

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 &quot;fmt&quot;
    
    var validWrittenNumbers = map[int]string{
    	1: &quot;One&quot;,
    	2: &quot;Two&quot;,
    	3: &quot;Three&quot;,
     	4: &quot;Four&quot;,
    	5: &quot;Five&quot;,
     	6: &quot;Six&quot;,
     	7: &quot;Seven&quot;,
     	8: &quot;Eight&quot;,
     	9: &quot;Nine&quot;,
     	10: &quot;Ten&quot;,
    }
    
    func printValidWrittenNumber(candidateNumber int) {
    	if stringNumber, ok := validWrittenNumbers[candidateNumber]; ok {
    		fmt.Println(stringNumber)
    	}
    }
    
    func main() {
    	inputTimes := 3
    
    	for index := 0; index &lt; inputTimes; index++ {
    		var candidateNumber int
    		_, _ = fmt.Scanln(&amp;candidateNumber)
    
    		printValidWrittenNumber(candidateNumber)
    	}
    }

I hope this works and sorry for not being more helpful!

</details>



huangapple
  • 本文由 发表于 2022年2月5日 23:48:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/70999570.html
匿名

发表评论

匿名网友

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

确定