I have an exercise where the goal is to output 15 integers into a 5x3 array, Output the values in chart format and calculate and output the product of each row beside the appropriate row. I am having trouble with getting and outputting the product for each row; as of now my code (below) can output the product for ALL 15 integers entered, but not for each row. Code is below, any help is appreciated!
* The naming convention for a non-constant variable is just lowercase letters. So PRODUCT would be product. Uppercase letters are only used if the value is constant.
* Comments are good, but be careful of comments that do nothing other than state what can be seen clearly from the code. For instance:
It's obvious from the code that you are printing numbers. Leave that comment out.
All things are lawful, but not all things are profitable.
I'd like to point out something about code comments though. Having all those comments like that is ok as a beginner. Your teacher might even encourage to write comments like that. However, they are very noisy and do nothing other than to obscure the code and make it way more difficult to read.
Comments like this are pointless and should really be deleted:
So is this:
Comments are often code smells. Avoid comments that try to explain WHAT the code is doing or HOW it's doing it. Strive to make your code more expressive and self-explanatory instead. You're an author, so make your code tell a story that others can follow along with and actually enjoy reading. The best comments are the ones that tell you WHY you're doing something.
This is a particularly bad kind of comment:
How does this line "loop until condition is met"? The comment oversteps its bounds and only causes the astute reader consternation.
Lastly, this is one of the worst kinds of comments because it's both pointless and redundant:
Proper indentation should make it clear what statement this closing brace terminates. There's no need to harp on about it in a comment.
Again, I realize that this is probably something you've been encouraged to do by your instructor. If not, then you've gone overboard. If it is, then just play along but know that most coding standards call for meaningful comments only. That is, comments that explain why something is being done, not what or how.
The Java compiler permits the brackets to appear after the variable name. This happened early on in the development of the Java compiler because some of the syntax conventions were borrowed from other languages. Today, this is frowned upon and the brackets should immediately follow the type, as in:
Thank you all for the help! And yes, my teacher requires that we comment on at least every other line of code to make sure we understand what we are doing. And thank you, I will put the square brackets immediately following the type from now on.
Mona Smith wrote:And yes, my teacher requires that we comment on at least every other line of code to make sure we understand what we are doing.
That's quite unfortunate. Again, I understand why you might want to do that as a beginner but it's quite ironic that your instructor thinks he/she is "making sure you understand" by telling you to write comments that make the code more difficult to understand.
One thing I'm happy to say though, Mona, is that your code is one of the rare examples we see around here of very well formatted and indented code. Keep up the good work on that. It may seem like a trivial thing but trust us when we say, it's VERY important to format and indent your code properly. So thank you.
In fact, I think that deserves a cow, so have one on me.
And definitely, showing effort to figure things out and fix your code, then sharing what you did deserves praise.