Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么在字符串长度测试中循环两次_C#_Try Catch_Uppercase - Fatal编程技术网

C# 为什么在字符串长度测试中循环两次

C# 为什么在字符串长度测试中循环两次,c#,try-catch,uppercase,C#,Try Catch,Uppercase,我在C#(2013)Windows窗体中工作。我的指导老师希望我们在点击calculate按钮时确保txtStateInput是大写的。然而,当我输入一个两个字符的字符串,如“wi”,然后点击calculate,它抛出消息“entervalidstate”,并清除文本框。当我第二次输入“wi”时,它就会工作。我不明白为什么会发生这种情况,代码会让我相信它会检查以确保txtStateInput中的字符串是两个字符,然后当单击calculate时,它会将字符串大写。我不明白为什么它只在我第二次进入“

我在C#(2013)Windows窗体中工作。我的指导老师希望我们在点击calculate按钮时确保txtStateInput是大写的。然而,当我输入一个两个字符的字符串,如“wi”,然后点击calculate,它抛出消息“entervalidstate”,并清除文本框。当我第二次输入“wi”时,它就会工作。我不明白为什么会发生这种情况,代码会让我相信它会检查以确保txtStateInput中的字符串是两个字符,然后当单击calculate时,它会将字符串大写。我不明白为什么它只在我第二次进入“wi”状态时才起作用

       private void btnCalc_Click(object sender, EventArgs e)
        {
            //declare variables.
            int startPop = 0;
            int endPop = 0;
            string Message = "Error";
            decimal Percent = 0.0m;
            string State = "";
            string City = String.Empty;
            int dTimes = 0;

            try
            {
                City = txtCityInput.Text;

                //System Globalization was initialized  so this method works.
                TextInfo myTI = new CultureInfo("en-US", false).TextInfo;
                txtCityInput.Text = myTI.ToTitleCase(City);

                if(txtStateInput.Text.Length != 2)
                {
                    MessageBox.Show("Enter valid State");
                    txtStateInput.Focus();
                    txtStateInput.SelectAll();
                }

                else
                {
                    State = txtStateInput.Text.ToUpper();
                    txtStateInput.Text = State;

                    if ((int.TryParse(txtStartPopInput.Text, out startPop)) && int.TryParse(txtEndPopInput.Text, out endPop))
                    {
                        if ((startPop > 0) && (endPop > 0))
                        {
                            //if population has decreased.
                            if ((startPop > endPop))
                            {
                                Percent = ((decimal.Parse(endPop.ToString()) - decimal.Parse(startPop.ToString())) / decimal.Parse(startPop.ToString()));
                                Message = "Pop. Decrease of " + Percent.ToString("p");
                            }
                            //if population has increased.
                            if ((startPop < endPop))
                            {
                                Percent = ((decimal.Parse(endPop.ToString()) - decimal.Parse(startPop.ToString())) / decimal.Parse(startPop.ToString()));
                                Message = "Pop. Increase of " + Percent.ToString("p");
                            }
                            //if population has not changed.
                            if ((startPop == endPop))
                            {
                                Percent = ((decimal.Parse(endPop.ToString()) - decimal.Parse(startPop.ToString())) / decimal.Parse(startPop.ToString()));
                                Message = "No Change in Population";
                            }
                        }
                        else
                        {
                            MessageBox.Show("Please enter valid population figures.");
                        }
                    }
                if (int.TryParse(txtDisplayTimes.Text, out dTimes))
                {
                    if (dTimes > 0)
                    {
                        lstResults.Items.Clear();
                        int iSum = 0;
                        int iLoopCount = dTimes;

                        //displays the results according to value in txtDisplayTimes.
                        for (iSum = 1; iSum <= iLoopCount; iSum++)
                        {
                            lstResults.Items.Add(Message);
                        }
                    }
                }
                }
            }



            catch
            {
                MessageBox.Show("Something went wrong.");
            }

        }
private void btnCalc\u单击(对象发送者,事件参数e)
{
//声明变量。
int startPop=0;
int-endPop=0;
string Message=“Error”;
小数百分比=0.0m;
字符串状态=”;
string City=string.Empty;
int-dTimes=0;
尝试
{
City=txtCityInput.Text;
//系统全球化已初始化,因此此方法有效。
TextInfo myTI=新文化信息(“en-US”,false);
txtCityInput.Text=myTI.ToTitleCase(城市);
if(txtStateInput.Text.Length!=2)
{
MessageBox.Show(“输入有效状态”);
txtStateInput.Focus();
txtStateInput.SelectAll();
}
其他的
{
State=txtStateInput.Text.ToUpper();
txtStateInput.Text=状态;
if((int.TryParse(txtStartPopInput.Text,out-startPop))和&int.TryParse(txtEndPopInput.Text,out-endPop))
{
如果((起始点>0)和(&(结束点>0))
{
//如果人口减少了。
如果((开始点>结束点))
{
百分比=((decimal.Parse(endPop.ToString())-decimal.Parse(startPop.ToString())/decimal.Parse(startPop.ToString()));
Message=“Pop.减少”+总串百分比(“p”);
}
//如果人口增加了。
如果((起始点<结束点))
{
百分比=((decimal.Parse(endPop.ToString())-decimal.Parse(startPop.ToString())/decimal.Parse(startPop.ToString()));
Message=“Pop.增加”+百分比到字符串(“p”);
}
//如果人口没有变化。
如果((startPop==endPop))
{
百分比=((decimal.Parse(endPop.ToString())-decimal.Parse(startPop.ToString())/decimal.Parse(startPop.ToString()));
Message=“人口没有变化”;
}
}
其他的
{
MessageBox.Show(“请输入有效的人口数字”);
}
}
if(int.TryParse(txtDisplayTimes.Text,out dTimes))
{
如果(数据时间>0)
{
lstResults.Items.Clear();
int iSum=0;
int iLoopCount=数据时间;
//根据txtDisplayTimes中的值显示结果。

对于(iSum=1;iSum)如何逐行调试它,并找出它为什么第一次不起作用?还要确保在camelCase中编写局部变量。你应该保持通常的样式约定。你一次做了很多事情,以便能够清楚地看到是什么导致了这种奇怪的行为。从
if开始(txtStateInput.Text.Length!=2){MessageBox.Show(“输入有效状态”);}else{MessageBox.Show(“Great!”);}
。如果有效,请尝试将else块的内部更改为
txtStateInput.Text=txtStateInput.Text.ToUpper();
,然后查看它是否仍然有效。继续进行非常小的更改(最佳情况:每次一行)除非你一直让它做你想做的事情,或者你注意到奇怪的行为。如果你做了,可能是最后一次改变造成的。谢谢你的反馈,我在(C#简介)类和涉及txtStateInput的代码是讲师正在使用的。因此我想我应该尝试将其添加到我的代码中。我发现了问题所在,不幸的是,txtStateInput设置为在文本更改时清除txtStateInput。我不敢相信我忽略了这段信息。谢谢bit,chris579,还有感谢科拉克再次指出我不应该忽视的显而易见的事情。我刚刚开始学习c#,但这确实给了我一个很好的教训。再次感谢大家。