所以我正在使用Livecode,我将一些数字输入到一个字段中,例如 1 2 3 4 五 6
我想将每两行相乘,例如1和2,3和4,5和6.
我正在使用此代码:
put 1 into x
repeat with y = x to the number of lines of field "Output"
put the text of line x of field "Output" into value1
put the text of line x + 1 of field "Output" into value2
put carDistance * carTimeSeconds into carSpeed // Every two lines multiply
repeat for each item b in value3
put value3 into valueList[b] // Just adds the speed to a list
end repeat
put y + 2 into x
end repeat
我得到这个输出: 2(正确) 12(正确) 30(正确) 然后20这是不正确的和我的问题。
感谢。
答案 0 :(得分:2)
看起来你已经在你的代码中交换了一些变量名,很难说,但无论如何这里有一种方法可以做你想要的:
local theTotal
put text of fld "output" into temp
repeat with N = 2 to (number of lines of temp) step 2
add (line N of temp) * (line (N-1) of temp) to theTotal
end repeat
以上假设您试图获得整体总数。如果要验证结果,可以执行此操作,这将在消息框中放置产品列表:
put text of fld "output" into temp
repeat with N = 2 to (number of lines of temp) step 2
put (line N of temp) * (line (N-1) of temp) & return after theProducts
end repeat
put theProducts
请注意,在使用字段时,将字段的内容放入变量并使用其中的数据更有效,而不是重复从字段中读取。