0

I did an exercise in one of my books to get this output. We are supposed to use nested loops and basic java. I could not format the output in here but the code below produces the correct output. I got it to print the correct output but I feel like it is very redundant, mainly with the loops regarding the * and spaces if you have a better method for doing this please share!

private static void printDesign(){
    int astrickStopper = 1;
    int slashStopper = 1;
    for (int lines = 1; lines <= 7; lines++) {
        for (int firstAstrick = 6; firstAstrick >= astrickStopper; firstAstrick--) {
            System.out.print("*");
        }
        for (int spaces = 1; spaces <= slashStopper; spaces++) {
            System.out.print(" ");
        }
        for (int forwardSlash = 6; forwardSlash >= slashStopper; forwardSlash--) {
            System.out.print("//");
        }
        for (int backSlash = 1; backSlash < slashStopper ; backSlash++) {
            System.out.print("\\\\");
        }
        for (int spaces = 1; spaces <= slashStopper; spaces++) {
            System.out.print(" ");
        }
        for (int secondAstrick = 6; secondAstrick >= astrickStopper; secondAstrick--) {
            System.out.print("*");
        }
        astrickStopper = astrickStopper + 1;
        slashStopper = slashStopper + 1;
        System.out.println();
    }
}
Ryan
  • 712
  • 7
  • 21
  • 3
    `the code below produces the correct output` what is the output that it produces? (you can put up an image) – Abubakkar Sep 20 '13 at 04:27
  • Here is the output. [link](http://i.imgur.com/Vi85hSM.png) –  Sep 20 '13 at 04:31
  • Added an image to your post with the output. Says something about needing to be peer reviewed. http://i.stack.imgur.com/3V6x8.png – Ryan Sep 20 '13 at 04:32
  • 1
    Why are you extra-indenting all of the inner `for` loops? That, especially combined with your question, gives the impression you mean them to be nested inside each other. – chrylis -cautiouslyoptimistic- Sep 20 '13 at 04:35
  • I did not intend for them to be inside of each other, I may have misunderstood my AP CS teacher. Should only the nested loops be indented? –  Sep 20 '13 at 04:40
  • They should only be indented for each level deep you go. If your function is the top level of indentation, then the first two variable declarations and the outer for loop will be indented 1 level deep. From there, anything inside of the outer for loop will be indented a second level; all 6 of those for loops should be indented the same amount. Finally, the print statement inside of each inner for loop will also be indented one level deeper than the inner for loops. – Ryan Sep 20 '13 at 04:47

1 Answers1

0

The code you wrote seems like it meets the problem description. You could move the inner loop into a function that outputs a given sequence of characters a certain number of times, then you would just be calling the function 6 times instead of having 6 inner loops.

public void printChars(int count, String chars) {
    for (int i = 0; i < count; i++) {
        System.out.print(chars);
    }
}
Ryan
  • 712
  • 7
  • 21