2

So I am new to C# and I'm making a basic program which assigns random values to an array of 6.

    private void button1_Click(object sender, EventArgs e)
    {
        tbxA.Clear();
        tbxB.Clear();

        int[] number = new int[6];
        Random m = new Random();

        for (int i = 0; i < 6; i++)
        {
            number[i] = m.Next(0, 4);
            tbxA.AppendText("" + Convert.ToString(number[i]) + "\n");
        }

        int sum = number.Sum();
        if (sum > 8)
        {
            tbxB.AppendText(" " + sum);
        }
    }

This will display the randomly generated array numbers 1-6 in the textbox A and if their total sum is over 8 it will display the sum in the textbox B.

However, what I want to do here is so the numbers in the array only displays in textbox A when their sum is greater than 8. If their total sum is less than 8, new numbers should automatically be generated for the array until it matches the requirement sum > 8.

I have tried to use another for loop with everything inside as well as ifs inside it, but it causes the program to crash.

2 Answers 2

2

try this... but remember since you are using a random numbers it may take a while before the condition is met and may look like infinite loop

private void button1_Click(object sender, EventArgs e)
    {
        tbxA.Clear();
        tbxB.Clear();

        int[] number = new int[6];
        Random m = new Random();
        while(true)
        {
            for (int i = 0; i < 6; i++)
            {
                number[i] = m.Next(0, 4);
                tbxA.AppendText(number[i].ToString() + Environment.NewLine);
                //tbxA.AppendText("" + Convert.ToString(number[i]) + "\n");
            }

            int sum = number.Sum();
            if (sum > 8)
            {
                tbxB.AppendText(" " + sum);
                break;
            }
            else{
                tbxA.Clear();
                tbxB.Clear()
            }
        }
    }

or in case you want additional numbers but not to replace old numbers already added..

private void button1_Click(object sender, EventArgs e)
    {   
        tbxA.Clear();
        tbxB.Clear();

        //int[] number = new int[6];
        List<string> number = new List<string>();
        Random m = new Random();
        while(true)
        {
            for (int i = 0; i < 6; i++)
            {
                number.Add(m.Next(0, 4));
            }

            int sum = number.Sum();
            if (sum > 8)
            {
                tbxA.AppendText(string.Join("Environment.NewLine", number.ToArray()))
                tbxB.AppendText(" " + sum);
                break;
            }

        }
    }
Sign up to request clarification or add additional context in comments.

3 Comments

You might also consider using this line: tbxA.AppendText(number[i].ToString() + Environment.NewLine);
thank you! The second code didn't really work cause it gave me red at the number.Add line and I got no clue what the thing you put on the bottom there means. But the first code works like I wanted :)
@Jokuc the second one uses a list.. it will work now as it had an extra semicolon before.. try now read this for more stackoverflow.com/questions/5777729/…
0

Sounds like you're looking for a while loop. Keep looping until the sum is greater than 8.

        int i = 0;
        while (number.Sum() < 8 && number.Length > i)
        {
            number[i++] = m.Next(0, 4);
            tbxA.AppendText("" + Convert.ToString(number[i]) + "\n");
        }

Edit: You will also need to account for adding more values to your array. The reason you probably ran into errors is because you are creating a 6-element array. You will need to create a dynamic array, allocate more room in your array, or use something like a List that you can keep adding more values.

Edit2: Edited loop. Remember, if you create an array of 6 elements you can't add anymore than that. So you have to create a larger array:

int[] number = new int[10];

Or something similary. The above solution assumes your array is large enough to keep adding values. If not, you will have to handle that.

3 Comments

that is an infinite loop.
It didn't work, it froze the whole program :/ I'm not sure how to use lists or dynamic arrays..
Edited accordingly. Sorry was just putting down ideas as opposed to a fully implemented solution.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.