描述:
有两个数组,求它们中的所有数的和。
int[] arr = { 12, 39, 90, 13};
int[] arr2 = { 4, 0, 3};
答案
1 先加一个类 my / Example.java
package my;
public class Example
{
public int sum ( int[] a )
{
int result = 0;
for(int i=0; i<a.length; i++)
{
result += a[i];
}
return result;
}
}
2 调用其方法
package my;
public class HelloWorld
{
public static void main(String[] args)
{
int[] arr = { 12, 39, 90, 13};
int[] arr2 = { 4, 0, 3};
Example ex = new Example();
int s1 = ex.sum( arr);
int s2 = ex.sum( arr2);
int s = s1 + s2;
System.out.println("结果: " + s);
}
}