www.zhnq.net > JAvA二维数组元素相加问题

JAvA二维数组元素相加问题

数组下标越界 for(int i=0;i<str.length;i++){ for(int j=0;j<str.length;j++){ 第二个str.length应该是str[0].length

public int sum(int[][] a2){ int sum = 0; for(int i=0;i 评论0 0 0

public class TypeOp { public static void main(String[] args) { int a[]= new int[5];//定义数组 a[0]=10;//赋值 a[1]=20;//赋值 int j = a[0]+a[1]; System.out.println(j);//求和 } }

使用循环将值遍历进行相加 例:int[] arr={1,2,3,4,5}; int sum=0; for(int a:arr){ sum+=a; }

int c[5]; int d=0; //补上这一句 for(a=0;a<5;a++) scanf("%d",&c[a]); for(b=0;b<5;b++) d+=c[b]; printf("%d",d);

public class ArraySum{ public static void main(String[] args) { int[] a1 = {1, 2, 3, 4}; int[] a2 = {5, 6, 7, 8}; int sum = 0; for (int i=0; i<a1.length; i++) { sum = sum + a1[i]; } for (int i=0; i<a2.length; i++) { sum = sum + a2[i]; } System.out.println("2数组总和是: " + sum); }}

//数组只能是偶数,奇数不运行import java.util.ArrayList;public class dd { public static void main(String[] args) { int[] a={1,2,3,4,5,6}; ArrayList<String> aList=new ArrayList<String>(); if (a.length%2==0){ for(int i=0,j=a.length-1;i<a.length/2;i++,j--){ int r=

二维数组的话,必须循环行和列,之后求总和即可:public class Test { public static void main(String args[]) throws ParseException { int [][]a = {{1,2,4,5},{6,7,8,9},{10,11,12,13},{14,15,16,17}}; int sum =0; //设置sum为0,方便计算 for(int i = 0; i < a.

public class CountSum { public static void main(String[] args) { double[] 牛奶={357,67,98,345.5,66.5,145.5}; double[] 苹果={135,674,98,345.5,200,245.5}; double[] 肥皂={245,178,98,346,66.5,289}; System.out.println("牛奶: "+getSum(牛奶)

public static void main(String[] args) { int[][] arr = {{23, 45, 65, 34, 21, 67, 78},{46, 14, 18, 46, 98, 63, 88},{98, 81, 64, 90, 21, 14, 23},{54, 43, 55, 76, 22, 43, 33}}; for (int i = 0; i < arr.length; i++) { int total=0; for (int j = 0; j < arr[i].length; j++) { total += arr[i]

网站地图

All rights reserved Powered by www.zhnq.net

copyright ©right 2010-2021。
www.zhnq.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com