Question

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.

Was it helpful?

Solution

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;
            }

        }
    }

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top