CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9

Thread: C# For Loop

  1. #1
    Join Date
    Sep 2015
    Posts
    6

    C# For Loop

    Hi, my problem is displaying the sequence of a for loop in a label. I am sure this is simple, but cannot figure it out. Please help... I want the dice to roll 100 times, and each time there is a double, it will display the roll sequence (the 2nd roll) thanks. Below is my code segment

    private void button1_Click(object sender, EventArgs e)
    {
    Random rollDie = new Random();
    int i, die1, die2, die1Value, die2Value, doubles;

    int count = 100;
    for (i = 1; i <= count; i++)
    {
    label3.Text = "";
    label4.Text = "";

    die1 = rollDie.Next(6);
    die2 = rollDie.Next(6);

    die1Value = die1 + 1;
    die2Value = die2 + 1;

    lblDie1.ImageIndex = die1;
    lblDie2.ImageIndex = die2;



    if (die1 == die2)
    {
    label3.Text = "you rolled double " + "'s";
    label4.Text = ("Current value of i is " + i);

    }



    }

  2. #2
    Join Date
    Sep 2015
    Posts
    6

    Re: C# For Loop

    **Update** Please help if you can. the label is saying the roll sequence is 100 everytime i roll doubles...

    private void button1_Click(object sender, EventArgs e)
    {
    Random rollDie = new Random();
    int i, die1, die2, die1Value, die2Value, doubles;

    int count = 100;
    for (i = 1; i <= count; i++)
    {
    label3.Text = "";
    label4.Text = "";

    die1 = rollDie.Next(6);
    die2 = rollDie.Next(6);

    die1Value = die1 + 1;
    die2Value = die2 + 1;

    lblDie1.ImageIndex = die1;
    lblDie2.ImageIndex = die2;

    doubles = die1 + 1;


    if (die1 == die2)
    {
    label3.Text = "You rolled double " + doubles + "'s";
    label4.Text = ("On the " + i + "th roll");
    }


    }


    }

    }

    }

  3. #3
    Join Date
    Feb 2008
    Posts
    108

    Re: C# For Loop

    You won't see the display until the function exits.
    Using the debugger, I saw that the dies matched 16 times. Of course, each time the results may vary. If you execute label3.Text = ""; and label4.Text = "";, you may see blank for those labels unless you got a match right before the function exited. You may want to add each update to the label string, otherwise you will only display the last match.
    Developing using:
    .NET3.5 / VS 2010

  4. #4
    Join Date
    Sep 2015
    Posts
    6

    Re: C# For Loop

    I took out the blank label.text execution, and it seemed to help. What function are you referring to? How do I do each update? Sorry, I am a novice C# programmer... Thank you for your help

  5. #5
    Join Date
    Sep 2015
    Posts
    6

    Re: C# For Loop

    Its displaying when I get doubles, but doesn't show the sequence in the iteration when the doubles occurred. It just says 100th roll instead of the correct sequence

  6. #6
    Join Date
    Feb 2008
    Posts
    108

    Re: C# For Loop

    The function is: private void button1_Click(object sender, EventArgs e)
    Outside the 'for' loop, add: label4.Text = "Value of i where there was a match, is ";
    Then inside the place where "if (die1 == die2)" add: label4.Text += ", " +i;
    That way after the function exits, all the places "i" where there was a match, are displayed.
    Developing using:
    .NET3.5 / VS 2010

  7. #7
    Join Date
    Sep 2015
    Posts
    6

    Re: C# For Loop

    Thank you for your response, but I am still having trouble. Every time I click the button "roll dice", it should roll the dice as a sequence starting with 1 and go to 100 each time its clicked. It looks like it runs all the way to 100 and shows the first or last matching double. I am so confused. I think their should be a break or continue in the loop, like a pause. Am I in the right direction? How would you do this. Thanks for any help

  8. #8
    Join Date
    Sep 2015
    Posts
    6

    Re: C# For Loop

    RESOLVED!! Used a counter instead of loop...

  9. #9
    Join Date
    Feb 2008
    Posts
    108

    Re: C# For Loop

    I usually prefer 'while' loops that exit when a flag is set, indicating that certain conditions have been met. For example,
    Code:
    while(!done)
    {
    // do whatever needs to be done;
    // When condition(s) have been met, 
    done - true;
    }
    I only use 'for' loops when a specific number of loops need to be made without premature exit.
    Developing using:
    .NET3.5 / VS 2010

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured