输入任意5个整数,输出它们的和。

mac2022-06-30  89

import java.util.*; public class Demo { public static void main(String[] args) { int intArray[] = new int[5]; //定义数组,并分配内存 long total = 0; int len = intArray.length; //数组的长度 System.out.println("请输入" + len + "个整数,以空格为分隔:"); Scanner sc = new Scanner(System.in); for (int i = 0; i < len; i++) { intArray[i] = sc.nextInt(); } //for循环遍历数组 for (int i = 0; i < len; i++) { total += intArray[i]; } System.out.println("所有数组元素的和为: " + total); sc.close(); } }

转载于:https://www.cnblogs.com/fanren224/p/8457229.html

最新回复(0)