每次我按运行时都会说有错误 这是我的程序:
package arraylab;
public class ArrayLab {
public static void main(String[] args) {
double number = 5.0;
double list[] = new double[10];
arrInit(list,number);
System.out.println("Array after Print --> ");
arrPrint(list);
double result;
result = arrSumFive(list);
System.out.println("Result = "+result);
System.out.println("Array after Print --> ");
arrPrint(list);
}
public static void arrInit(double array[],double number)
{
for(int i = 0;i<array.length;i++){
array[i] = number;
}
}
public static void arrSumFive(double arr[]){
double sum = 0;
for(int i=0;i<arr.length;i++){
if(arr[i]%5==0)
sum = sum + arr[i];
}
return sum;
}
public static void arrPrint(double arr[]){
int counter = 0;
for(int i=0;i<arr.length;i++){
if(counter == 2){
counter = 1;
System.out.println();
}
else{
counter++;
}
System.out.print(arr[i]+" ");
}
System.out.println();
我想编写程序执行这个 thugs:arrInit” 方法,该方法有两个参数;双精度数组和初始化 数字。该方法将使用初始化编号初始化数组。
b. “arrSumFive”方法,采用双精度数组作为参数并返回 该数组中接受除以 5 的数字。 c. “arrPrint”方法将数组的内容打印为每行 2 个数字。 在 main 方法中,声明一个名为“list”的双精度数组,其中有 10 个元素并测试 以前的方法。 这个
我建议使用像eclipse这样的IDE,这样你就不会犯简单的语法错误,你的代码应该是这样的,
package arraylab;
public class ArrayLab {
public static void main(String[] args) {
double number = 5.0;
double list[] = new double[10];
arrInit(list, number);
System.out.println("Array after Print --> ");
arrPrint(list);
double result;
result = arrSumFive(list);
System.out.println("Result = " + result);
System.out.println("Array after Print --> ");
arrPrint(list);
}
public static void arrInit(double array[], double number) {
for (int i = 0; i < array.length; i++) {
array[i] = number;
}
}
public static double arrSumFive(double arr[]) {
double sum = 0;
for (int i = 0; i < arr.length; i++) {
if (arr[i] % 5 == 0) {
sum = sum + arr[i];
}
}
return sum;
}
public static void arrPrint(double arr[]) {
int counter = 0;
for (int i = 0; i < arr.length; i++) {
if (counter == 2) {
counter = 1;
System.out.println();
} else {
counter++;
}
System.out.print(arr[i] + " ");
}
System.out.println();
}
}