在C Sharp.net中的循环 [英] for loop in c sharp.net

查看:71
本文介绍了在C Sharp.net中的循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码.我希望所有数字都打印在文本框中,但只打印最后一个数字.


 私有  void  btnDeal_Click(对象发​​件人,EventArgs e)
        {
            
             int  []数字= { 9  8  7  6 };
             for ( int  i =  0 ; i !=数字.长度; i ++)
            {
                 int  num =数字[i];
                txtNorth.Text = num.ToString();
                i ++;
            }

        } 




如何将所有数字打印到文本框.

谢谢,

解决方案

好,让我们指出所有错误:

 私有  void  btnDeal_Click(对象发​​件人,EventArgs e)// 不是错误,而是违反命名约定
        {
             int  []数字= { 9  8  7  6 }; // 硬编码
            // 无需用于循环仪,foreach更简单,并且不需要i 
             for ( int  i =  0 ; i !=数字.长度; i ++)// 变量名错误,请使用<数字.长度代替
            {
                 int  num =数字[i];
                txtNorth.Text = num.ToString(); // 旧迭代中的数据丢失
                i ++; // 再次增加;不要操作循环变量!
            }
} 



固定:

 静态 只读  int  []数字= { 9  8  7  6 };
静态 字符串定界符= " "; // 从不使用立即常量,仅使用显式常量

//  .. 

btnDeal.Click + =(sender,eventArgs)= >  {//  System.Text.StringBuilder();
     foreach ( int 数字 in 数字){
        sb.Append(number.ToString());
        sb.Append(delimiter);
    }
    txtNorth.Text = sb.ToString().Trim(); // 修剪以删除最后一个定界符
} //  btnDeal.Click  



这是C#v.2的委托方法的一种变体,其中尚未引入lambda语法:

 btnDeal.Click + = 代理(对象发​​件人,System.EventArgs eventArgs ){// 需要明确声明的类型
    //  ...与上面相同
}
</  pre  >  



关于Microsoft命名约定的一些解释:是的,此名称由Microsoft,Designer生成.所以呢?谁告诉你应该保留这些名字呢?您应该将 all 自动生成的名称重命名为语义名称.重构引擎使它变得容易.如果您要获取可维护的代码,请执行此操作.我建议完全不要使用Designer创建事件处理程序(就像我在上面所做的那样),但是许多人不喜欢它.可以,但至少要使用语义名称.

另外,在按钮单击上一次又一次地显示相同的东西是没有意义的,但是我希望这只是为了研究其他技术.

祝你好运,

—SA


问题在于将字符串分配给文本框.
在当前代码中,当for循环迭代时,它将为整数数组分配一个新值至文本框,而旧值将被覆盖,这是仅显示数组最后一个元素的原因.还要在代码末尾删除多余的i ++语句.

 私有  void  btnDeal_Click(对象发​​件人,EventArgs e)
        {

             int  []数字= { 9  8  7  6 };
             for ( int  i =  0 ; i < 数字.长度; i ++)
            {
                 int  num =数字[i];
                txtNorth.Text + = num.ToString()+ "  ;;
            }

        } 



希望此帮助纠正错误.


此代码:

 txtNorth.Text = num.ToString(); 

覆盖每个文本的内容调用循环的时间.您需要这样的内容:

 txtNorth.Text + = num.ToString()+ Environment.NewLine; 

(当然,如果不替换Environment.NewLine,则希望它们全部位于不同的行上和所需的分隔符).

另外,在循环末尾调用i ++是不必要的,循环已经做到了(这就是为什么"for(int i = 0; i!= numbers.Length; i ++)"已经包含i ++)


I have this code. I want all the number are printed out on textbox, but there is only the last number is printed.


private void btnDeal_Click(object sender, EventArgs e)
        {
            
            int[] numbers = { 9, 8, 7, 6 };
            for (int i = 0; i != numbers.Length; i++)
            {                
                int num = numbers[i];
                txtNorth.Text = num.ToString();
                i++;
            }

        }




How to print all number to textbox.

thanks,

解决方案

OK, let''s point out all bugs:

private void btnDeal_Click(object sender, EventArgs e) //not a bug but violation of naming conventions
        {
            int[] numbers = { 9, 8, 7, 6 }; //hard-coded
            //no need to use for cycler, foreach is simpler, and i is not needed
            for (int i = 0; i != numbers.Length; i++) //bad variable name, use < numbers.Length instead
            {                
                int num = numbers[i]; 
                txtNorth.Text = num.ToString(); //data from old iterations is lost
                i++; //incremented again; don't operate cycle variable!
            } 
}



Fixed:

static readonly int[] numbers = { 9, 8, 7, 6 };
static string delimiter = " "; //never use immediate constants, only use explicit constants

//..

btnDeal.Click += (sender, eventArgs) => { //anonymous delegate is the best
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    foreach(int number in numbers) {
        sb.Append(number.ToString());
        sb.Append(delimiter);
    }
    txtNorth.Text = sb.ToString().Trim(); //trim to remove last delimiter
} //btnDeal.Click



This is a variant of delegate method for C# v.2 where lambda syntax was not yet introduced:

btnDeal.Click += delegate(object sender, System.EventArgs eventArgs) { //explicitly declared types needed
    //... same as above
}
</pre>



Some explanation about Microsoft naming conventions: Yes, this name was generated by Microsoft, by Designer. So what? Who told that you''re supposed to leave these names as is? You should renamed all auto-generated names into semantic names. The refactoring engine makes it easy. Do it, if you want to get maintainable code. I would recommend not creating event handlers with Designer at all (as I did above), but many don''t like it. OK, but at least use semantic names.

Also, it makes no sense to show same thing on button click, potentially again and again, but I hope this is just for study of other techniques.

Good luck,

—SA


The problem is with assigning string to text box.
In current code when for loop iterates it will assign a new value from integer array to text box and old value is overwritten as is a reason for showing only last element of array. Also remove extra i++ statement at the end of code.

private void btnDeal_Click(object sender, EventArgs e)
        {

            int[] numbers = { 9, 8, 7, 6 };
            for (int i = 0; i < numbers.Length; i++)
            {
                int num = numbers[i];
                txtNorth.Text += num.ToString() + " ";
            }

        }



Hope this help in rectifying error.


This code:

txtNorth.Text = num.ToString();

overwrites the contents of the text each time the loop is called. You want something like:

txtNorth.Text += num.ToString() + Environment.NewLine;

(assuming of course you want them all on different lines, if not replace Environment.NewLine with your desired separator).

Also, calling i++ at the end of the loop is unnecessary, the loop already does that (which is why the "for (int i = 0; i != numbers.Length; i++)" has i++ in it already)


这篇关于在C Sharp.net中的循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆