80 二维数组
package com.fqs.test; import java.util.Random; public class hello { public static void main(String[] args) { //定义数组 int[][]arr3={ {1,2,3}, {4,5,6,7,8} }; //获取元素 System.out.print(arr3[0][0]);//获取第一列的第一个 System.out.print(arr3[0][1]);//获取第一列的第二个 } }//类结束
package com.fqs.test; import java.util.Random; public class hello { public static void main(String[] args) { //定义数组 int[][]arr3={ {1,2,3}, {4,5,6,7,8} }; //获取元素 for (int i = 0; i < arr3.length; i++) { System.out.print("\n"); for (int j = 0; j < arr3[i].length; j++) { System.out.print(arr3[i][j]); } } } }//类结束
求营业额
package com.fqs.test;
import sun.font.FontRunIterator;
import java.util.Random;
public class hello {
public static void main(String[] args) {
//计算每个季度的总营业额和全年的总营业额
int[][]arr={
{22,66,44},
{77,33,88},
{25,45,65},
{11,66,99}
};
int num=0;
int sum=0;
int ySum=0;
for (int i = 0; i < arr.length; i++) {//共有行
int []yArr= arr[i];//将每行里的每列的值放到一个数组中
int jSum2=getYSum(yArr);//调用方法获得总和
System.out.println("每季度的sum:"+jSum2);
for (int j = 0; j < arr[i].length ; j++) {
num=arr[i][j];
sum=sum+num;
}
}
System.out.println("全年的sum:"+sum);
}//结束主方法
//获得一维数组每行的和
public static int getYSum(int[]yArr){
int jSum=0;
for (int i = 0; i < yArr.length; i++) {
jSum=jSum+yArr[i];
}
return jSum;
}
}//类结束