【发布时间】:2018-02-23 18:17:33
【问题描述】:
我目前正在为我在学校的 Swift 班级做一个作业,该作业需要一系列班级成绩并通过循环发送它们。我有两个循环正在运行,并且我认为我正确阅读了说明,但出现了两个错误:
62:11:二元运算符“>=”不能应用于“Int?”类型的操作数和'Int'
54:27:二元运算符“
我不知道为什么会出现这些错误。
我的代码:
//: 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)
}
【问题讨论】:
-
错误说在第一次比较中,操作数的类型不同:
Int?和Int。如果您确定第一个操作数,您可以强制解包:(Int?)! >= Int。第二个错误看起来是将字典中的元素与Int进行比较。您是否尝试从字符串键中获取整数值然后进行比较?
标签: swift