www.zhnq.net > jAvA数组10个数求和

jAvA数组10个数求和

[图文] 设计思想:先从命令行读出数字,然后计算各个数字之和.求出结果.流程图:程序源代码:import java.io.*;public class Sum {public static void main(S_java数组10个数求和

public class Sum { public static void main(String[] args) { double i;//随机生成的数字 double sum=0; for(int j=0;j<10;j++) { i=(double) Math.random(); System.out.println("第"+(j+1)+"次生成的数字是"+i); sum+=i; } System.out.println("它们的和是:"+sum); }}

int arr[] = new int[10]; int sum = 0; int avg = 0; for (int i = 0; i arr[i] = (int) (Math.random()*10); sum += arr[i]; } System.out.println("sum:"+sum); System.out.println("avg:"+sum/arr.length);

import java.util.Arrays; public class $ { public static void main(String[] args) { int[] arr = new int[10]; double sum = 0; for (int i = 0; i < arr.length; i++) { arr[i] = (int) (Math.random() * 100); sum += arr[i]; System.out.print(arr[i] + " "); } System.out.println();

这个很简单的吗public class ss { public static void main(String[] args) { int[] a = {1,2,3,4,5}; int sum = cd(a); System.out.println(sum); } private static int cd(int[] a) { int sum = 0; for(int i = 0 ;i< a.length;i++) { sum += a[i]; } return sum; } }

public static void main(String [] args) { Scanner sc = new Scanner(System.in); int count =1; int sum = 0; int avg = 0; while(count<11){ sum +=sc.nextInt(); count++; } //平均值 avg = sum/10; }

public class Main { public static void main(String[] args){ int[] a = {1,2,3,4,5}; int[] b = {6,7,8,9,10}; int[] c = new int[a.length]; for(int i=0;i c[i]=a[i]+b[i]; } } }

c[] c=a.join(b);float sum=0;for(int i=0;i<c.length;i++){sum=sum+c[i];}好像是这样.

利用Scanner循环输入10个数,在for循环中把每次输入的数抄字求和.代码如下:public class Test8 { 袭public static void main(String[] args) { Scanner scanner = new Scanner(System.in); int sum = 0; for (int i = 0; i < 10; i++) { System.out.print("

import java.util.Scanner; public class Du { public static void main(String[] args) { final int length = 10; int[] ary = getAryFromInput(length); int sum = 0; System.out.print("Inputed element are: "); for(int i = 0; i < ary.length; i++){ System.out.print(ary[i] +

网站地图

All rights reserved Powered by www.zhnq.net

copyright ©right 2010-2021。
www.zhnq.net内容来自网络,如有侵犯请联系客服。zhit325@qq.com