2015-07-03 112 views
5

我正在为一个夏季java课程做一个简单的任务,希望你们能看看我的代码,看看我做的方式是否是最好的方法。目的是创建一个至少有25个元素的简单int数组,并使用循环遍历它并合并所有元素。我有一些问题,但看起来像我得到它的工作。在我解决问题之后,我做了一些小小的研究,看到一些人们使用For Each循环(增强循环)的类似东西。这会是一个更好的选择吗?我有点困惑于最好的方式来使用,而不是普通的循环。数组元素的总和

无论如何,任何评论或批评帮助我成为更好的程序员!

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."); 
    } 
} 
+8

以供将来参考,这种问题会更适合codereview.stackexchange.com。 –

+1

这个问题和这个类似吗? https://stackoverflow.com/questions/4550662/how-do-you-find-the-sum-of-all-the-numbers-in-an-array-in-java –

+2

你应该缩进你的'主要' – Quill

回答

7

不要修改数组。我宁愿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); 
+0

老实说,我尝试过类似的东西,但它似乎只是加上索引,所以我搞砸了,直到我得到了我上面的东西。我会再试一次,因为我只是看着作业,它想要一个额外的循环来打印出我不知道的方式,因为我改变了阵列。 – NoobCoderChick

+0

我可能会使用long long,因为我认为int和long在某些平台上可能具有相同的大小 – ggrr

+0

@amuse不在Java中,Java没有“long long”。另见[JLS-4.2。原始类型和值](http://docs.oracle.com/javase/specs/jls/se7/html/jls-4.html#jls-4.2)。 –

4
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."); 
    } 
} 
2

除了一些不错的贡献,我的for-each loop风扇,并且通常在一行做到这一点。

for(int i : absencesArr) absenceTotal += i; 
System.out.printf("There were %d absences that day.", absenceTotal); 

但在某些情况下,当我想要在我的对象大小/长度/计数控制,我会用for loop像下面的例子:

for (int i = 0; i < absencesArr.length; i++) absenceTotal += absencesArr[i]; 
System.out.printf("There were %d absences that day.", absenceTotal); 

如果我需要有一个以上的for loopfor-each loop里面的代码行,然后我会把它们全部放在花括号内{ more than one line of code }

0

在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."); 
    } 
}