java 请编写一名为sumarr方法,
请编写一名为sumarr方法,该方法的功能为求某一维数组的所有元素的和并返回,请在main方法中从键盘输入5个数存入1维数组,而后调用sumarr方法求所有元素的和并输出...
请编写一名为sumarr方法,该方法的功能为求某一维数组的所有元素的和并返回,请在main方法中从键盘输入5个数存入1维数组,而后调用sumarr方法求所有元素的和并输出。程序保存为E18_3.java
展开
4个回答
展开全部
import java.util.Scanner;
public class E18_3{
public static void main(String[] args){
int[] intArr = new int[5];
Scanner sc = new Scanner(System.in);
for(int i=0;i<5;i++){
System.out.print("请输入数组第"+(i+1)+"个数:");
intArr[i] = sc.nextInt();
}
int sum = 0;
for(int j=0;j<intArr.length;j++){
sum+=intArr[j];
}
System.out.print("数组的和为:"+sum);
}
}
展开全部
public static void main(String[] args)
{
/* boolean t=(false||true);
System.out.println(t);*/
Scanner sc = new Scanner(System.in);
int[] b = new int[5];
for (int i = 0; i < 5; i++) {
b[i] = sc.nextInt();
}
System.out.println(sumarr(b));
}
public static int sumarr(int[] a) {
int sum=0;
for (int i = 0; i < a.length; i++) {
sum += a[i];
}
return sum;
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package test5;
import java.util.Scanner;
public class E18_3
{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int a[]=new int[5];
for(int i=0;i<5;i++){
a[i]=s.nextInt();
}
System.out.println("五个数的和为:"+summer( a));
}
public static int summer(int a[]){
int sum=0;
for(int i=0;i<5;i++){
sum+= a[i];
}
return sum;
}
}
import java.util.Scanner;
public class E18_3
{
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
Scanner s=new Scanner(System.in);
int a[]=new int[5];
for(int i=0;i<5;i++){
a[i]=s.nextInt();
}
System.out.println("五个数的和为:"+summer( a));
}
public static int summer(int a[]){
int sum=0;
for(int i=0;i<5;i++){
sum+= a[i];
}
return sum;
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
package com.xx.oo;
import java.util.Scanner;
public class E18_3 {
static Long sumarr(long arr[]){
Long lo=0L;
for (long l : arr) {
lo+=l;
System.out.println(lo);
}
return lo;
}
public static void main(String[] args) {
Scanner san1 = new Scanner(System.in);
long a1 = san1.nextLong();
Scanner san2 = new Scanner(System.in);
long a2 = san2.nextLong();
Scanner san3 = new Scanner(System.in);
long a3 = san3.nextLong();
Scanner san4 = new Scanner(System.in);
long a4 = san4.nextLong();
Scanner san5 = new Scanner(System.in);
long a5 = san5.nextLong();
long [] arr={a1,a2,a3,a4,a5};
E18_3.sumarr(arr);
}
}
import java.util.Scanner;
public class E18_3 {
static Long sumarr(long arr[]){
Long lo=0L;
for (long l : arr) {
lo+=l;
System.out.println(lo);
}
return lo;
}
public static void main(String[] args) {
Scanner san1 = new Scanner(System.in);
long a1 = san1.nextLong();
Scanner san2 = new Scanner(System.in);
long a2 = san2.nextLong();
Scanner san3 = new Scanner(System.in);
long a3 = san3.nextLong();
Scanner san4 = new Scanner(System.in);
long a4 = san4.nextLong();
Scanner san5 = new Scanner(System.in);
long a5 = san5.nextLong();
long [] arr={a1,a2,a3,a4,a5};
E18_3.sumarr(arr);
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询