簡體   English   中英

函數的未解析標識符的使用 // Swift

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

下面更新

我目前正在我的大學學習 Swift 入門課程,並且在函數方面苦苦掙扎。 我以為我清楚地遵循了說明,但我收到了關於“使用未解析的標識符”的某些錯誤。

這是完整的錯誤:

錯誤:My Functions Playground 2 2.playground:23:8: 錯誤:使用未解析的標識符'newGPA' switch newGPA {

這是我的代碼(原始說明如下):

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.")
}

指示:

我們將跟蹤您從一個學期到下一個學期的 GPA。 假設在你大二結束時,你已經嘗試了 60 個小時並獲得了 222.5 的成績點。 將嘗試的學時和成績點分配給變量。 編寫一個函數來更新您當前的 GPA 並將其分配給 GPA 變量(您將在此過程中對其進行更新)。 標記您的函數參數。 從函數內打印您的新 GPA。

在本學期結束時,將 16 小時和 60 個成績點添加到您的記錄中。 調用 gpa 函數來更新您的整體 gpa。

在年底測試你的gpa,看看是否需要采取任何行政措施。 如果 gpa 低於 1.8,學生將需要被停學。 如果低於2.0,我們需要讓學生留校察看。 如果超過 3.5,我們會把學生放在院長的名單上,如果超過 3.9,我們會把學生放在校長的名單上。 創建一個打印推薦管理操作的開關。 如果不需要任何操作,請打印“繼續。這里沒什么可看的。” 為您的參數創建內部和外部標簽。

感謝您的幫助!

更新

我的 Swift 代碼的功能部分現在是正確的,謝謝大家的幫助。 現在我正在嘗試修復我的 switch 語句。 這是我的代碼:

// 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.")
}

我認為,問題是我的老師希望 GPA 是一個整數,但是如果我想對 gpa 使用 1.9 之類的值,那么它需要是一個雙精度值。 這是我收到的錯誤:

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

范圍。 范圍。 范圍。

newGPAgpaUpdater的范圍內本地聲明。 它在頂層不可見。

你可以做

// 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 { ...

沒有評論gpaUpdater的(未使用的)第一個參數和切換Int的浮點情況😉

我將從作業的角度回答這個問題; 關於返回局部變量值的其他答案對於訪問您的 newGPA 變量是正確的。

您通過創建“newGPA”變量錯過了分配中的要點。 賦值聲明使用函數內的新值“更新”全局 gpa 變量。

如果這是介紹性編碼,您可能沒有遇到遞歸的概念。 這基本上是通過將自身包含在計算中來分配一些值。

代替

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

思考

gpa = gpa + moreGPA
print(gpa)

也可以寫成

gpa += moreGPA

然后在您的開關功能中使用 gpa 。

這樣做是將您的全局 gpa 變量更新為新值(通過向其添加 moreGPA)。 這是全局變量的主要優勢之一。 可以從程序中的任何位置訪問和修改它。

這是我根據作業說明的理解。 也就是說,從函數返回一個值更清晰(在我看來),因為全局變量在更復雜的程序中可能會成為沖突。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM