无法解码此模式的逻辑

时间:2018-01-05 08:17:55

标签: java logic

我想在用户输入no时打印图案。 ' N'它会打印出如下图案,图案的所有外臂都会包含“N' N' ' - &#39 ;.

    /\
   /  \
  / /\ \
 / /  \ \
   \ \  / /
    \ \/ /
     \  /
      \/

我已经尝试了很多解码,但我无法解码,我确实尝试过很少的东西。 如果有人帮助我或指导我这个逻辑,那将会很有帮助。

int num=4;
int input = num*2;
String[][] arr = new String[input][input];

for (int i = 0; i < input; i++) {
    for (int j = 0; j < input; j++) {
        if (j+1== num - i) {
            arr[i][j] = "/";
            System.out.print(arr[i][j]);
        } else if (j + 1 == num + i + 1) {
            arr[i][j] = "\\";
            System.out.print(arr[i][j]);
        } else {
            arr[i][j] = "-";
            System.out.print(arr[i][j]);
        }
    }
    System.out.println();
}

1 个答案:

答案 0 :(得分:1)

嗯,也许不是世界上最好的解决方案,但是......

public class PatternPrinter {

  public static void main(String[] args) {
    PatternPrinter pp = new PatternPrinter();
    for (String arg : args) {
      pp.printPattern(Integer.parseInt(arg));
    }
  }

  private void printPattern(int outerArms) {
    printUpperArms(outerArms, "/", "\\");
    printLowerArms(outerArms, "\\", "/", " ");
  }

  private void printUpperArms(int arms, String leftArm, String rightArm) {
    int leftspaces = 0;
    int rightspaces = 0;
    int leftArms = 1;
    int rightArms = 1;
    for (int i = 0; i < arms; i++) {
      for (leftspaces = 0; leftspaces < arms - leftArms; leftspaces++) {
      System.out.print(" ");
    }
    for (int j = 0; j < leftArms; j++) {
      System.out.print(leftArm);
    }
    for (int h = 0; h < rightArms; h++) {
      System.out.print(rightArm);
    }
    for (rightspaces = 0; rightspaces < arms - leftArms - rightArms - leftspaces; rightspaces++) {
      System.out.print(" ");
     }
      System.out.println("");
      leftArms++;
      rightArms++;
    }
  }

private void printLowerArms(int arms, String leftArm, String rightArm, String shift) {
  int leftspaces = 0;
  int rightspaces = 0;
  int rightArms = arms;
  int leftArms = arms;
  for (int i = 0; i < arms; i++) {
    System.out.print(" ");
    for (leftspaces = 0; leftspaces < arms - leftArms; leftspaces++) {
    System.out.print(shift);
    }
    for (int j = 0; j < leftArms; j++) {
    System.out.print(leftArm);
    }
    for (int h = 0; h < rightArms; h++) {
      System.out.print(rightArm);
    }
    for (rightspaces = 0; rightspaces < arms - leftArms - rightArms - leftspaces; rightspaces++) {
      System.out.print(" ");
    }
    System.out.println("");
    leftArms--;
    rightArms--;
  }
}