两个不同大小的数组的元素总和C# [英] sum of elements of two arrays with different sizes C#

查看:55
本文介绍了两个不同大小的数组的元素总和C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

需要此练习的帮助:

"我们有两个 整数,由两个数组表示。找到数字的总和并在数组中表示它。"

"We have two  integer numbers, which are represented by two arrays. Find the sum of numbers and represent it in array."

我编写了一个程序,用于相同大小的数组,但是当元素的总和>时它不起作用。 9. 

I wrote a program, for arrays with same size, but it's not working when the sum of element > 9. 

我不知道,怎么写? 一个不同大小的程序。

And I don't know, how can I write  a program for diferent sizes.

  int [] array1 = new int [] {1,2,9};

            int [] array2 = new int [] {4,5,6};

            int [] array_sum = new int [3];

  for(int i = array.L; i< array1.Length; i ++)



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; {

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; int a_sum = array1 [i] + array2 [i];



  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; Console.Write(a_sum +"");

  &NBSP; &NBSP; &NBSP; &NBSP; &NBSP; }

 int[] array1 = new int[] { 1, 2, 9 };
            int[] array2 = new int[] { 4, 5, 6 };
            int[] array_sum = new int[3];
 for (int i =array.L; i < array1.Length; i++)

            {
                int a_sum = array1[i] + array2[i];

                Console.Write(a_sum + " ");
            }

推荐答案

这是什么意思?你对数组项求和,但它可以是两个数组的总和。你必须从数组的末尾求和它,当一个数组更短时,它意味着有零。你需要两个索引 - 每个索引一个数组。
What does it mean sum? You sum array item but it could be sum of two array. You must to sum it from end of array and when one array is shorter it means there is zero. You need two indices - each for one array.


这篇关于两个不同大小的数组的元素总和C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆