檢閱「do」與「while」挑戰活動的解決方案
下列範例應該使用 do,因為您知道至少需要執行程式碼區塊一次。 您也可以使用 while 來達成相同的結果。 有些開發人員認為 while 的邏輯讓程式碼更容易閱讀。 如果是這種情況,則您可以選擇實作 while。 在此情況下,請注意,大部分的程式碼編譯器都會將反覆項目陳述式轉換成 do-while,以最佳化您的程式碼。
專案 1 程式碼
下列程式碼是先前單元中之挑戰專案 1 的其中一個可能解決方案。
程式碼會使用 do 陳述式是因為程式碼區塊至少必須執行一次。 您也可以使用 while 來達成相同的結果。 有些開發人員可能會認為 while 的邏輯讓程式碼更容易閱讀。 如果是這種情況,您可以選擇在這裡實作 while 陳述式。
string? readResult;
string valueEntered = "";
int numValue = 0;
bool validNumber = false;
Console.WriteLine("Enter an integer value between 5 and 10");
do
{
readResult = Console.ReadLine();
if (readResult != null)
{
valueEntered = readResult;
}
validNumber = int.TryParse(valueEntered, out numValue);
if (validNumber == true)
{
if (numValue <= 5 || numValue >= 10)
{
validNumber = false;
Console.WriteLine($"You entered {numValue}. Please enter a number between 5 and 10.");
}
}
else
{
Console.WriteLine("Sorry, you entered an invalid number, please try again");
}
} while (validNumber == false);
Console.WriteLine($"Your input value ({numValue}) has been accepted.");
readResult = Console.ReadLine();
專案 2 程式碼
下列程式碼是先前單元中之挑戰專案 2 的其中一個可能解決方案。
程式碼會使用 do 陳述式是因為程式碼區塊至少必須執行一次。 您也可以使用 while 來達成相同的結果。 有些開發人員可能會認為 while 的邏輯讓程式碼更容易閱讀。 如果是這種情況,您可以選擇在這裡實作 while 陳述式。
string? readResult;
string roleName = "";
bool validEntry = false;
do
{
Console.WriteLine("Enter your role name (Administrator, Manager, or User)");
readResult = Console.ReadLine();
if (readResult != null)
{
roleName = readResult.Trim();
}
if (roleName.ToLower() == "administrator" || roleName.ToLower() == "manager" || roleName.ToLower() == "user")
{
validEntry = true;
}
else
{
Console.Write($"The role name that you entered, \"{roleName}\" is not valid. ");
}
} while (validEntry == false);
Console.WriteLine($"Your input value ({roleName}) has been accepted.");
readResult = Console.ReadLine();
專案 3 程式碼
下列程式碼是先前單元中之挑戰專案 3 的其中一個可能解決方案。
程式碼會針對外部迴圈使用 for 陳述式,因為您無法修改指派至「foreach 反覆運算變數」的值。 您可以藉由在迴圈內宣告額外的字串變數來解決此問題,但接著您會在程式碼邏輯中 foreach 增加不必要的複雜度。 換句話說,使用反覆運算陳述式 foreach (string myString in myStrings),然後嘗試處理 myString 變數會產生編譯錯誤。
程式碼針對內部迴圈使用了 while 語句,因為根據資料字串的值,可能不會執行程式碼區塊 (當字串不包含句點時)。 在反覆運算區塊可能不需要執行的情況下,您不應該使用 do 陳述式。
string[] myStrings = new string[2] { "I like pizza. I like roast chicken. I like salad", "I like all three of the menu choices" };
int stringsCount = myStrings.Length;
string myString = "";
int periodLocation = 0;
for (int i = 0; i < stringsCount; i++)
{
myString = myStrings[i];
periodLocation = myString.IndexOf(".");
string mySentence;
// extract sentences from each string and display them one at a time
while (periodLocation != -1)
{
// first sentence is the string value to the left of the period location
mySentence = myString.Remove(periodLocation);
// the remainder of myString is the string value to the right of the location
myString = myString.Substring(periodLocation + 1);
// remove any leading white-space from myString
myString = myString.TrimStart();
// update the comma location and increment the counter
periodLocation = myString.IndexOf(".");
Console.WriteLine(mySentence);
}
mySentence = myString.Trim();
Console.WriteLine(mySentence);
}
如果成功,恭喜您! 如果您遇到問題,請花時間檢閱解決方案,並嘗試了解其運作方式。