大哥大姐帮忙解释一下下面的程序,每条都解释一下,有追加分

来源:百度知道 编辑:UC知道 时间:2024/07/01 04:42:44
public class Calculate {
static int [] aa={2,7,9,11,44,23,68,92,81};
static long getSum() {
long result = 0 ;
if (aa == null) {
return 0;
}
for (int i = 0; i < aa.length; i++) {
if (aa[i]%2 == 0) {
System.out.println(aa[i] + "added");
result += aa[i];
}
}
return result;
}
public static void main(String[] args) {
System.out.println(getSum());
}
}

public class Calculate {
/**
* 待计算的数组
*/
static int [] aa={2,7,9,11,44,23,68,92,81};
/**
* 用于计算一个数组里偶数之和的方法
*/
static long getSum() {
long result = 0 ; // 用于保存计算结果,为防止整数相加越界,因此使用long型
if (aa == null) { // 如果数组为空,直接返回0
return 0;
}
// 循环遍历数组,找出其中的偶数
for (int i = 0; i < aa.length; i++) {
if (aa[i]%2 == 0) {// 如果能被2整除,则说明是偶数
System.out.println(aa[i] + "added"); // 打印信息
result += aa[i]; // 将该偶数累加到结果中
}
}
return result; // 返回结果
}
/**
* 主函数
*/
public static void main(String[] args) {
System.out.println(getSum()); // 打印计算结果
}
}

你这是计算一个数组里所有偶数之和并把它打印出来的一段代码吧!!

同意楼上意见

//定义了一个类 好象是用来计算的
public class Calculate {
//定义了一个静态的数组aa
static int [] aa={2,7,9,11,44,23,68,92,81};
//定义了一个静态的方法,应该是求和的
static long getSum() {
//定义了一个长整型的变量,赋初值0。
long result = 0 ;
//判断aa这个数组是否为空