0

I need help making a program that lets you specify the dimensions of an arrow in console.

import java.util.Scanner;

public class Project6 {
  public static void main(String[] args) {
    int height = 0;
    int width = 0;
    boolean run = true;
    Scanner scanner = new Scanner(System.in);
    int rows = 0;
    while (run) {
      System.out.println("Please Specify The Demensions of Your Arrow:");
      System.out.print("\tHeight: ");
      if (!(scanner.hasNextInt())) {
        System.out.println("Please Enter an Integer.");
      } else {
        height = scanner.nextInt();
        if (!(height > 0)) {
          if (height == -1) {
            break;
          } else {
            System.out.println("Please Enter A Positive Integer");
          }
        } else {
          System.out.print("\tWidth: ");
          if (!(scanner.hasNextInt())) {
            System.out.println("Please Enter An Integer");
          } else {
            width = scanner.nextInt();
            if (!(width > 0)) {
              if (width == -1) {
                break;
              } else {
                System.out.println("Please Enter A Positive Integer");
              }
            }
          }
        }
        if (width % 2 == 0) {
          System.out.println("Width is an Even Number");
        } else {
          System.out.println("Width is odd");
          for (int numRow = width; numRow >= 1; numRow -= 2) {
            System.out.println(numRow);
            rows++;
          }
          System.out.println("Number of Rows: " + rows);
        }
        /*if(height % 2 == 0) {
          System.out.println("height is an Even Number");
        } else {
          System.out.println("height is odd");
          for(int numCol = height; numCol >= 1; numCol -=2) {
            System.out.println(numCol);
          }
        }*/
      }
    }
  }
}

I put a part in comments because I wasn't sure if I wanted to keep it or not. This is as far as I could get. I have no clue where to go from here. Essentially what I'm trying to do is, for example: you input height: 10 and width: 9 and it looks like this:

    *
   ***
  *****
 *******
*********
   ***
   ***
   ***
   ***
   ***
Community
  • 1
  • 1
Evan
  • 1
  • 1

2 Answers2

0

I would suggest the following:

First write a method that prints this: (you only consider the height of the triangle)

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

Then modify this method, so you obtain this: (now you also consider the width) All you have to do is think of how many spaces you have to add at each line, before printing the stars.

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

(you can do it in like 7 lines of code)

And now add a total number of arrowHeight - triagleHeight of this:

***
DeiAndrei
  • 947
  • 6
  • 16
0

You can print an arrow as a triangle and its prop. In this example you have to to specify three parameters: triangle height, prop height and prop width:

/**
 * Prints an arrow in a console with the specified dimensions
 *
 * @param th triangle height
 * @param ph prop height
 * @param pw prop width
 */
public static void printArrow(int th, int ph, int pw) {
    // triangle width
    int tw = 2 * th - 1;
    // prop offset
    int po = tw > pw ? ((tw - pw) / 2 * 2 - (pw % 2 > 0 ? 1 : 0)) : 0;
    // output
    System.out.println("Triangle: h=" + th + ", w=" + tw);
    System.out.println("Prop: h=" + ph + ", w=" + pw);
    // triangle
    IntStream.rangeClosed(1, th)
            .mapToObj(i -> IntStream.rangeClosed(-th, i)
                    .map(Math::abs)
                    .map(j -> j = i - j)
                    .filter(j -> j != 0)
                    .mapToObj(j -> j > 0 ? "*" : " ")
                    .collect(Collectors.joining(" ")))
            .forEach(System.out::println);
    // prop
    IntStream.range(0, th)
            .mapToObj(i -> " " .repeat(po) + " *" .repeat(pw))
            .forEach(System.out::println);
}
// test
public static void main(String[] args) {
    printArrow(3, 3, 2);
    printArrow(4, 4, 3);
}

Output:

Triangle: h=3, w=5
Prop: h=3, w=2
    *
  * * *
* * * * *
   * *
   * *
   * *
Triangle: h=4, w=7
Prop: h=4, w=3
      *
    * * *
  * * * * *
* * * * * * *
    * * *
    * * *
    * * *
    * * *

See also:
How to get following format output?
Pascal's triangle 2d array - formatting printed output