對於我的項目的一部分,我希望強制執行用戶輸入只能在最小/最大字邊界內的規則。最少1個字,最多50個字。布爾值不會從默認設置值false更改。這裏是我的代碼:計算字符串以確保輸入處於最小/最大邊界內
bool WordCount_Bool = false;
//Goto the method that handles the calculation of whether the users input is within the boundary.
WordCount_EH(WordCount_Bool);
//Decide whether to continue with the program depending on the users input.
if (WordCount_Bool == true)
{
/*TEMP*/MessageBox.Show("Valid input");/*TEMP*/
/*Split(split_text);*/
}
else
{
MessageBox.Show("Please keep witin the Min-Max word count margin.", "Error - Outside Word Limit boundary");
}
方法處理陣列和布爾的變化:
private bool WordCount_EH(bool WordCount_Bool)
{
string[] TEMPWordCount_Array = input_box.Text.Split(' ');
int j = 0;
int wordcount = 0;
for (int i = 100; i <=0; i--)
{
if (string.IsNullOrEmpty(TEMPWordCount_Array[j]))
{
//do nothing
}
else
{
wordcount++;
}
j++;
}
if (wordcount >= 1)
{
WordCount_Bool = true;
}
if (wordcount < 1)
{
WordCount_Bool = false;
}
return WordCount_Bool;
}
謝謝大家提前。
附註:我意識到,for循環將拋出一個異常,或者至少不是最適合其目的,因此任何意見將非常感激。
額外附註:對不起,我應該說,我沒有使用長度的原因是,只要有可能,我應該做我自己的代碼,而不是使用內置函數。
注:當你的問題是標籤的'視覺studio'家庭,才應使用*約*的Visual Studio。 「如果您有關於Visual Studio特性和功能的特定問題,請使用此標記,而不僅僅是關於您的代碼的問題。」 – Amy
如果要在「WordCount_EH」中更改它,則需要通過'ref'傳遞'WordCount_Bool'這種情況下,你可能只是使用返回值。 – Lee
我可能會錯過一些東西,但爲什麼不使用TEMPWordCount_Array的長度 – Dale