-1

For this class assignment I need to make a program print the letter E like this:

***
*
***
*
***

I need to make this using nested loops. Here is what I have so far;

public class LetterE
{
    public static void main(String args[])
    {


        final int NUM_ACROSS = 3;   // Number of asterisks to print across.
        final int NUM_DOWN = 5;     // Number of asterisks to print down.

        int row;    // Loop control for row number.
        int column; // Loop control for column number.

        // This is the work done in the detailLoop() method
        // Write a loop to control the number of rows.
                for(row = 1; row <= NUM_DOWN; row++) {
                    if(row == 1 || row == 3 || row == 5 || row == NUM_DOWN)
                for(column = 1; column <= NUM_ACROSS; ++column){
                    if(column == 2 || column == 4 || column == NUM_ACROSS)
                   // Write a loop to control the number of columns
                // Decide when to print an asterisk in every column.   
            System.out.print("*");         
            // Decide when to print asterisk in column 1.
            System.out.print("*");
            // Decide when to print a space instead of an asterisk.
            System.out.print(" "); 
        // Figure out where to place this statement that prints a newline.
        System.out.println();
                }
                }
                // This is the work done in the endOfJob() method
        System.exit(0); 
    } // End of main() method.

} // End of LetterE class.

Unfortunately, it just prints

**
*
**
*
**

What am I mising? Thanks for the help.

PM 77-1
  • 12,933
  • 21
  • 68
  • 111
Chad Bowman
  • 13
  • 1
  • 5
  • 1
    Welcome to Stack Overflow! [What does your step debugger tell you?](http://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems) –  Oct 29 '17 at 00:09

2 Answers2

1

It seems to me that you are overcomplicating this. All you need to do is to alternate printing three stars and one star, with odd lines (1, 3, 5) getting three stars and even lines (2, 4) getting one star.

for (int i=0; i < 5; ++i) {
    String line = i % 2 == 0 ? "***" : "*";
    System.out.println(line);
}

If you also had to print other letters, then we might want to change our design. In that case, we could create helper methods which print each letter. Or, better yet, create helper methods to generate portions of letters. But the above answer seems reasonable based on the requirements you gave us.

Demo

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
0

Here is what you are missing (marked it with a comment).

public class LetterE
{
    public static void main(String args[])
    {


        final int NUM_ACROSS = 3;   // Number of asterisks to print across.
        final int NUM_DOWN = 5;     // Number of asterisks to print down.

        int row;    // Loop control for row number.
        int column; // Loop control for column number.

        // This is the work done in the detailLoop() method
        // Write a loop to control the number of rows.
        for(row = 1; row <= NUM_DOWN; row++) {
            if(row == 1 || row == 3 || row == 5 || row == NUM_DOWN)
                for(column = 1; column <= NUM_ACROSS; ++column) {
                    if (column == 2 || column == 4 || column == NUM_ACROSS)
                        // Write a loop to control the number of columns
                        // Decide when to print an asterisk in every column.
                        System.out.print("*");
                  } // <<< here it is, you misplaced one curly bracket (delete extra one before System.exit and you're good to go)
                    // Decide when to print asterisk in column 1.
                    System.out.print("*");
                    // Decide when to print a space instead of an asterisk.
                    System.out.print(" ");
                    // Figure out where to place this statement that prints a newline.
                    System.out.println();
                }
        // This is the work done in the endOfJob() method
        System.exit(0);
    } // End of main() method.

} // End of LetterE class.
Yevgen
  • 1,576
  • 1
  • 15
  • 17