在 java 中打印出一个 ASCII 星号

Print out an ASCII star in java

我正在尝试使用 for 循环打印出下面的图像。我正在使用 for 循环和 if 语句在 Java.

中创建一个 ASCII 星号

我的代码:

public class asciistar {
    public static void main(String[] args) {
        final int X = 9;
        for (int R = 0; R < X; R++) {
            for (int V = 0; V < X; V++) {
                if (R == V || R + V == X - 1 || V == X / 2 || R == X / 2) {
                    System.out.print("* ");
                } else {
                    System.out.print("  ");
                }
            }
        }
    }
}

您永远不会输入新行。您应该在外部循环中插入一个新行: System.out.print("\n");

在“V”for 循环之后和“R”for 循环内部,添加打印行语句以打印出每一行:

System.out.println();

您的代码有效!只需在外循环的末尾添加一个换行符的打印:

public static void main(String[] args) {
    final int X = 9;
    for (int R = 0; R < X; R++) {
        for (int V = 0; V < X; V++) {
            if (R == V || R + V == X - 1 || V == X / 2 || R == X / 2) {
                System.out.print("* ");
            } else {
                System.out.print("  ");
            }
        }
        System.out.println("");
    }
}

结果:

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

您可以使用流中的流而不是循环中的循环,如下所示:

int m = 9;
IntStream.range(0, m).forEach(i -> {
    IntStream.range(0, m).forEach(j -> {
        if (i == j || i + j == m - 1 || i == m / 2 || j == m / 2) {
            System.out.print("* ");
        } else {
            System.out.print("  ");
        }
    });
    System.out.println();
});

输出:

*       *       * 
  *     *     *   
    *   *   *     
      * * *       
* * * * * * * * * 
      * * *       
    *   *   *     
  *     *     *   
*       *       * 
public Cheater() 
{
   EventQueue.invokeLater(new Runnable() 
   {
     @Override
      public void run() 
      {
        try 
        {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          String path = "https://i.stack.imgur.com/xUAW1.png";
          URL url = new URL(path);
          BufferedImage image = ImageIO.read(url);
          JLabel label = new JLabel(new ImageIcon(image));
          JFrame f = new JFrame();
          f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          f.getContentPane().add(label);
          f.pack();
          f.setLocation(200, 200);
          f.setVisible(true);
         } catch (Exception exp) { exp.printStackTrace();}
       }
    });
 }

输出:

4K,跟图片差不多

嘿嘿,还是java中的ASCII星:D

您可以将星星可视化作为平面原点并从-n迭代到n以简化您的代码。

Try it online!

int m = 5;
IntStream.rangeClosed(-m, m)
        .map(Math::abs)
        .peek(i -> IntStream.rangeClosed(-m, m)
                .map(Math::abs)
                .mapToObj(j -> i == 0 || j == 0
                        || i == j ? "* " : "  ")
                .forEach(System.out::print))
        .forEach(i -> System.out.println());

输出:

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

另请参阅:Making an hourglass using asterisks in java