跳轉到內容

AppleScript 程式設計/示例程式/計算器

來自華夏公益教科書,開放的書籍,開放的世界

簡單計算器

[編輯 | 編輯原始碼]

這個簡單的計算器使用了我們在數字和字串部分顯示對話方塊部分以及基本命令部分中學到的知識。它還包括“if”和“else”語句以及“tell”塊。嘗試將它放到指令碼編輯器中並執行它。

這是程式碼

--Brought to you by Smiler121
--Version 1.3

set r to return as text
display dialog "Choose one" buttons {"Add", "Subtract"}
set opr to the button returned of the result
if opr is "Add" then --you clicked add
	display dialog "Current equation: ?+?" & r & r & "What is the first number?" default answer "" --displays the current equation and
--asks for first number
	set num1 to the text returned of the result
	display dialog "Current equation: " & num1 & "+?" & r & r & "What is the second number?" default answer "" --displays the current
--equation and asks for next number
	set num2 to the text returned of the result
	display dialog "Equation: " & num1 & "+" & num2 & r & r & "Answer: " & num1 + num2 --displays the equation and calculates the answer
else --you clicked subtract
	display dialog "Current equation: ?-?" & r & r & "What is the first number?" default answer "" --displays the current equation and
--asks for first number
	set num1 to the text returned of the result
	display dialog "Current equation: " & num1 & "-?" & r & r & "What is the second number?" default answer "" --displays the current
--equation and asks for next number
	set num2 to the text returned of the result
	display dialog "Equation: " & num1 & "-" & num2 & r & r & "Answer: " & num1 - num2 --displays the equation and calculates the answer
end if

高階計算器

[編輯 | 編輯原始碼]

此計算器指令碼阻止除數字以外的所有內容輸入。嘗試將它放到指令碼編輯器中並執行它。

這是程式碼

--Brought to you by Smiler121
--Version 3.2

tell application "Finder"
	set r to return as text
        display dialog "Welcome to 'Advanced Calculator' Version 3.2" buttons {"Continue"}
	display dialog "Choose one" buttons {"Add", "Subtract", "Divide"} with title "Calculator"
	set opr to the button returned of the result
	if opr is "Add" then --you clicked add
		repeat
			set response1 to display dialog "Current equation: ?+?" & r & r & "What is the first number?" default answer "" with title "First number..."
--displays the current equation and asks for first number
			try
				set num1 to (text returned of response1) as number
				exit repeat
			on error --if input is not number
				set num1 to text returned of response1
				display dialog "\"" & num1 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
				set num1 to text returned of response1
			end try
		end repeat
		repeat
			set response2 to display dialog "Current equation: " & num1 & "+?" & r & r & "What is the second number?" default answer "" with title "Second number..."
--displays the current equation and asks for next number
			try
				set num2 to (text returned of the response2) as number --checks to see if input is number
				exit repeat
			on error --if input is not number
				set num2 to text returned of response2
				display dialog "\"" & num2 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
				set num2 to text returned of response2
			end try
		end repeat
		display dialog "Equation: " & num1 & "+" & num2 & r & r & "Answer: " & num1 + num2 with title "The answer is..." --displays the equation
--and calculates the answer
	else if opr is "Subtract" then --you clicked subtract
		repeat
			set response1 to display dialog "Current equation: ?-?" & r & r & "What is the first number?" default answer "" with title "First number..."
--displays the current equation and asks for first number
			try
				set num1 to (text returned of response1) as number
				exit repeat
			on error --if input is not number
				set num1 to text returned of response1
				display dialog "\"" & num1 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
				set num1 to text returned of response1
			end try
		end repeat
		repeat
			set response2 to display dialog "Current equation: " & num1 & "-?" & r & r & "What is the second number?" default answer "" with title "Second number..."
--displays the current equation and asks for next number
			try
				set num2 to (text returned of the response2) as number --checks to see if input is number
				exit repeat
			on error --if input is not number
				set num2 to text returned of response2
				display dialog "\"" & num2 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
				set num2 to text returned of response2
			end try
		end repeat
		display dialog "Equation: " & num1 & "-" & num2 & r & r & "Answer: " & num1 - num2 with title "The answer is..." --displays the equation and calculates the answer
	else if opr is "Divide" then --you clicked divide
		repeat
			set response1 to display dialog "Current equation: ?÷?" & r & r & "What is the first number?" default answer "" with title "First number..."
--displays the current equation and asks for first number
			try
				set num1 to (text returned of response1) as number --checks to see if input is number
				exit repeat
			on error --if input is not number
				set num1 to text returned of response1
				display dialog "\"" & num1 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
				set num1 to text returned of response1
			end try
		end repeat
		repeat
			set response2 to display dialog "Current equation: " & num1 & "÷?" & r & r & "What is the second number?" default answer "" with title "Second number..."
--displays the current equation and asks for next number
			set num2 to text returned of response2
			try
				set num2 to (text returned of the response2) as number --checks to see if input is number
				exit repeat
			on error --if input is not number
				set num2 to text returned of response2
				display dialog "\"" & num2 & "\" is not valid. Please input a number." buttons {"OK"} default button 1 with title "Numbers Only!" with icon caution
				set num2 to text returned of response2
			end try
			set num2 to text returned of response2
			if num2 is "" then --if user didn't input anything
				display dialog "Can't divide \"" & num1 & "\" by \"null\". Please try again." buttons {"OK"} default button 1 with title "Try Again" with icon caution
			else if num2 is "0" then --if the input is 0
				display dialog "Can't divide \"" & num1 & "\" by \"0\". Please try again." buttons {"OK"} default button 1 with title "Try Again" with icon caution
			else if num2 is not "0" then --if it's neither
				exit repeat
			end if
			set num2 to text returned of response2
		end repeat
		display dialog "Equation: " & num1 & "÷" & num2 & r & r & "Answer: " & num1 / num2 with title "The answer is..." --displays the equation and calculates the answer
	end if
end tell
下一頁: Safari 退出警告 | 上一頁: 字母替換
首頁: AppleScript 程式設計/示例程式
華夏公益教科書