简体   繁体   English

如何在 Java 中打印降序/升序的空格数?

[英]How to print a descending/ascending number of spaces in Java?

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.我正在尝试打印一个图形,先打印 3 个空格,然后是一个星号,然后下一行打印 2 个空格,/ 和一个星号,然后是 1 个空格,// 和一个星号等等。 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.这是一个作业,我必须使用嵌套的 for 循环。 Any thoughts?有什么想法吗? PS I don't want an exact answer (ie "type in this code"), I just want a suggestion to try to point me in the right direction. PS我不想要一个确切的答案(即“输入此代码”),我只想要一个建议来尝试将我指向正确的方向。

What I have so far is (H is the scale):到目前为止我所拥有的是(H 是比例):

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();
    }
  }
}

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?因此,如果我理解正确,您的row变量从 1 到 4,并且您想要第 1 行 3 个空格、第 2 行 2 个空格、第 3 行 1 个空格和第 4 行 0 个空格? 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.我建议您应该能够找到一个算术表达式(就像您已经找到row-1的斜杠数)来为每行/行提供正确的空格数。 If I need to say more, feel free to add a comment to this answer.如果我需要说更多,请随时在此答案中添加评论。

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; } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java中的升序和降序数 - Ascending and Descending Number Order in java 在Java中按降序或升序打印整数的范围 - print range of integers in descending or ascending order in java 如何按降序打印HashMap值,但如果两个或多个值相等,则按键升序打印? (爪哇) - How to print HashMap values in descending order, but if two or more values are equal, print them by keys ascending? (JAVA) 升序和Java降序 - Ascending order and Descending Java 如何使用Java中的字符串或列表检查数字的位数是升序还是降序? - How can I check if the digits of a number are ordered ascending or descending whitout using strings or lists in Java? Java:如何在单个文本文件中打印升序列,然后在该列的旁边打印同一组整数,除了按降序排列 - Java : How do I print an ascending column and next to that column the same set of integers except in descending order all in one single text file Java数组降序排列并打印其元素编号 - Java array sort descending and print their element number 如何将此插入排序从升序更改为降序 - java - how to i change this insertion sort from ascending to descending - java 如何在Java中按降序打印结果? - How to print the result in descending order in java? 按升序和降序对数组的 LinkedHashmap 进行排序 - Java - Sort LinkedHashmap of Array in ascending and descending order - Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM