程式撰寫(三)
 
  • 題目五:九九乘法 (困難度:****)
    • 「九九乘法」大概是每一個學程式設計,第一個會去練習的題目
    • 程式:
    • to 9x9
      make "m 2
      make "n 1
      repeat 8[
      repeat 9[

      print (list :m "X :n "= :m*:n)
      make "n :n+1
      if :n>9 [make "n 1]
      ]
      make "m :m+1
      ]
      end

    • 你還有不同解法嗎?
      • 如運用「while」來做!
      • 如何調整,可以不用if :n>9 [make "n 1]那行!

  • 題目六:算出「質數」 (困難度:*****)
    • 「質數」是除了1與本身外,沒有其他的因數,如:2、3、5、7、11、13...
    • 程式:
    • to prime :a
      print 2
      make "b 3
      make "c :a-:b+1

      repeat :c[
      make "d 2
      make "e :b-:d
      make "f 0

      repeat :e[
      make "g remainder :b :d
      ifelse :g=0 [make "e -1][make "d :d+1 make "f :f+1]
      ]
      if :e=:f [print :b]
      make "b :b+1
      ]
      end

  • 題目七:猜數字遊戲 (困難度:******)
    • 構想中:也就是電腦會亂數出一個四位數的數值,然後我們輸入一個猜測的數值,電腦會告知「幾A幾B」,一直到完全答對為止!

  • 題目八:時鐘 (困難度:**********)
    • 本程式取自http://lp95monica.live.com.tw/
    • TO AAA
      CLOCK
      END

      TO CLOCK
      CS
      HT
      DRAWFACE
      MAKE "SEC 0
      MAKE "MIN 0
      MAKE "HOUR 0
      PLOTTIMEHOUR :HOUR
      PLOTTIMEMIN :MIN
      PLOTTIMESEC :SEC
      SETTIMER 1 1000 [UPDATE]
      END

      TO DRAWFACE
      SETHEADING 30
      PU
      REPEAT 12 ~
      [~
      FD 120 ~
      SETX XCOR-12 ~
      SETY YCOR+12 ~
      MAKE "SH HEADING
      SETHEADING 90
      LABEL REPCOUNT ~
      SETHEADING :SH
      SETX XCOR+12 ~
      SETY YCOR-12 ~
      BK 120 ~
      RT 30 ~
      ]
      END

      TO GETTIME
      MAKE "THETIME TIME
      MAKE "THETIME BUTFIRST :THETIME
      MAKE "THETIME BUTFIRST :THETIME
      MAKE "THETIME BUTFIRST :THETIME
      MAKE "THETIME FIRST :THETIME
      MAKE "THETIME PARSE MAP "OO :THETIME
      OUTPUT :THETIME
      END

      TO OO :A
      IFELSE EQUALP ": :A [OUTPUT CHAR 32] [OUTPUT :A]
      END

      TO PLOTTIMEHOUR :HOUR
      PENREVERSE
      SETPENSIZE [5 5]
      SETHEADING :HOUR*30
      PD
      FD 50
      PU
      BK 50
      END

      TO PLOTTIMEMIN :MIN
      PENREVERSE
      SETPENSIZE [2 2]
      SETHEADING :MIN*6
      PD
      FD 100
      PU
      BK 100
      END

      TO PLOTTIMESEC :SEC
      PENREVERSE
      SETPENSIZE [1 1]
      SETHEADING :SEC*6
      PD
      FD 100
      PU
      BK 100
      END

      TO UPDATE
      MAKE "THETIME GETTIME
      MAKE "NHOUR FIRST :THETIME
      MAKE "THETIME BUTFIRST :THETIME
      MAKE "NMIN FIRST :THETIME
      MAKE "THETIME BUTFIRST :THETIME
      MAKE "NSEC FIRST :THETIME
      PLOTTIMESEC :SEC
      IF NOT EQUALP :MIN :NMIN~
      [~
      PLOTTIMEMIN :MIN ~
      IF NOT EQUALP :HOUR :NHOUR~
      [~
      PLOTTIMEHOUR :HOUR ~
      PLOTTIMEHOUR :NHOUR ~
      ]~
      PLOTTIMEMIN :NMIN ~
      ]
      PLOTTIMESEC :NSEC
      MAKE "HOUR :NHOUR
      MAKE "MIN :NMIN
      MAKE "SEC :NSEC
      END

      TO ZZZ
      IFELSE YESNOBOX [WELCOME] [CHOOSE YES TO RUN EXAMPLE NOW, CHOOSE NO STUDY IT NOW.] [AAA] [EDALL]
      END

      MAKE "STARTUP [ZZZ]