在Java中打印出一个ASCII星星。

huangapple go评论98阅读模式
英文:

Print out an ASCII star in java

问题

以下是您提供的代码的翻译部分:

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("  ");
                }
            }
        }
    }
}
英文:

I am trying to print out the image below using a for loop. I am using for loops and if statements to create an ASCII star in Java.

在Java中打印出一个ASCII星星。

My code:

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

答案1

得分: 2

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

英文:

You never enter a new line. You should insert a new line in the outter loop:
System.out.print(&quot;\n&quot;);

答案2

得分: 2

Your code works! Just add the printing of a newline at the end of the outer loop:

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

Result:

*       *       * 
  *     *     *   
    *   *   *     
      * * *       
* * * * * * * * * 
      * * *       
    *   *   *     
  *     *     *   
*       *       * 
英文:

Your code works! Just add the printing of a newline at the end of the outer loop:

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

Result:

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

答案3

得分: 1

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

输出

在Java中打印出一个ASCII星星。

4K,几乎像一张图片

嘿,Java里还是一个ASCII星星:D

英文:
public Cheater() 
{
   EventQueue.invokeLater(new Runnable() 
   {
     @Override
      public void run() 
      {
        try 
        {
          UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
          String path = &quot;https://i.stack.imgur.com/xUAW1.png&quot;;
          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();}
       }
    });
 }

Output:

在Java中打印出一个ASCII星星。

4K, Almost seems like a pic

hey, still an ASCII star in java : D

答案4

得分: 0

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

System.out.println();

英文:

After the "V" for loop and inside the "R" for loop, add a print line statement to print out each line:

System.out.println();

答案5

得分: 0

你可以使用 一个流中嵌套另一个流 来代替 一个循环中嵌套另一个循环,代码如下:

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

输出结果:

*       *       * 
  *     *     *   
    *   *   *     
      * * *       
* * * * * * * * * 
      * * *       
    *   *   *     
  *     *     *   
*       *       * 
英文:

You can use a stream in a stream instead of a loop in a loop as follows:

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

Output:

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

答案6

得分: 0

你可以将这颗星星*视为平面上的原点,并在从 -nn 的范围内进行迭代,以简化你的代码。

在线尝试!

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

输出:

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

另请参阅:使用 Java 制作一个星号沙漏

英文:

You can visualize the star as the origin on a plane and iterate from -n to n to simplify your code.

Try it online!

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

Output:

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

<sup>See also: Making an hourglass using asterisks in java</sup>

1: https://tio.run/##lVA9T8MwFNzzK54yOYhYXVhSBQbEwFB1KBtieElMatdfil8qIdrfHtzQBIaC4CTLT/fu3kmncI@588KqZjcM0njXEahI8p6k5oE6gYY/WtqM0zJJfF9pWUOtMQRYobTwnkDEmQ@EFL@9kw2YuGXRKG37/ALYtSE7i0@QlsBACTfLmZpzeIe2FffaBdGw3FyDyWbRBG7QsxXStiiwChf2Xogdk5Df/u/uX@9/1z25daWYOmVJKEtYwOEAapx@9E2IytGj4A7SK0ihiA/SXwJfXfeA9ZZt3gIJw11PReFjy5RdaGESj0V8Ofho0JZl2Wf/x@Q4DB8 "Java (OpenJDK 8) – Try It Online"
2: https://stackoverflow.com/a/66790387/15402945 "Making an hourglass using asterisks in java"

huangapple
  • 本文由 发表于 2020年10月23日 00:07:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/64486202.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定