在開發應用程式時,您通常需要建置和修改資料集。 方法對執行資料作業很有用,而且其自行建置資料集的功能特別強大。 開發方法來建立代表資料集的陣列,有助於讓您的程式碼保持可重複使用、有條理及簡化。 在此練習中,您將練習從方法傳回陣列。
In developing applications, you usually need to construct and modify data sets. Methods are useful for executing data, and their own creation functions are particularly powerful. Development methods for creating arrays of representative data sets help to keep your code reusable, structured and simplified. In this exercise, you will practice from the method back to the array.
假設您有數個不同幣值的硬幣。 您的工作是找到兩個加總起來等於目標值的硬幣。 在此練習中,可用的硬幣會以整數陣列表示。 您必須在新陣列中傳回兩個硬幣的索引。 現在就開始吧!
Assuming that you have several coins of different value, your job is to find two coins that add up to the target value. In this exercise, the available coins are expressed in an integer array. You must return an index of two coins in the new array. Now!
在 [Visual Studio Code 編輯器] 中,刪除先前練習中任何現有的程式碼。
Deletes any existing code from previous exercises in [Visual Studio Code Editor].
在 [Visual Studio Code 編輯器] 中輸入下列程式碼:
Enter the following code in [Visual Studio Code Editor]:
在找不到兩個硬幣的情況下,您的方法會傳回空陣列。 請花點時間考慮傳回結果的語法。 雖然您可以建立變數以儲存新的 陣列並傳回變數,但 陳述式可讓您同時建立和傳回值。
If two coins cannot be found, your method returns to the empty array. Take some time to consider the syntax for returning the results. While you can create a variable to save the new array and return the variable, the statement allows you to create and return the value at the same time.
有許多方法可以解決此問題。 請花點時間考慮如何在陣列中搜尋兩個加總起來等於指定值的數字。
There are many ways to solve this problem. Please take some time to consider how to search in the array for two numbers that add up to the specified value.
在此練習中,您將會使用下列方法:
In this exercise, you will use the following methods:
- 從陣列中選擇一個數字
- 每次檢查一個其他的數字,看看其總和是否等於目標值
- 找到相符項目後立即傳回結果
若要檢查陣列中的每個數字,請使用下列程式碼更新 方法:
To check each number in the array, use the following code to update it:
在這裡, 代表一個固定的硬幣索引,而 代表後續的硬幣索引。 您嘗試將每個 硬幣與固定的 硬幣加總起來,看看它們是否等於目標值。
Here, it represents a fixed coin index and a subsequent coin index. You try to add each coin together with a fixed coin to see if it equals the target value.
接下來,新增邏輯來檢查兩個硬幣總和是否等於目標值。 如果要這樣做,請使用下列程式碼更新先前的 迴圈:
Next, add a logic to check whether the sum of two coins is equal to the target value. If you want to do this, use the following code to update the previous loop:
在此程式碼中,您會檢查陣列中 和 的幣值總和是否等於目標值。 如果總和相等,您會建立一個用來儲存這些索引的陣列,並將其傳回。 如果它們不相等,您可以忽略它們並繼續檢查。
In this code, you check whether the sum of the currency values in the array is equal to the target value. If the sum is equal, you create an array to save the indexes and return them. If they are not equal, you can ignore them and continue to check them.
在此步驟中,您將測試程式碼,以確保程式碼正確執行。 首先,將一些變數初始化以儲存輸入資料,然後呼叫方法並列印結果。
In this step, you will test the code to ensure that the code is correctly executed. First, you will initialize some variables to save the input data and then call the method to print the results.
在 方法簽章上方建立新的空白程式碼。 然後輸入下列程式碼:
Create a new blank code above the method signature. Enter the following code:
請記住,如果找不到任何變更, 方法會傳回空陣列。 您必須先檢查陣列大小,然後再嘗試列印 陣列。
Remember that if no changes are found, the method returns the empty array. You must check the array size and then try printing the array.
輸入新的空白程式碼。 然後輸入下列程式碼:
Enter a new blank code. Then enter the following code:
如有必要,請開啟 Visual Studio Code 的 [整合式終端] 面板。
Open the [integrated end] panel of the Visual Studio Code, if necessary.
在終端命令提示字元中,輸入 dotnet run。 將您的輸出與下列項目進行比較:
Enter dotnet run in the end command hint character. Compare your output with the following items:
如果您的程式碼顯示非預期的結果,您必須檢閱程式碼以尋找錯誤並進行更新。 再次執行程式碼,查看您是否已修正此問題。 請繼續更新和執行程式碼,直到其產生預期的結果為止。
If your code shows an unexpected result, you must check the code to find the error and update it. Once again, run the code and see if you have corrected the problem. Please continue updating and running the code until it produces the expected result.
在此步驟中,您將擴充 方法,找出加總起來等於目標值的更多組硬幣。 在此練習中,您最多會找到五組。 這表示傳回型別將會是 2D 陣列,而不是 1D 陣列,而且您必須修改程式碼傳回結果的方式。 現在就開始吧!
In this step, you will expand the method to find more coins that add up to the target value. In this exercise, you will find up to five groups. This means that the return type will be a 2D array, not a 1D array, and you will have to change the code to return the results. Now!
藉由將程式碼更新為下列項目,將方法簽章中的傳回型別從 變更為 :
By updating the code to the following items, the return type in the method signature will be changed from:
接下來,您將建立用來儲存並傳回結果的 陣列,以及用來追蹤新增至陣列之組數的計數器變數。
Next, you will create arrays to save and return the results, as well as array variables to track those that have been added to the array.
以下列程式碼取代 方法:
Replace method with the following code:
請注意,您已將結果陣列元素初始化為 。 這會在您稍後要列印找到的硬幣組時協助您。
Please note that you have initialized the results array elements. This will help you when you print the coin group you will find later.
接下來,您將使用 陣列來儲存找到的每組項目,而不是傳回最初相符項目。
Next, you will use the array to save each set of items found instead of returning the original matching items.
以下列程式碼取代 方法:
Replace method with the following code:
請注意,每次將一組項目加入陣列時, 都會遞增。 如果找到超過五組,這可能會導致索引超出範圍的錯誤。 如果要避免此錯誤,您可以新增程式碼來檢查 的值,並傳回 陣列的填入結果。
Please note that each time a group of items is added to the array, it increases. If more than five sets are found, this may lead to an error in the index beyond the range. If this error is avoided, you can add a new code to check the value and return the entered results of the array.
請以下列程式碼更新 方法中的邏輯:
Please update the logic in the method with the following code:
最後,如果完全找不到相符項目,或找到少於五個相符項目,您必須更新最後的 return 陳述式以傳回正確的結果。
Finally, if you find no matches at all, or if you find fewer than five matches, you must update the last return statement to return the correct result.
瀏覽至 方法中的 陳述式。 修改 陳述式以符合下列程式碼:
Browse the statement in the method. Modify the statement to conform to the following code:
您也可以使用三元運算子來縮短此傳回碼,如下所示:
You can also use the Trinity Calculator to shorten this return code, as follows:
重要
Important
關鍵字的彈性可讓您傳回運算式的值,只要結果符合方法的指定傳回型別即可。
The punctuation of key words allows you to return the value of the mode of operation, provided that the result corresponds to the method specified return type.
此時, 方法應該符合下列程式碼:
At this point, the method should match the following code:
現在您的方法正在傳回 2D 陣列,您可以更新程式碼來擷取並列印結果。 由於結果陣列元素已初始化為 ,因此您可以新增檢查以列印所有硬幣組,直到找到 值為止。
Now your method is being returned to the 2D array, which allows you to update the code to retrieve the print results. Since the result array elements have been initialized, you can add a check to print all coin groups until the value is found.
瀏覽至已定義 變數的程式開頭。 修改您的程式碼,如下所示:
Browse to the beginning of a program that is defined as a variable. Modify your code as follows:
接下來,您將更新對 的呼叫,以正確列印 值。
Next, you will update the call to the correct print value.
瀏覽至 呼叫。 更新您的程式碼以符合下列項目:
Browse to the Call. Update your code to match the following items:
在這裡,您可以依原樣保留空白陣列的檢查,並在 for-loop 中列印 2D 陣列的值。 當找到 值時,您會中斷迴圈,因為沒有下一組。
Here you can keep the check of the blank array and print the value of the 2D array in for-loop. When the value is found, you break the loop because there is no next set.
在這項工作中,您將從整合式終端執行應用程式,並確認程式碼正常運作。 這就開始吧。
In this task, you will run the application from the integration end and verify that the code is working properly.
將您的程式碼與下列內容進行比較,確保其正確:
Compare your code with the following to make sure that it is correct:
使用 Ctrl 鍵 + S 或使用 [Visual Studio Code 檔案] 功能表儲存您的工作。
Saves your work using Ctrl + S or the [Visual Studio Code file] menu.
如有必要,請開啟 Visual Studio Code 的 [整合式終端] 面板。
Open the [integrated end] panel of the Visual Studio Code, if necessary.
在 [總管] 面板中,若要在 TestProject 資料夾位置開啟 [終端],請以滑鼠右鍵按一下 [TestProject],然後選取 [在整合式終端中開啟]。
In the [master] panel, if you want to open [end of] the TestProject folder position, press [TestProject] on the right mouse button and then select [open in integration end].
在終端命令提示字元中輸入 dotnet run
Enter dotnet run in terminal command hint character
請驗證您的程式碼會產生下列輸出:
Verify that your code generates the following outputs:
如果您的程式碼顯示不同的結果,您必須檢閱程式碼以尋找錯誤並進行更新。 再次執行程式碼,查看您是否已修正此問題。 請繼續更新和執行程式碼,直到其產生預期的結果為止。
If your code shows different results, you must search the code to find errors and update them. Once again, run the code to see if you have corrected the problem. Please continue updating and running the code until it produces the expected results.
接下來,將 的值更新為 的值:
Next, update the value to the value:
儲存您的工作,然後在終端命令提示字元中輸入 dotnet run
Save your work and then enter dotnet run in the end command hint character
若要確認您的程式碼如預期般運作,請比較應用程式的輸出與下列輸出:
To confirm that your code works as intended, compare the output of the application to the following output:
如果您的程式碼顯示不同的結果,您必須檢閱程式碼以尋找錯誤並進行更新。 再次執行程式碼,查看您是否已修正此問題。 請繼續更新和執行程式碼,直到其產生預期的結果為止。
If your code shows different results, you must search the code to find errors and update them. Once again, run the code to see if you have corrected the problem. Please continue updating and running the code until it produces the expected results.
注册有任何问题请添加 微信:MVIP619 拉你进入群
打开微信扫一扫
添加客服
进入交流群
发表评论