50733113-陳志嘉個人網頁

  • Home
    • Site Map
    • reveal
    • blog
  • 首頁簡介
    • 頁面資源
      • git http 與 ssh
      • 目錄結構
      • 頁面編輯
      • 插入程式碼
      • Java 程式碼
      • Python 程式碼
      • C或C++程式碼
      • Lua 程式碼
      • Javascript 程式碼
      • Html 原始碼
      • 網際簡報
      • 網誌編輯
      • 已知錯誤
  • 相關資訊
    • Proxy 設定
    • 隨身碟格式化
    • GitHub 倉儲建立
  • 筆記整理
    • git 等相關指令
  • 課程內容
    • 校園免費授權
      • windows 10
    • TWAREN SSL-VPN
    • Ubuntu 18.04
    • FileZilla
    • VirtualBox
    • 虛擬主機開啟動態網站
  • ROC flag(w14)
    • ROC Flag 成果
  • 程式
    • 亂數產生系統
    • 骰子
    • 繪圖
  • 期中操作影片
  • 期末報告
C或C++程式碼 << Previous Next >> Javascript 程式碼

Lua 程式碼

-- 導入 js 模組
js = require("js")
-- 取得 window
window = js.global
-- 猜小於或等於 n 的整數
big = 100
-- 計算猜測次數, 配合 while 至少會猜一次
num = 1
-- 利用 window:prompt 方法回應取得使用者所猜的整數
guess = window:prompt("請猜一個介於 1 到 "..big.." 的整數")
-- 利用數學模組的 random 函數以亂數產生答案
answer = math.random(big)
output = ""
-- 若沒猜對, 一直猜到對為止
while answer ~= tonumber(guess) do
    if answer > tonumber(guess) then
        output = "猜第 "..num.." 次, guess="..guess..", answer="..answer.." - too small"
        print(output)
    else
        output = "猜第 "..num.." 次, guess="..guess..", answer="..answer.." - too big"
        print(output)
    end 
    guess = window:prompt(output..", 請猜一個介於 1 到 "..big.." 的整數")
    num = num + 1
end
print("總共猜了 "..num.." 次, answer=guess="..answer.." - correct")
    


C或C++程式碼 << Previous Next >> Javascript 程式碼

Copyright © All rights reserved | This template is made with by Colorlib