所以我的目标在这个码位是随机滚动两个骰子,因为我们都知道你的常规模具只有6面,所以我导入Foundation访问arc4random_uniform(UInt32)。我尝试使用范围(1..7),以避免随机获取0,但返回一个错误,我不喜欢太多。我试图这样做:
原文链接:https://www.f2er.com/swift/321422.htmldice1 = arc4random_uniform(UInt32(1..7))
然而,返回
Could not find an overload for ‘init’ that accepts the supplied arguments
我希望这是足够的信息,你惊人的debs在那里帮助我:)
请注意,我只是在操场上练习快速。我不是必须学习如何做到这一点;它只是我修补,我才开始构建实际的应用程序:D
//imports random number function import Foundation //creates data storage for dice roll var dice1: UInt32 = 0 var dice2: UInt32 = 0 //counter variable var i = 0 //how many times snake eyes happens var snakeeyes = 0 //how many times a double is rolled var `double` = 0 //rolls dice 100 times while i < 100{ //from here //sets dice roll
这返回一个错误’Range $ T3’不能转换为UInt32
06002
//checks for snake eyes if dice1 == 1 && dice2 == 1 { snakeeyes = snakeeyes + 1 } //checks for doubles if dice1 == dice2{ `double` = `double` + 1 } //increases counter i = i + 1 //to here } println("You got Snake Eyes \(snakeeyes) times.") println("You got Doubles,\(`double`) times.")