Perhaps it might help if we back away from the
Java syntax a little bit and describe the steps in English. First of all, it seems like there are 3 different tasks you need to accomplish:
When you translate this into Java, you could put each calculation in its own method. But for now, let's just pretend we will do it in a single method and elaborate on each step a little further. So how do we "sum up the elements in the array". Well, we can flesh this out like this
Notice that I have replaced the first line with some details that are a little closer to the way we actually do it in Java. Similarly, we can elaborate on the last two steps:
Notice that this time I used symbols that look a little more like Java than English. This is because I found it more convenient and expressive. I also ignored a few details like what the value of n is. If I were writing this out for myself, I would know that I mean that n stands for the number of elements in the array.
Using so-called "pseudocode" in this manner to describe the steps to solve a problem is often useful. One advantage is that you can concetrate on how to solve the problem and you don't have to worry about syntax details. Notice how on each iteration, I added more details so that it is now to the point that I can translate each line of "pseudocode" into a line of actual Java code. Also notice how I split up the three tasks to start with. This lead me to eventually use two different for loops since I need to know the mean before I can calculate the standard deviation. The lesson is that you shouldn't try to do too many things at once.
If you have learned about methods, it would be a good exercise to write three different methods to do these calculations. The calculateMean() method could use the calculateSum() method and the calcualteStdDev() method could use the calculateMean() method to avoid repitition of code.
I hope this helps illustrate one approach to writing code to solve a given problem. There are many other approaches, but one thing they have in common is that you rarely just jump into writing code at the beginning.
Layne