通过简单的数组求和,了解动态数组的构建
package com.ty.java;
import java.util.Scanner;
public class word {
public static void main(String[] args) {
int[] a = null;
a = new int[10]; //构建动态数组
int sum = 0;
Scanner sc = new Scanner(System.in); //构建输入数组的属性
for (int i = 0; i < a.length; i++) {
a[i] = sc.nextInt();
sum += a[i];
}
System.out.println("和 = " + sum);
}
}
代码运行如下:
com.ty.java.word
12 45 32 69 21 31 56 68 99 50
和 = 483
Process finished with exit code 0