求一个数组内值的和

it2023-02-03  51

定义一个数组 并求数组内值得和

public class Demo3 { public static void main(String[] args) { int [] sum = new int[5];//数组中一共有5个值 //给数组赋值 sum[0] = 1; sum[1] = 2; sum[2] = 3; sum[3] = 4; sum[4] = 5; //计算数组中的和 int sums = 0; for (int i = 0; i < sum.length; i++){ sums = sums + sum[i]; } System.out.println("数组中的和为"+sums); } }
最新回复(0)