数组中元素的总和
Sum of elements in an array
我正在为夏季 java 课程做一个简单的作业,只是希望你们能看看我的代码,看看我的做法是否是最好的方法。目的是创建一个简单的 int
数组,至少有 25 个元素,并使用循环遍历它并将所有元素相加。我遇到了一些问题,但看起来我可以正常工作了。在我解决之后,我做了一些研究并看到了一些类似的东西,人们正在使用 For Each 循环(增强循环)。那会是更好的选择吗?我对使用它而不是常规 for 循环的最佳方式感到困惑。
无论如何,任何帮助我成为更好的程序员的评论或批评!
public class Traversals {
public static void main(String[] args) {
int absenceTotal = 0;
// initialize array with 30 days of absences.
int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1,
11, 23, 5, 6, 7, 10, 1, 5,
14, 2, 4, 0, 0, 1, 3, 2, 1,
1, 0, 0, 1, 3, 7, 2 };
for (int i = 0; i < absencesArr.length; i++) {
absencesArr[i] += absenceTotal;
absenceTotal = absencesArr[i];
}
System.out.println("There were " + absenceTotal + " absences that day.");
}
}
不要修改数组。我更喜欢 for-each
loop。而且你应该考虑到可能会有非常多的学生,所以我可能会用 long
代替 sum
。并格式化输出。将其组合成类似
的东西
long sum = 0;
for(int i : absencesArr) {
sum += i;
}
// System.out.println("There were " + sum + " absences that day.");
System.out.printf("There were %d absences that day.%n", sum);
public class Traversals {
public static void main(String[] args) {
int absenceTotal = 0;
// initialize array with 30 days of absences.
int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1,
11, 23, 5, 6, 7, 10, 1, 5,
14, 2, 4, 0, 0, 1, 3, 2, 1,
1, 0, 0, 1, 3, 7, 2 };
for (int i = 0; i < absencesArr.length; i++) {
// remove this
//absencesArr[i] += absenceTotal;
absenceTotal += absencesArr[i]; //add this
}
System.out.println("There were " + absenceTotal + " absences that day.");
}
}
除了其他不错的贡献,我是 for-each loop
的粉丝,通常会在一行中完成。
for(int i : absencesArr) absenceTotal += i;
System.out.printf("There were %d absences that day.", absenceTotal);
但在某些情况下,当我想控制我的对象 size/length/count 时,我会使用 for loop
,如下例所示:
for (int i = 0; i < absencesArr.length; i++) absenceTotal += absencesArr[i];
System.out.printf("There were %d absences that day.", absenceTotal);
如果我需要在 for loop
或 for-each loop
中包含不止一行代码,那么我会将它们全部放在大括号 { more than one line of code }
.
中
在 Java 8 中你可以使用流 api:
public class Traversals {
public static void main(String[] args) {
int absenceTotal = 0;
// initialize array with 30 days of absences.
int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1,
11, 23, 5, 6, 7, 10, 1, 5,
14, 2, 4, 0, 0, 1, 3, 2, 1,
1, 0, 0, 1, 3, 7, 2 };
absenceTotal = IntStream.of(array).sum();
System.out.println("There were " + absenceTotal + " absences that day.");
}
}
我知道的最短方法是:
int sum=Arrays.stream(absencesArr).sum();
我正在为夏季 java 课程做一个简单的作业,只是希望你们能看看我的代码,看看我的做法是否是最好的方法。目的是创建一个简单的 int
数组,至少有 25 个元素,并使用循环遍历它并将所有元素相加。我遇到了一些问题,但看起来我可以正常工作了。在我解决之后,我做了一些研究并看到了一些类似的东西,人们正在使用 For Each 循环(增强循环)。那会是更好的选择吗?我对使用它而不是常规 for 循环的最佳方式感到困惑。
无论如何,任何帮助我成为更好的程序员的评论或批评!
public class Traversals {
public static void main(String[] args) {
int absenceTotal = 0;
// initialize array with 30 days of absences.
int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1,
11, 23, 5, 6, 7, 10, 1, 5,
14, 2, 4, 0, 0, 1, 3, 2, 1,
1, 0, 0, 1, 3, 7, 2 };
for (int i = 0; i < absencesArr.length; i++) {
absencesArr[i] += absenceTotal;
absenceTotal = absencesArr[i];
}
System.out.println("There were " + absenceTotal + " absences that day.");
}
}
不要修改数组。我更喜欢 for-each
loop。而且你应该考虑到可能会有非常多的学生,所以我可能会用 long
代替 sum
。并格式化输出。将其组合成类似
long sum = 0;
for(int i : absencesArr) {
sum += i;
}
// System.out.println("There were " + sum + " absences that day.");
System.out.printf("There were %d absences that day.%n", sum);
public class Traversals {
public static void main(String[] args) {
int absenceTotal = 0;
// initialize array with 30 days of absences.
int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1,
11, 23, 5, 6, 7, 10, 1, 5,
14, 2, 4, 0, 0, 1, 3, 2, 1,
1, 0, 0, 1, 3, 7, 2 };
for (int i = 0; i < absencesArr.length; i++) {
// remove this
//absencesArr[i] += absenceTotal;
absenceTotal += absencesArr[i]; //add this
}
System.out.println("There were " + absenceTotal + " absences that day.");
}
}
除了其他不错的贡献,我是 for-each loop
的粉丝,通常会在一行中完成。
for(int i : absencesArr) absenceTotal += i;
System.out.printf("There were %d absences that day.", absenceTotal);
但在某些情况下,当我想控制我的对象 size/length/count 时,我会使用 for loop
,如下例所示:
for (int i = 0; i < absencesArr.length; i++) absenceTotal += absencesArr[i];
System.out.printf("There were %d absences that day.", absenceTotal);
如果我需要在 for loop
或 for-each loop
中包含不止一行代码,那么我会将它们全部放在大括号 { more than one line of code }
.
在 Java 8 中你可以使用流 api:
public class Traversals {
public static void main(String[] args) {
int absenceTotal = 0;
// initialize array with 30 days of absences.
int absencesArr[] = { 1, 3, 0, 9, 8, 23, 1,
11, 23, 5, 6, 7, 10, 1, 5,
14, 2, 4, 0, 0, 1, 3, 2, 1,
1, 0, 0, 1, 3, 7, 2 };
absenceTotal = IntStream.of(array).sum();
System.out.println("There were " + absenceTotal + " absences that day.");
}
}
我知道的最短方法是:
int sum=Arrays.stream(absencesArr).sum();