对于我的项目的一部分,我希望强制执行用户输入只能在最小/最大字边界内的规则。最少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