練習 - 從方法傳回陣列

资讯 2024-07-15 阅读:19 评论:0
美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址
已完成

在開發應用程式時,您通常需要建置和修改資料集。 方法對執行資料作業很有用,而且其自行建置資料集的功能特別強大。 開發方法來建立代表資料集的陣列,有助於讓您的程式碼保持可重複使用、有條理及簡化。 在此練習中,您將練習從方法傳回陣列。

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!

  1. 在 [Visual Studio Code 編輯器] 中,刪除先前練習中任何現有的程式碼。

    Deletes any existing code from previous exercises in [Visual Studio Code Editor].

  2. 在 [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.

  3. 有許多方法可以解決此問題。 請花點時間考慮如何在陣列中搜尋兩個加總起來等於指定值的數字。

    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:

    1. 從陣列中選擇一個數字
    2. 每次檢查一個其他的數字,看看其總和是否等於目標值
    3. 找到相符項目後立即傳回結果
  4. 若要檢查陣列中的每個數字,請使用下列程式碼更新 方法:

    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.

  5. 接下來,新增邏輯來檢查兩個硬幣總和是否等於目標值。 如果要這樣做,請使用下列程式碼更新先前的 迴圈:

    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.

  1. 在 方法簽章上方建立新的空白程式碼。 然後輸入下列程式碼:

    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.

  2. 輸入新的空白程式碼。 然後輸入下列程式碼:

    Enter a new blank code. Then enter the following code:

    
    
  3. 如有必要,請開啟 Visual Studio Code 的 [整合式終端] 面板。

    Open the [integrated end] panel of the Visual Studio Code, if necessary.

  4. 在終端命令提示字元中,輸入 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!

  1. 藉由將程式碼更新為下列項目,將方法簽章中的傳回型別從 變更為 :

    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.

  2. 以下列程式碼取代 方法:

    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.

  3. 以下列程式碼取代 方法:

    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.

  4. 請以下列程式碼更新 方法中的邏輯:

    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.

  5. 瀏覽至 方法中的 陳述式。 修改 陳述式以符合下列程式碼:

    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.

  6. 此時, 方法應該符合下列程式碼:

    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.

  1. 瀏覽至已定義 變數的程式開頭。 修改您的程式碼,如下所示:

    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.

  2. 瀏覽至 呼叫。 更新您的程式碼以符合下列項目:

    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.

  1. 將您的程式碼與下列內容進行比較,確保其正確:

    Compare your code with the following to make sure that it is correct:

    
    
  2. 使用 Ctrl 鍵 + S 或使用 [Visual Studio Code 檔案] 功能表儲存您的工作。

    Saves your work using Ctrl + S or the [Visual Studio Code file] menu.

  3. 如有必要,請開啟 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].

  4. 在終端命令提示字元中輸入 dotnet run

    Enter dotnet run in terminal command hint character

  5. 請驗證您的程式碼會產生下列輸出:

    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.

  6. 接下來,將 的值更新為 的值:

    Next, update the value to the value:

    
    
  7. 儲存您的工作,然後在終端命令提示字元中輸入 dotnet run

    Save your work and then enter dotnet run in the end command hint character

  8. 若要確認您的程式碼如預期般運作,請比較應用程式的輸出與下列輸出:

    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.

美化布局示例

欧易(OKX)最新版本

【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   全球官网 大陆官网

币安(Binance)最新版本

币安交易所app【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址

火币HTX最新版本

火币老牌交易所【遇到注册下载问题请加文章最下面的客服微信】永久享受返佣20%手续费!

APP下载   官网地址
文字格式和图片示例

注册有任何问题请添加 微信:MVIP619 拉你进入群

弹窗与图片大小一致 文章转载注明

分享:

扫一扫在手机阅读、分享本文

发表评论
平台列表
美化布局示例

欧易(OKX)

  全球官网 大陆官网

币安(Binance)

  官网

火币(HTX)

  官网

Gate.io

  官网

Bitget

  官网

deepcoin

  官网
热门文章
  • 0.00003374个比特币等于多少人民币/美金

    0.00003374个比特币等于多少人民币/美金
    0.00003374比特币等于多少人民币?根据比特币对人民币的最新汇率,0.00003374比特币等于2.2826 1222美元/16.5261124728人民币。比特币(BTC)美元(USDT)人民币(CNY)0.00003374克洛克-0/22216.5261124728比特币对人民币的最新汇率为:489807.72 CNY(1比特币=489807.72人民币)(1美元=7.24人民币)(0.00003374USDT=0.0002442776 CNY)。汇率更新于2024...
  • 0.00006694个比特币等于多少人民币/美金

    0.00006694个比特币等于多少人民币/美金
    0.00006694比特币等于多少人民币?根据比特币对人民币的最新汇率,0.00006694比特币等于4.53424784美元/32.5436 16人民币。比特币(BTC)美元(USDT)人民币(CNY)0.000066944.53424784【比特币密码】32.82795436 16比特币对人民币的最新汇率为:490408.64 CNY(1比特币=490408.64人民币)(1美元=7.24人民币)(0.00006694USDT=0.0004846456 CNY)汇率更新时...
  • 0.00015693个比特币等于多少人民币/美金

    0.00015693个比特币等于多少人民币/美金
    0.000 15693比特币等于多少人民币?根据比特币对人民币的最新汇率,0.000 15693比特币等于10.6 1678529美元/76.86554996人民币。比特币(BTC)【比特币价格翻倍】美元(USDT)人民币(CNY)0.000/克洛克-0/5693【数字货币矿机】10.6 167852976.8655254996比特币对人民币的最新汇率为:489,807.72 CNY(1比特币= 489,807.72人民币)(1美元=7.24人民币)(0.00015693 U...
  • 币圈院士:5.20比特币(BTC)以太坊(ETH)行情分析

    币圈院士:5.20比特币(BTC)以太坊(ETH)行情分析
    利空出尽?华尔街多头坚信美股将摆脱泥潭 经济衰退风险被夸大A lot of people on Wall Street believe that beauty will escape the quagmire; the risk of recession is exaggerated. 从目前美国经济的情况加上美股先有的走势来判断,确信通胀已经或即将见顶,这为价格压力回落铺平了道路,这最终将使美联储得以放缓...
  • 2000年美国GDP占世界的304%,中国GDP仅占35%,现在呢?

    2000年美国GDP占世界的304%,中国GDP仅占35%,现在呢?
    GDP作为全球公认的实力基准,就像是一个大国实力的代言人,它是布雷顿森林体系下全球团结的声音。它不仅仅是数字的累积,更是大国综合实力的人格化,默默诉说着每个国家的辉煌与荣耀。虽然GDP不是衡量一个国家综合实力的唯一标准,但无疑是最关键的指标之一。作为一面镜子,它反映了国家的经济实力和发展水平,是国家综合实力的重要体现,不容忽视。2000年,中国GDP迈过/克洛克-0/万亿美元的重要门槛,达到/克洛克-0/。2/克洛克-0/万亿美元(折合人民币7。7万亿元)。然而,在全球经济的...
标签列表