public class arrayAddition
{
public static void main(String[]args)
{
int[] myarray={3,0,6,8,4};
int addmyarray()
{
int sum=0;
for(int a:myarray)
{
sum=sum+a;
}
return sum;
}
System.out.println("Sum of array is "+ addmyarray());
}
}
上面的代码在方法声明中抛出错误。错误说';'预期。有人可以帮忙吗?
答案 0 :(得分:1)
试试这个,
public class arrayAddition {
public static int addmyarray(int[] myarray) {
int sum = 0;
for (int a : myarray) {
sum = sum + a;
}
return sum;
}
public static void main(String[] args) {
int[] myarray = { 3, 0, 6, 8, 4 };
System.out.println("Sum of array is " + addmyarray(myarray));
}
}
答案 1 :(得分:0)
你好我想你想这样做:
public class DemoClass {
public static void main(String[] args)
{
int[] myarray={3,0,6,8,4};
System.out.println(" Sum of array is "+ addArray(myarray));
}
public static int addArray(int[] arrayOfInteger)
{
int sum=0;
for(int a: arrayOfInteger)
{
System.out.println("\n Current Value of Array : " + a);
sum=sum+a;
}
return sum;
}
}
<强>输出强>
希望这对你有所帮助。
myarray
传递给方法public static int addArray(int[] arrayOfInteger)
,该方法定义为接受integer array