大佬教程收集整理的这篇文章主要介绍了ios – Swift中的算术运算符数组,大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
var operatorArray = ['+','-','*','/'] // or =[+,-,*,/] ?
我只想随机生成数字,然后从上面的数组中随机选择算术运算符并执行等式.例如,
var firstNum = Int(arc4random_uniform(120)) var secondNum = Int(arc4random_uniform(120)) var equation = firstNum + operatorArraY[Int(arc4random_uniform(3))] + secondNum //
上面的’等式’会起作用吗?
谢谢.
单一运算符:
// declare a variable that holds a function let op: (Int,int)->Int = (+) // run the function on two arguments op(10,10)
使用数组,您可以使用map来应用每个数组:
// operatorArray is an array of functions that take two ints and return an int let operatorArray: [(Int,int)->Int] = [(+),(-),(*),(/)] // apply each operator to two numbers let result = map(operatorArray) { op in op(10,10) } // result is [20,100,1]
以上是大佬教程为你收集整理的ios – Swift中的算术运算符数组全部内容,希望文章能够帮你解决ios – Swift中的算术运算符数组所遇到的程序开发问题。
如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。
本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。