【问题标题】:How to draw the characters in Java [closed]如何在Java中绘制字符[关闭]
【发布时间】:2015-06-06 15:59:51
【问题描述】:

程序要打印符号的矩形的长度和宽度 例如

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

【问题讨论】:

标签: java character symbols


【解决方案1】:

你可以通过你的逻辑来解决这个问题。你可以看到下面的源代码:

import java.util.Scanner;

public class Piramd1 {

    public static void main(String args[]) {

        Scanner conin = new Scanner(System.in);
        System.out.print("How many lines=");
        int n = conin.nextInt();

        for (int r = 1; r <= n; r++) {
            for (int c = 1; c <= n; c++) {

                if (r == 1 || r == n || c == 1 || c == n) {
                    System.out.print("*");
                } else {
                    System.out.print(" ");
                }
            }
           System.out.println();
        }
    }
}

【讨论】:

    【解决方案2】:

    使用逻辑检查迭代为二维数组:

    public static void rectOuter(int length, int width) {
    
        String printStr = "*";
        String seprator = " ";
    
        for (int i = 0; i < length; i++) {
    
            for (int j = 0; j < width; j++)
    
                if (i == 0 || j == 0 || i == length - 1 || j == width - 1)
                    System.out.print(printStr + seprator);
                else
                    System.out.print(seprator + seprator);
    
            System.out.println();
        }
    }
    

    PS:System.out.print 被 StringBuilder 替换

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-08-04
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 2017-02-27
      • 2013-04-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多