Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Objective: Print "cross" or "star" pattern from this post:
- * https://www.facebook.com/groups/java4developers/permalink/1742344979155475/
- * JUnit tests:
- * https://pastebin.com/pVw3CUY0
- * Additional tests, and tweaked for someone else's implementation:
- * https://pastebin.com/bZMmLnQs
- */
- import java.util.stream.IntStream;
- public class AlternateStreamingCrossingStars {
- public static void printCrossingStars(final int n) {
- IntStream.rangeClosed(-n, n).forEach(lineIdx -> { // For each line...
- IntStream.rangeClosed(1, n).forEach(column -> { // For each "T" (multiple characters), left to right...
- final String leadingSpacesOrStars = nOf(column, (lineIdx == 0) ? '*' : ' '); // Spaces -- except for the middle one.
- final char crossBar = (Math.abs(lineIdx) > column) ? ' ' : '*'; // = last character of each "T"
- System.out.print(leadingSpacesOrStars + crossBar); // = all characters for one "T" (on one line)
- });
- if (n > 0) { // (special case for zero input -- of no output)
- System.out.println();
- }
- });
- }
- /**
- * @return a <code>String</code> consisting of <code>count</code> copies of the character, <code>chr</code>.
- */
- private static String nOf(final int count, final char chr) {
- return new String(new char[count]).replace('\0', chr);
- }
- }
Add Comment
Please, Sign In to add comment