加载中...

习题 2: 注释和井号


程式里的注释是很重要的。它们可以用自然语言告诉你某段程式码的功能是什么。在你想要临时移除一段程式码时,你还可以用注释的方式将这段程式码临时禁用。接下来的练习将让你学会注释:

  1. # A comment, this is so you can read your program later.
  2. # Anything after the # is ignored by Ruby.
  3. puts "I could have code like this." # and the comment after is ignored
  4. # You can also use a comment to "disable" or comment out a piece of code:
  5. # print "This won't run."
  6. puts "This will run."

你应该看到的结果

  1. $ ruby ex2.rb
  2. I could have code like this.
  3. This will run.
  4. $

加分习题

  1. 弄清楚 # 符号的作用。而且记住它的名称。(中文为井号,英文为 octothorpe 或者 pound character)。
  2. 打开你的 ex2.rb 档案,从后往前逐行检查。从最后一行开始,倒著逐个单词单词检查回去。
  3. 有发现更多错误嘛?有的话就改正过来。
  4. 阅读你写的习题,把每个字符(charcter)都读出来。有没有发现更多的错误呢?有的话也一样改正过来。

还没有评论.