【问题标题】:Printing an arrow in a console with the specified dimensions在控制台中打印具有指定尺寸的箭头
【发布时间】:2014-09-11 13:04:27
【问题描述】:

我需要帮助制作一个程序,让您在控制台中指定箭头的尺寸。

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);
          }
        }*/
      }
    }
  }
}

我在 cmets 中加入了一部分,因为我不确定我是否想保留它。这是我所能得到的。我不知道从这里去哪里。基本上我想做的是,例如:你输入height: 10width: 9,它看起来像这样:

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

【问题讨论】:

    标签: java string ascii-art


    【解决方案1】:

    我建议如下:

    首先编写一个打印这个的方法:(你只考虑三角形的高度)

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

    然后修改这个方法,所以你得到这个:(现在你还考虑宽度)你所要做的就是在打印星星之前考虑在每行添加多少空格。

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

    (你可以用 7 行代码完成)

    现在添加一个总数的arrowHeight - triagleHeight 这个:

    ***
    

    【讨论】:

      【解决方案2】:

      您可以将箭头打印为 triangle 及其 prop。在此示例中,您必须指定三个参数:三角形高度道具高度道具宽度

      /**
       * 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);
      }
      

      输出:

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

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-11-17
        • 2012-10-05
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-31
        • 1970-01-01
        相关资源
        最近更新 更多