Print Out the Alphabet
Back to the Program-A-Day homepage
ProblemPrint out the entire lower case letters of the alphabet. Use the following guidelines when coming up with a solution:
|
...By Students"Comments are very important in documenting how your program works. They make it easier for the marker to understand and grade your work and it also makes it easier for someone else to understand it. Commenting as you code is a very important skill that you should acquire." "I usually don't comment as I code but I have been trying to change that. For small first year assignments not commenting as you code is okay because it is easy to add afterwords. But when you are working on later assignments and you end up having 600+ lines of code it gets quite a bit harder to add them at the end. I remember having to spend a couple of hours after I finished coding an assignment in order to add in all of my comments; I found that very stressful and not enjoyable. It is easier and more enjoyable to document code as you write it." | |
---|---|---|
SolutionWhen printing out the alphabet you are going to need to know the number of letters to stop the for loop at the right time. //final indicates that this is a constant //Uppercase also indicates that this is a constant final int NUMLETTERS = 26;
char letter = 'a'; //initialize the first letter
char letter = 'a'; letter++; //will increment the letter System.out.println("letter = " + letter) letter = b
for(int i = 0; i < NUMLETTERS; i++) { //code to be added here later }
for(int i = 0; i < NUMLETTERS; i++) { System.out.println(letter);//print out the current letter letter++;//increase the letter to get the next } For the complete final code you can look under the code heading. | ||
Code |