第二個解決方案
下列程式碼是先前單元中挑戰的其中一個可能解決方案:
int value1 = 12;
decimal value2 = 6.2m;
float value3 = 4.3f;
int result1 = Convert.ToInt32((decimal)value1 / value2);
Console.WriteLine($"Divide value1 by value2, display the result as an int: {result1}");
decimal result2 = value2 / (decimal)value3;
Console.WriteLine($"Divide value2 by value3, display the result as a decimal: {result2}");
float result3 = value3 / value1;
Console.WriteLine($"Divide value3 by value1, display the result as a float: {result3}");
這段程式碼只是一個可能的解決方案,因為有好幾種方法可以解決這項挑戰。 此解決方案大多依賴轉換 (以及呼叫轉換);不過,您也可以用其他同樣可行的方法。 只要確定您的結果符合下列輸出:
Divide value1 by value2, display the result as an int: 2
Divide value2 by value3, display the result as a decimal: 1.4418604651162790697674418605
Divide value3 by value1, display the result as a float: 0.3583333
如果成功,恭喜您! 請繼續進行下一個單元中的知識檢定。
重要
如果您無法順利完成此挑戰,則建議在繼續之前先檢閱先前的單元。 您需要先了解本課程模組內容,才能繼續深入我們要在其他課程模組中探討的所有新概念。