-1

I'm trying to print a figure that prints 3 spaces then a star, then the next line prints 2 spaces, / and a star, and then 1 space, // and a star and so on. I've got the code to print all the slashes and stars, but I can't figure out how to get the spaces to print with a descending number. This is for an assignment and I have to use nested for loops. Any thoughts? PS I don't want an exact answer (i.e. "type in this code"), I just want a suggestion to try to point me in the right direction.

What I have so far is (H is the scale):

public class Pattern { //program that prints a boxed-in design
  public static final int H = 9;
  public static void main(String[] args) {
    line();
    design();
  }
  public static void line() {
    System.out.print("+");
    for (int d=1; d <=H-2; d++) {
      System.out.print("-");
    }
    System.out.println("+");
  }
  public static void design() {
    top();
  }
  public static void top() {
    for (int row = 1; row <=H-2; row++){
      System.out.print("|");
      for (int sp =3; sp>=1; sp--){
        System.out.print(" ");
      }
      for (int fsl=1; fsl<=row-1; fsl++){
        System.out.print("/");
      }
      for (int star=1; star<=1; star++){
        System.out.print("*");
      }
      for (int bsl=1; bsl<=row-1; bsl++){
        System.out.print("\\");
      }
      System.out.print("|");
      System.out.println();
    }
  }
}
  • 3
    What have you tried? When asking about homework problems, you must show what you have done so far. Please read [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve) – Orin Sep 23 '16 at 14:18
  • 1
    I would suggest using two loops. The first/outer loop from `n` to `0`. And the inner loop from `0` to `n`. Use `System.out.print` to print in the inner loop without creating a newline. Good luck. – CollinD Sep 23 '16 at 14:18
  • Paste the code here as plain text in proper formatting. – progyammer Sep 23 '16 at 14:37
  • Nice to have the code in the question, makes it much more convenient to refer to it in a comment or answer. Thx. – Ole V.V. Sep 23 '16 at 15:00

2 Answers2

0

So if I understand correctly, your row variable goes from 1 through 4, and you want 3 spaces in row 1, 2 spaces in row 2, 1 space in row 3 and 0 spaces in row 4? I suggest you should be able to find an arithmetic expression (like you have already found row-1 for the number of slashes) to give you the correct number of spaces on each line/row. If I need to say more, feel free to add a comment to this answer.

Ole V.V.
  • 81,772
  • 15
  • 137
  • 161
  • This was really useful and I ended up using the expression "(-1)*row + 4" Which works when the scale is 9, now I just have to figure out what expression to use when the scale changes – Aquiel Womble Sep 23 '16 at 22:17
  • Glad it helped! You can do a little simpler with `4 - row`. For the scale (`H`) changing, I believe you have to put something in instead of 4 that depends on `H`. Happy coding. PS You may mark the answer correct by clicking the grey tick mark to the left of it if and when you think. – Ole V.V. Sep 24 '16 at 05:04
0
package com.acme;

import java.util.stream.IntStream;
import java.util.stream.Stream;

import static java.util.Comparator.reverseOrder;

public class PrintIt {

    public static void main(String[] args) {
        printSpacesWithStar(10, Order.ASC);
        printSpacesWithStar(10, Order.DESC);
    }

    private static void printSpacesWithStar(int numbers, Order order) {
        Stream<Integer> streamOfInt = IntStream.rangeClosed(1, numbers)
                .boxed();

        switch (order) {
            case ASC:
                streamOfInt
                        .sorted(reverseOrder())
                        .forEach(Test::printingLogic);
                break;
            case DESC:
                streamOfInt
                        .forEach(Test::printingLogic);
                break;
        }
    }

    private static void printingLogic(Integer currentValue) {
        for (int k = 1; k < currentValue; k++) {
            System.out.print(" ");
        }
        System.out.println("*");
    }

    enum Order {
        ASC, DESC;
    }


}
Dinidu Hewage
  • 2,169
  • 6
  • 40
  • 51
  • The code is fine. However the OP specifically said “I don't want an exact answer (i.e. "type in this code")”. – Ole V.V. Sep 23 '16 at 16:32