1

我有一个变量“start”,它的初始化值为 0。

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

所以这就是我想要完成的

当我点击按钮

第一个循环从包含“XXXX”的文本块 1 开始,每次单击/触摸时,变量“开始”递增 1 直到达到 34 。所以当计数器达到 34 时,文本变为 'YYYY'

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

Last Loop: 计数器再次重置,直到 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();

    }

我还有一个重置按钮,无论您在柜台的哪个位置,一切都将重新开始。关于我如何做到这一点的任何指示?

4

2 回答 2

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();
}
于 2012-09-20T21:25:42.780 回答
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();
}
于 2012-09-20T21:54:20.123 回答