我目前正在为学校的Swift课程完成一项任务,该课程需要一系列课程成绩并通过循环发送。我有两个循环运行,我认为我正确地阅读了说明,但我收到了两个错误:
62:11:二元运算符'> ='不能应用于类型' Int?'的操作数。和' Int'
54:27:二元运算符'<'不能应用于类型' [String:Int]'的操作数。和' Int'
我不知道为什么我会收到这些错误。
我的代码:
//: Playground - noun: a place where people can play
import UIKit
// Create an array that contains a list of courses that you are taking this semester. Print the array.
var listOfCourses: [String] = ["Coding Topics", "Intro to Video Production", "New Testament", "Spiritual Formation in Ministry", "Systematic Theology"]
print(listOfCourses)
// Drop this coding class from the array by targeting its index position and assign the dropped class to a variable. Remember that index counting starts at zero. Using the variable you just created, print a statement that says, "You have dropped __" with the name of the dropped class.
var droppedCoding = listOfCourses.remove(at: 0)
print("You just dropped your \(droppedCoding) class.")
// Add the dropped class back to the end of the array by using the variable you just created.
listOfCourses.append(droppedCoding)
print(listOfCourses)
// Using the count method on the array, print a statment that says, "I am taking __ courses this semester."
listOfCourses.count
print("I'm taking \(listOfCourses.count) classes this semester.")
// Create an array of courses that you took last semester.
var classesITook: [String] = ["Art History Survey 1", "Old Testament", "Hermeneutics", "Family & Childrens Ministry", "Computer Information Systems"]
print(classesITook)
// Create an array that combines the courses from your last two semesters (an array of arrays). Print the new combined array.
var combinedClassSchedule = listOfCourses + classesITook
print(combinedClassSchedule)
// Create a dictionary of assignments and grades from any of your current courses that contains at least four items. Though there's a way to set up a dictionary so you can leave values blank (by using optionals, which we'll get to in a future class), for now just enter assignments for which you've received a grade. Each of your assignments will need to have a unique name, so you may need to add the course code to the beginning of some, e.g. ABC101 Midterm, ABC102 Midterm.
var vidProductionGrades = ["COM251 QUIZ 1": 65, "COM251 Partner Interview": 96, "COM251 Editing Tutorial": 100, "COM251 QUIZ 2": 90, "COM251 Final Exam": 100]
// Update the grade for the second assignment in your list and add five points to it. Assign the previous grade to a variable and print, "Your old grade was __, and your new grade is __." Your print statement should use variables rather than pluging the numbers in directly.
let oldGradeValue = vidProductionGrades["COM251 Partner Interview"] = 96 // <- I can't figure out how to have the old value and the updated new value.
if let newGradeValue = vidProductionGrades.updateValue(101, forKey: "COM251 Partner Interview") {
print("Your old grade was \(oldGradeValue) and your new grade is \(newGradeValue).")
}
// Using a for loop on the first array of courses you created, print a statement for each of your courses that says, "I am taking __ this semester."
for course in listOfCourses {
print("I am taking \(course) this semester.")
}
// Using a while loop, print the first three graded assignments with their grades. "For __, I earned __"
while vidProductionGrades < 3 { //<-- How to do I loop through the keys and values?
print("For \(vidProductionGrades), I earned \(vidProductionGrades).")
}
// Modify your assignment grades dictionary so that you have at least one grade lower than 90 and at least one higher. Now create a loop with the control flow method that will only print out the first grade it finds that is higher or equal to 90. You can use the same wording as the previous loop. This will be the grade you tell your parents about.
let grade1 = vidProductionGrades.updateValue(95, forKey: "COM251 Final Exam")
let grade2 = vidProductionGrades.updateValue(89, forKey: "COM251 Partner Interview")
if grade1 >= 90 {
print(grade1)
}
答案 0 :(得分:0)
在将元素分配到字典中的意义上,以下行不正确:
let oldGradeValue = vidProductionGrades["COM251 Partner Interview"] = 96
您要做的是首先查看字典中是否有一个带有 COM251 Parter Interview 键的元素,然后将其分配给变量。
let oldGradeValue:Int? = vidProductionGrades["COM251 Partner Interview"]
请注意类型为Int?
,因为可能存在具有该键的元素。
要分配新成绩,请执行以下操作:
vidProductionGrades["COM251 Partner Interview"] = 96
这会更新该密钥的现有成绩,或者如果它是新的,它将被添加到字典中。
以下代码会产生与上述操作相关的错误。
let grade1 = vidProductionGrades.updateValue(95, forKey: "COM251 Final Exam")
let grade2 = vidProductionGrades.updateValue(89, forKey: "COM251 Partner Interview")
if grade1 >= 90 {
print(grade1)
}
如果添加了新元素,方法updateValue
将返回已替换的值或nil
。如果密钥 COM251 Parter Interview 最初不存在于字典中,则会导致比较错误grade1 >= 90
。这意味着grade1
的类型为Int?
。这可以通过以下方式解决:
if let previousGrade = grade1 {
if previousGrade > 90 {
print(previousGrade)
}
}
如果grade1
不是nil
,那么只有当它大于90时,它才会显示上一个等级。