在苹果脚本中将一个整数乘以另一个整数一定次数

Multiplying an integer by another integer certain number of times in apple script

我最近尝试在 apple script 中多次将两个整数相乘,但没有成功。我试图使用各种重复循环,但整数只乘以一次,然后什么也没有发生。我的代码看起来像这样。

display dialog "Enter the first number here" default answer ""
set firstnumber to text returned of result
display dialog "Enter the second number." default answer ""
set secondnumber to text returned of result
display dialog "How many times numbers should be multiplied?" default answer ""
set multiplier to text returned of result
repeat multiplier times
    set finalresult to firstnumber * secondnumber
end repeat
display dialog finalresult

给定的脚本一遍又一遍地将第一个数字乘以第二个数字。相反,您 想要 它迭代:将前一个乘法 的 结果乘以第二个数字。你这样做:

display dialog "Enter the first number here" default answer ""
set firstnumber to text returned of result
display dialog "Enter the second number." default answer ""
set secondnumber to text returned of result
display dialog "How many times numbers should be multiplied?" default answer ""
set multiplier to text returned of result
-- set the output variable to the first number
set finalresult to firstnumber
repeat multiplier times
    -- repeatedly multiply the output variable by the second number,
    -- storing it back in the output variable
    set finalresult to finalresult * secondnumber
end repeat
display dialog finalresult

一种更直接的方法通过观察重复将某个常数乘以另一次来否定重复循环的需要,即:

×××× ……×

(出现在连续乘法中)等同于可以表示为:

×

是您的起始值,可以称为 系数 。是 base (of exponentiation),这是我们的起始系数的指数增长速率。是 exponent(或 power),它决定了将增加多少个数量级。

因此,您可以将 ⓵ 中的 's 字符串替换为单个二元取幂运算,即 ,大多数编程语言都有指定的运算符。在 AppleScript 中,它是 ^.

set coeff to the text returned of (display dialog "Enter the coefficient, i.e. a starting value")
set base to the text returned of (display dialog "Enter the base of exponentiation")
set pow to the text returned of (display dialog "Enter the power of exponentiation")

return coeff * (base ^ pow)