简体   繁体   English

函数的未解析标识符的使用 // Swift

[英]Use of Unresolved Identifier for Function // Swift

UPDATE BELOW下面更新

I am currently in an introductory Swift course at my college and am struggling with functions.我目前正在我的大学学习 Swift 入门课程,并且在函数方面苦苦挣扎。 I thought I followed the instructions clearly, but I am getting a certain error about "use of an unresolved identifier."我以为我清楚地遵循了说明,但我收到了关于“使用未解析的标识符”的某些错误。

This is the full error:这是完整的错误:

error: My Functions Playground 2 2.playground:23:8: error: use of unresolved identifier 'newGPA' switch newGPA {错误:My Functions Playground 2 2.playground:23:8: 错误:使用未解析的标识符'newGPA' switch newGPA {

Here is my code (the original instructions are below):这是我的代码(原始说明如下):

var gpa: Int
var attemptedHours: Int
var earnedGradePoints: Int

// create your function here
func gpaUpdater(hours moreHours: Int, grade moreGPA: Int) {
    let _: Int = attemptedHours + moreHours
    let newGPA: Int = gpa + moreGPA
    print(newGPA)
}

// call the function
gpaUpdater(hours: 16, grade: 60)

// add the new hours and grade points here and call the function again
switch newGPA {
case 0...1.8:
    print("You will be placed on suspension")
case 1.8...2.0:
    print("You will be placed on probation")
case 3.5...3.8:
    print("You will be put on the dean's list.")
case 3.9:
    print("You will be put on the president's list.")
default:
    print("Carry on. Nothing to see here.")
}

Instructions:指示:

We're going to track your GPA from one semester to the next.我们将跟踪您从一个学期到下一个学期的 GPA。 Assume at the end of your sophomore years, you have attempted 60 hours and have earned 222.5 grade points.假设在你大二结束时,你已经尝试了 60 个小时并获得了 222.5 的成绩点。 Assign attempted hours and grade points to variables.将尝试的学时和成绩点分配给变量。 Write a function that updates your current GPA and assigns it to the GPA var (you'll update it along the way).编写一个函数来更新您当前的 GPA 并将其分配给 GPA 变量(您将在此过程中对其进行更新)。 Label your function arguments.标记您的函数参数。 Print your new GPA from within the function.从函数内打印您的新 GPA。

At the end of the current semester, add 16 hours and 60 grade points to your record.在本学期结束时,将 16 小时和 60 个成绩点添加到您的记录中。 Call the gpa function to update your overall gpa.调用 gpa 函数来更新您的整体 gpa。

Test your gpa at the end of the year to see if any administrative action needs to be taken.在年底测试你的gpa,看看是否需要采取任何行政措施。 If the gpa is less than 1.8, the student will need to be placed on suspension.如果 gpa 低于 1.8,学生将需要被停学。 If less than 2.0, we need to put the student on probation.如果低于2.0,我们需要让学生留校察看。 If over 3.5, we'll put the student on the dean's list, and if over 3.9, we'll put the student on the president's list.如果超过 3.5,我们会把学生放在院长的名单上,如果超过 3.9,我们会把学生放在校长的名单上。 Create a switch that prints the recommended adminstrative action.创建一个打印推荐管理操作的开关。 If no action is required, print, "Carry on. Nothing to see here."如果不需要任何操作,请打印“继续。这里没什么可看的。” Create internal and external labels for your arguments.为您的参数创建内部和外部标签。

Thank You for your help!感谢您的帮助!

UPDATE更新

The function part of my Swift code is now correct, thank you all for the help.我的 Swift 代码的功能部分现在是正确的,谢谢大家的帮助。 Now I am trying to fix my switch statement.现在我正在尝试修复我的 switch 语句。 Here is my code:这是我的代码:

// add the new hours and grade points here and call the function again
switch gpa {
case gpa > 1.8:
    print("You will be placed on suspension")
case 1.8...2.0:
    print("You will be placed on probation")
case 3.5...3.8:
    print("You will be put on the dean's list.")
case gpa > 3.9:
    print("You will be put on the president's list.")
default:
    print("Carry on. Nothing to see here.")
}

The problem, I think, is that my teacher wants GPA to be an int, but if I want to use values like 1.9 for the gpa, then it needs to be a double.我认为,问题是我的老师希望 GPA 是一个整数,但是如果我想对 gpa 使用 1.9 之类的值,那么它需要是一个双精度值。 Here is an error that I am getting:这是我收到的错误:

error: My Functions Playground 2 2.playground:26:10: error: binary operator '>' cannot be applied to operands of type 'Int' and 'Double' case gpa > 1.8

Scope.范围。 Scope.范围。 Scope.范围。

newGPA is declared locally in the scope of gpaUpdater . newGPAgpaUpdater的范围内本地声明。 It's not visible on the top level.它在顶层不可见。

You could do你可以做

// create your function here
func gpaUpdater(hours moreHours: Int, grade moreGPA: Int) -> Int {
    // let _: Int = attemptedHours + moreHours
    return gpa + moreGPA
}

// call the function
let newGPA = gpaUpdater(hours: 16, grade: 60)

// add the new hours and grade points here and call the function again
switch newGPA { ...

No comment about the (unused) first parameter of gpaUpdater and the floating point cases switching over an Int 😉没有评论gpaUpdater的(未使用的)第一个参数和切换Int的浮点情况😉

I'm going to answer this from an assignment perspective;我将从作业的角度回答这个问题; the other answers regarding returning the local variable value are correct for accessing your newGPA variable.关于返回局部变量值的其他答案对于访问您的 newGPA 变量是正确的。

You missed the point in the assignment by creating the "newGPA" variable.您通过创建“newGPA”变量错过了分配中的要点。 The assignment states to "update" the global gpa variable with the new value from within the function.赋值声明使用函数内的新值“更新”全局 gpa 变量。

If this is introductory coding, you may not have come across the concept of recursion.如果这是介绍性编码,您可能没有遇到递归的概念。 This is basically assigning some value by including itself in the calculation.这基本上是通过将自身包含在计算中来分配一些值。

Instead of代替

let newGPA: Int = gpa + moreGPA print(newGPA)

think思考

gpa = gpa + moreGPA
print(gpa)

which can also be written as也可以写成

gpa += moreGPA

and then use gpa in your switch function.然后在您的开关功能中使用 gpa 。

What this does is updates your global gpa variable to a new value (by adding the moreGPA to it).这样做是将您的全局 gpa 变量更新为新值(通过向其添加 moreGPA)。 This is one of the main strengths of a global variable.这是全局变量的主要优势之一。 It can be accessed and modified from anywhere in your program.可以从程序中的任何位置访问和修改它。

That's my understanding based on the assignment instructions.这是我根据作业说明的理解。 That said, returning a value from a function is cleaner (in my opinion) since global variables can become conflicts in more complicated programs.也就是说,从函数返回一个值更清晰(在我看来),因为全局变量在更复杂的程序中可能会成为冲突。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM