1

I have to write a program that takes a command-line argument n and prints out a pattern with alternating spaces and asterisks (as demonstrated below). Use at least two nested for loops and a constructor to achieve the pattern (an image is shown below of how it's suppose to look).

This is the code I have tried already and no luck. I understand how to do this with a single for loop, but not a nested. I am also unsure how to integrate a constructor with this program.

This is how the image should look: * * * *
                                    * * * *
                                   * * * *
                                    * * * *
public class Box {
     public static void main(String[] args) {

         for (int i=1; i<2; i++) {
             System.out.println("* " + "* " + "* " + "* ");

             for (int j=0; j<i; j++) {
                 System.out.print(" *" + " *" + " *" + " *");

             }
         }
     }
 }
2
  • You aren't using a constructor nor a command line argument n. Commented Feb 12, 2012 at 19:54
  • Jeffrey any idea how to include a constructor or a command line. I kinda feel like it's irrelevant in this problem. Commented Feb 12, 2012 at 20:17

4 Answers 4

2

I guess this is a homework problem, so I won't give you any code :) Your issue here is you are printing out an entire row with both the outer loop and the inner loop. Use the outer loop to draw each row and use the inner loop to draw each asterisk in each row. So, the outer loop is for rows, the inner loop for columns.

Sign up to request clarification or add additional context in comments.

1 Comment

Ok, I see what you're saying here. That makes a little more sense.
1

Slight modification to Bohemian's answer. The outer for-loop is responsible for printing the rows. The inner loop prints the repeated characters on each row. The constructor simply sets the n field, which controls how many rows you print out. The main method creates a new object and invokes its only method.

public class Box {

private static int n; 

public Box(int n){
    this.n = n;
}

public static void doMagic() {
    for (int row = 0; row < n; row++) {
        if(row%2==1)
            System.out.print(" ");
        for (int col = 0; col < n; col++) {
            System.out.print("* ");
        }
        System.out.println();
    }
}
   public static void main(String[] args) {
    new Box(4).doMagic();
 } 
}

2 Comments

Thanks reseter. It makes a lot more sense when I think of one loop for the row and then another for the columns
doMagic() didn't have to be static. Unless you forgot to remove the static modifier after testing/calling it from the main method.
0

In the outer for loop you can control how many rows you will print and select if you´ll print "* " or " *". In the inner loop you´ll print the selected string as many times as columns you have.

Comments

0
  • Give reasonable names to your loop variables.
  • Think about what each iteration for each type should do

Try this:

public static void main(String[] args) {
     for (int row = 0; row < 4; row++) {
         // Not sure if you really meant to indent odd rows. if not, remove if block
         if (row % 2 == 1) {
            System.out.print(" "); 
         }
         for (int col = 0; col < 4; col++) {
             System.out.print("* ");
         }
         System.out.println();
     }
 }

output:

* * * * 
 * * * * 
* * * * 
 * * * * 

2 Comments

I think the odd-numbered rows are supposed to be indented
@reseter oh - i jut thought his formatting was off - fixed code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.