Skip to content

Instantly share code, notes, and snippets.

@dlintw
Last active July 24, 2021 15:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dlintw/990484733fee8a605ef1ff72c388a354 to your computer and use it in GitHub Desktop.
Save dlintw/990484733fee8a605ef1ff72c388a354 to your computer and use it in GitHub Desktop.
我的第一隻 Julia 小遊戲
# Julia 剪刀石頭布
# 1. 變數可用中文
# 2. 加上 try catch 錯誤處理
# 3. 加上多行註解 #= =#
# 4. 採用公式計算結果
"""
`input(prompt::AbstractString="")`
Read a string from STDIN. The trailing newline is stripped.
The prompt string, if given, is printed to standard output without a
trailing newline before reading input.
"""
function input(prompt::AbstractString="")
print(prompt)
return chomp(readline())
end
while true
computer=rand([1:3;])
try
答 = parse(Int,input("1:剪刀,2:石頭,3:布,輸入1~3: "))
if 1<=答<=3
println("電腦出 $computer",
["平手","你輸了","你贏了"][mod(computer+3-答,3) + 1])
else
println("輸入錯誤的資料,脫離程式")
break
end
#=
if computer == 答
println("平手")
elseif mod(computer + 3 - 答, 3) == 1
println("電腦出 $computer, 你輸了")
else
println("電腦出 $computer, 你贏了")
end
=#
catch err
println("輸入錯誤的資料,脫離程式")
break
end
end
# vim:et sw=4 ts=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment