本文目录一览:
JAVA数组元素求和
static int[] sum(int[] arr)
{
int[] s=new int[2];
for(int i=0;iarr.length;i+=2)
s[0]+=arr[i];
for(int i=1;iarr.length;i+=2)
s[1]+=arr[i];
return s;
}
求教java中求数组中数的总和是调用哪个方法?
遍历循环,累加求和即可:
int sum=0;
int [] a={100,200,300};
for(int i=0;ia.length ;i++){
sum=sum+a[i];
}
Java数组元素求和的问题
public class Test {
public static void main(String[] args) {
int[] numbers = { 68, 27, 95, 88, 171, 996, 51, 210};
int sum = 0;
for(int i : numbers) {
if(i%2 == 0) {
char[] strs = (i+"").toCharArray();
if(strs[strs.length-1] != 7 strs[strs.length-2] != 7) {
sum = sum+i;
}
}
}
System.out.println(sum);
}
}
Java二维数组每一列求和
public class Array2BianLi {
public static void main(String[] args) {
int[][] arr = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
int sum = 0;
System.out.println("遍历二维数组:");
for (int i = 0; i arr.length; i++) {
for (int j = 0; j arr[i].length; j++) {
System.out.print(arr[i][j] + " ");
sum += arr[i][j];
}
System.out.println();
}
System.out.println("所有元素总和为:" + sum);
}
}
java中如何求一个数组中元素的和.
import java.util.Scanner;
public class XiTi464 {
public static void main(String[] args) {
Scanner sr = new Scanner(System.in);
System.out.print("输入数组元素个数:");
int a = sr.nextInt();
int score[] = new int[a];
for (int i = 0; i a; i++) {
System.out.print("输入第" + (i + 1) + "个值:");
score[i] = sr.nextInt();
}
int sum = arrSum(score);
int max = arrMax(score);
int min = arrMin(score);
System.out.println("数组元素之和:" + sum);
System.out.println("数组元素中最大值:" + max);
System.out.println("数组元素中最小值:" + min);
sr.close();
}
public static int arrSum(int arr[]) {
int temp = 0;
for (int i = 0; i arr.length; i++) {
temp += arr[i];
}
return temp;
}
public static int arrMax(int arr[]) {
int temp = arr[0];
for (int i = 1; i arr.length; i++) {
if (temp arr[i])
temp = arr[i];
}
return temp;
}
public static int arrMin(int arr[]) {
int temp = arr[0];
for (int i = 1; i arr.length; i++) {
if (temp arr[i])
temp = arr[i];
}
return temp;
}
}