1

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.

enter image description here

My code:

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("  ");
                }
            }
        }
    }
}
exoad
  • 146
  • 2
  • 13

6 Answers6

2

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

Thomas
  • 401
  • 1
  • 6
  • 11
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:

*       *       * 
  *     *     *   
    *   *   *     
      * * *       
* * * * * * * * * 
      * * *       
    *   *   *     
  *     *     *   
*       *       * 
Community
  • 1
  • 1
CryptoFool
  • 21,719
  • 5
  • 26
  • 44
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();}
       }
    });
 }

Output:

enter image description here

4K, Almost seems like a pic

hey, still an ASCII star in java : D

aran
  • 10,978
  • 5
  • 39
  • 69
0

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

System.out.println();

Thomas M
  • 192
  • 1
  • 1
  • 10
0

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

Output:

*       *       * 
  *     *     *   
    *   *   *     
      * * *       
* * * * * * * * * 
      * * *       
    *   *   *     
  *     *     *   
*       *       * 
0

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 -> IntStream.rangeClosed(-m, m)
                .map(Math::abs)
                .mapToObj(j -> i == 0 || j == 0
                        || i == j ? "* " : "  ")
                .forEach(System.out::print))
        .forEach(i -> System.out.println());

Output:

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

See also: Making an hourglass using asterisks in java