gpt4 book ai didi

Swift - 找不到 'init' 和 'round' 的重载

转载 作者:行者123 更新时间:2023-11-30 10:15:29 25 4
gpt4 key购买 nike

我一直在尝试做一些事情来为我的一些三年级学生提出问题,但我不断遇到“无法找到接受所提供参数的‘init’的重载”和“找不到重载对于接受“(Int)”类型的参数列表的“round”。我对 Swift 完全陌生,在过去一个小时左右的时间里我一直在谷歌上搜索。

我知道这不是最优雅的东西,但任何帮助将不胜感激。

import UIKit

class Question {

var player: Player
var question: String
var answer: Int = 0
var gradeThreeAdditionBank:[String] = ["Hello","Goodbye"]
var gradeThreeSubtractionBank:[String] = ["Hello"]
var gradeThreeMultiplicationBank:[String] = ["Hello"]
var gradeThreeDivisionBank:[String] = ["Hello"]
var gradeThreeRoundingBank:[String] = ["Round the following to the nearest rnd.factors: ", "rnd.name needs around rnd.number rnd.item(s). About how many does s/he need? (Round to the nearest rnd.factors.)"]
var gradeThreeRoundingFactors:[String] = ["one", "ten", "hundred"]
var gradeThreeFractionBank:[String] = ["Hello"]
var gradeThreeMultiStepBank:[String] = ["Hello"]

var randomName:[String] = ["Brayden", "Justin", "Nelson", "Ryan", "Russell", "Albert", "Adam", "Yan Bo", "Jonathan", "Chris", "Michael", "Alan", "Nicholas", "Brian", "Jeremy", "Max", "William", "Victor", "Ethan", "Johnny", "Terry", "Liam", "Selina", "Althea", "Amber", "Morgan", "Emma", "Kelly", "Kaitlyn", "Ashley", "Patience", "Brianna", "Jessica", "Lilian", "Charlotte", "Tiffany", "Nancy", "Crystal", "Lily", "Winnie", "Sarah", "Doris"]

init(question:String, answer:Int, player:Player) {
self.question = question
self.answer = answer
self.player = player
}

func generateQuestion() -> (String, Int) {
var questionType:String = ""

if (player.getLevel() >= 1 && player.getLevel() < 5) {
//(question, answer) = generateGradeThreeRoundingBank()
}
else if (player.getLevel() == 5) {

}

return (question, answer)
}


func generateGradeThreeRoundingBank() -> (String, Int) {
// Chooses the question type
var tempType:Int = Int(arc4random_uniform(1))
// Generates the text for the selected question type
var temp:String = gradeThreeRoundingBank[tempType]
// Generates a random number to round
var tempNumb = Int(arc4random_uniform(990)) + 10
// Chooses a factor to round to
var roundTo:String = gradeThreeRoundingFactors[Int(arc4random_uniform(3))]
if (tempNumb < 100) {
roundTo = gradeThreeRoundingFactors[Int(arc4random_uniform(2))]
}
else if (tempNumb < 10) {
roundTo = "one"
}
else {
roundTo = gradeThreeRoundingFactors[Int(arc4random_uniform(3))]
}
// Saves an Int version of roundTo
var roundToNumber:Int = 0
if (roundTo == "one") {
roundToNumber = 1
} else if (roundTo == "ten") {
roundToNumber = 10
} else {
roundToNumber = 100
}
// Generates an answer based on retrieved information
var tempAnswer = 0
if (roundToNumber == 1) {
tempAnswer = Int(round(tempNumb))
} else if (roundToNumber == 10) {
tempAnswer = Int(round(tempNumb)/10) * 10
} else {
tempAnswer = Int(round(tempNumb)/100) * 100
}
// Generates a question using the bank and replacing values with retrieved values
if (tempType == 0) {
temp.stringByReplacingOccurrencesOfString("rnd.factor", withString: roundTo)
temp.append(round(tempNumb))
}
else if (tempType == 1) {
temp.stringByReplacingOccurrencesOfString("rnd.factor", withString: roundTo)
}
return (temp, tempAnswer)
}

}

最佳答案

Swift对其数据类型很挑剔。在这里,您创建了 tempNumb 类型为 Int:

var tempNumb = Int(arc4random_uniform(990)) + 10

稍后,当您调用时:

    tempAnswer = Int(round(tempNumb))

您收到错误消息,告诉您 round() 不接受 Int,因为它不接受。 round() 接受一个 DoubleFloat

所以也许您想从 Double 开始:

var tempNumb = Double(arc4random_uniform(990) + 10)
<小时/>

有用的提示:按住选项键并单击任何变量以查找其类型。

关于Swift - 找不到 'init' 和 'round' 的重载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30274648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com