停止循环 [英] stop the loop

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

问题描述

你好!

我有以下代码:

Hello!

I have this code:

String[] text = { "a", "b", "c", "d" };
int count = 0;

private void button1_Click(object sender, EventArgs e)
{
      label1.Text = text[(count % text.Length)];
      count++;
}



此代码将是我第一次按下按钮时先显示"a",然后显示"b","c"和"d",然后从"a"开头开始.但是我如何停止这个循环?所以当我按下按钮时,它不会显示A->仅B,"a"然后是"b","c","d"和"a",仅此而已.
希望我不要让您感到困惑!

在此先感谢!



This code will the first time that i press the button show ''a'' then ''b'', ''c'' and ''d'' and then it starts from the beginning with ''a''. But how do i stop this loop? so when i press the button it wont show A -> B only, ''a'' then ''b'', ''c'' and ''d'' and ''a'' and nothing more.?

I hope that i didn''t confuse you!

Thanks in advance!

推荐答案

如果您只想重新启动cylce:

If you want to just start the cylce over again:

private void button1_Click(object sender, EventArgs e)
{
    if (count == text.Length)
    {
        count = 0;
    }
    label1.Text = text[(count % text.Length)];
    count++;
}


如果您只想在最后一项之后停下它:


If you want to just stop it after the last item:

private void button1_Click(object sender, EventArgs e)
{
    if (count < text.Length)
    {
        label1.Text = text[(count % text.Length)];
        count++;
    }
}






您没有告诉您要在哪里停止循环.
停止"本身没有意义:方法的名称表明这是按钮Click的处理程序,因此它将重复用户单击的次数(为此,您不需要要具有字段"count",您可能需要声明static局部变量.)

当用户关闭应用程序时,该循环将最终"停止.怎么了

同样,如果您想要别的东西,而不是周期性的"a","b","c","d"(您可能期望什么),请直接说:-)

按照John的建议(请参见下文),您可以在count == text.Length时禁用该按钮,但是在这种情况下,尚不清楚为什么用模量运算符(%")发出的所有嗡嗡声:)

放轻松.
You did not tell where you want to stop the loop.
The "stop" itself does not make sense: the name of the method suggests this is a handler of button''s Click, so it will repeat as many times as the user clicks (to do this you don''t have to have a field "count", you may want to declare static local variable, by the way.)

The loop will be "finally" stopped when the user closes the application. What''s wrong.

Again, if you want something else instead of periodic "a", "b", "c", "d" (what else could you possibly expect), say this directly :-)

Following John''s suggestion (see below), you could disable the button when count == text.Length, but in this case it is not clear why all that buzz with modulus operator ("%") :)

Take it easy.


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

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