如何在for循环后运行for循环?

时间:2012-09-20 21:04:55

标签: c#

我有一个变量'start',用值0初始化。

当1参数变为真时,如何切换到不同的循环。

所以这就是我想要完成的事情

当我点击按钮

第一个循环开始,textBlock 1包含“XXXX” 每次点击/触摸时,变量'开始'增量为1,直到达到34。因此,当计数器达到34时,文本将变为“YYYY”

第二个循环是计数器重置并从0再次开始,但这次只需要上升到33。一旦达到33,文本就会变为'ZZZZ'。

最后一个循环:计数器再次重置直到33。但这次它结束了。它回到循环1。

这是我现在的代码,我似乎无法弄清楚如何进行最后一次循环。

public partial class MainPage : PhoneApplicationPage
{
    private int start = 0;
    private bool sw = false;
    // Constructor
    public MainPage()
    {
        InitializeComponent();
        int start = 0;

    }

    private void button1_Click(object sender, RoutedEventArgs e)
    {
        tasbih();            
    }
    public void tasbih()
    {
        if (sw == false)
        {
            textBlock1.Text = "TEXTBX 1";
        }
        start++;
        String text1 = Convert.ToString(start);
        textBlock2.Text = text1;

        if (start >= 35)
        {
            textBlock1.Text = "TEXTBX 2";
            start = 0;
            String text2 = Convert.ToString(start);
            textBlock2.Text = text2;
            sw = true;                
        }


    }

    private void button2_Click(object sender, RoutedEventArgs e)
    {


        textBlock1.Text = "Reset";
        tasbih();

    }

此外,我还有一个重置按钮,无论您在柜台的哪个位置,都会启动所有内容。有关如何做到这一点的任何指示?

2 个答案:

答案 0 :(得分:0)

我建议你有一个更明确的状态变量,并关闭所有逻辑。您目前仅在转换为start时关注sw = true,如果您有明确的状态,则会更容易。

enum MyState { Part1, Part2, Part3 }


MyState currentState = Part1;
int clickCount = 0;

public void tasbih()
{
    clickCount++;

    // First, do state transitions.
    switch(currentState)
    {
        case MyState.Part1:
            if(clickCount >= 34) { currentState = MyState.Part2; clickCount = 0; } 
            break;
        case MyState.Part2:
            if(clickCount >= 33) { currentState = MyState.Part3; clickCount = 0; } 
            break;
        case MyState.Part3:
            if(clickCount >= 33) { currentState = MyState.Part1; clickCount = 0; } 
            break;
    }

    // Now, act on the current (or new) state.
    switch(currentState)
    {
        case MyState.Part1:
            textBlock1.Text = "TEXTBX 1";
            textBlock2.Text = clickCount.ToString();
            break;
        case MyState.Part2:
            textBlock1.Text = "TEXTBX 2";
            textBlock2.Text = clickCount.ToString();
            break;
        case MyState.Part3:
            textBlock1.Text = "ZZZZ";
            textBlock2.Text = clickCount.ToString();
            break;
    }
}

private void button2_Click(object sender, RoutedEventArgs e)
{
    currentState = MyState.Part1;
    clickCount = 0;
    tasbih();
}

答案 1 :(得分:0)

// these have to be member variables, or you'll never be able to reset
// or even remember the counter between calls
private int clicks = 0;  // the number of clicks in the current loop
private int state = 0;   // which loop we're in

// these are just so we don't have to repeat ourselves.
// each element corresponds to a possible value of `state`.
// both arrays should have the same number of elements.
// alternatively, you could have a type containing the label and max together,
// and just have an array of those.
private string[] labels = { "TEXTBX 1", "TEXTBX 2", "ZZZZ" };
private int[] max = { 34, 33, 33 };


public void reset() {
    clicks = 0;
    state = 0;
    // probably reset text boxes here too.  If you don't want to, then delete
    updateTextBoxes();
}

public void bump()
{
    // bump the counter, and if it's high enough, switch to the next state
    // and reset the counter
    // (the "% max.Length" causes 3 to be 0, so states wrap around)
    if (++clicks > max[state])
    {
         state = (state + 1) % max.Length;
         clicks = 0;
    }

    updateTextBoxes();
}

private void updateTextBoxes() {
    textBlock1.Text = labels[state];
    textBlock2.Text = clicks.ToString();
}
相关问题