【问题标题】:Is it possible to return the cursor to nth previous line?是否可以将光标返回到前 n 行?
【发布时间】:2014-08-29 00:10:22
【问题描述】:

我要打印图案:

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

代码必须在与给定输入相关的列中打印* 的数量。

在此示例中,提供的输入是 {2,5,1,4,3}

【问题讨论】:

  • 不,不可能
  • 有没有办法使用多维数组来做到这一点?
  • 如果您的终端支持,ANSI escape codes 是可能的。这应该适用于大多数 Linux 和 Mac 终端仿真器,但不适用于 Windows CMD。我不确定 IDE 中的控制台是否支持它们。
  • Java bar chart method的可能重复
  • 您使用的是什么操作系统和什么编辑器?

标签: java console-application


【解决方案1】:
public void printStars(final int[] inputArray) {

    // get the maximum value from the array
    int max = 0;
    for(final int value : inputArray) {
        if(value > max) {
            max = value;
        }
    }

    for(int row = max; row >= 1; row--) {
        for(final int value : inputArray) {
            if(value >= row) {
                System.out.print("* ");
            } else {
                System.out.print("  ");
            }
        }
        System.out.println();
    }

}

【讨论】:

  • 感谢您的回复:)
  • 使用windows操作系统和记事本
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-10-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多