来自不同方法的两个数组的总和

时间:2019-10-11 19:15:53

标签: c# arrays visual-studio

我有一个小程序可以掷两个骰子。因此,用户在单击开始时应输入一个数字作为掷骰子的频率编号,程序将显示两个不同的列,每个列均根据用户输入的频率填充随机数。

程序如下:

rolldice

出现随机数后。程序将对随机数求和(我将每个骰子的随机数填充到数组中),我感到困惑,如何从不同方法求和两个数组?

到目前为止,这是我的代码:

private void button1_Click(object sender, EventArgs e)
{
    firstDice();
    secondDice();
}
Random vran = new Random();
void firstDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0;i<x;i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox2.Text = temp;
    }
}
void secondDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0; i < x; i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox3.Text = temp;
    }
}

注意:数组将为每个值求和(即array [0] + array [0] ..依此类推)。

4 个答案:

答案 0 :(得分:1)

您将需要从每个firstDicesecondDice方法返回一个数组,以便您可以在button1_Click方法中将它们两个相加。

private void button1_Click(object sender, EventArgs e)
{
    int[] d1 = firstDice();
    int[] d2 = secondDice();
    // Assuming d1 and d2 are always of the same length
    int[] sum = new int[d1.Length];
    for (int i = 0; i < sum.Length; i++)
    {
        sum[i] = d1[i] + d2[i];
    }
}
Random vran = new Random();
int[] firstDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0;i<x;i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox2.Text = temp;
    }
    return arr;
}
int[] secondDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0; i < x; i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox3.Text = temp;
    }
    return arr;
}

答案 1 :(得分:1)

这是您的代码已更正,可以执行您想要的操作。首先请注意,firstDice()secondDice()现在都返回一个整数数组。然后在button1_Click中,通过一个for循环遍历每个数组元素并将其求和,结果返回到array1中。

private void button1_Click(object sender, EventArgs e)
{
     int[] array1 = firstDice();
     int[] array2 = secondDice();
     for (int i = 0; i < array1.Length; i++)
     {
         array1[i] += array2[i];
     }
}
Random vran = new Random();
int[] firstDice()
{
     string temp = null;
     int x = Convert.ToInt32(textBox1.Text);
     int[] arr = new int[x];
     for (int i = 0;i<x;i++)
     {
          int a = vran.Next(1, 6);
          arr[i] = a;
          temp += a + " ";
          textBox2.Text = temp;
     }
     return arr;
}
int[] secondDice()
{
     string temp = null;
     int x = Convert.ToInt32(textBox1.Text);
     int[] arr = new int[x];
     for (int i = 0; i < x; i++)
     {
         int a = vran.Next(1, 6);
         arr[i] = a;
         temp += a + " ";
         textBox3.Text = temp;
     }
     return arr;
}

答案 2 :(得分:0)

只需在类下声明私有变量,然后像这样使用它们:

private int firstDiceSum = 0;
private int secondDiceSum = 0;
private int totalSum = 0;

private void button1_Click(object sender, EventArgs e)
{
    firstDice();
    secondDice();

    // Sum
    totalSum = firstDiceSum + secondDiceSum;

    //Show it to the user 
    textBox4.Text = totalSum.ToString();
}

Random vran = new Random();

void firstDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0;i<x;i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox2.Text = temp;

        //add this 
        firstDiceSum += a;
    }
}

void secondDice()
{
    string temp = null;
    int x = Convert.ToInt32(textBox1.Text);
    int[] arr = new int[x];
    for (int i = 0; i < x; i++)
    {
        int a = vran.Next(1, 6);
        arr[i] = a;
        temp += a + " ";
        textBox3.Text = temp;

        //add this 
        secondDiceSum += a;
    }
}

答案 3 :(得分:0)

您可以仅使用一种方法来生成骰子掷骰来简化代码。您可以使用string.Join来连接数组,使用Enumerable.Zip来求和。另请注意,Random.Next is exclusive的最大值,因此,如果要包含6,则必须为7

private void button1_Click(object sender, EventArgs e)
{
    int frequency = Convert.ToInt32(textBox1.Text);
    var firstDice = RollDice(textBox2,frequency);
    var secondDice = RollDice(textBox3,frequency);
    textBox4.Text = string.Join(" ",firstDice.Zip(secondDice, (x,y)=> x+y));
}
Random vran = new Random();
int[] RollDice(TextBox display, int frequency)
{    
    int[] arr = new int[frequency];
    for (int i = 0; i<frequency; i++)
    {
        int a = vran.Next(1, 7); //If you want 6 inclusive you should change to 7
        arr[i] = a;        
    }
    display.Text = string.Join(" ", arr);
    return arr;
}