简体   繁体   中英

How to print star pattern in java

I tried to find any sequence or formula for below star pattern but i did not found, so simply tried below

public class MyClass {
    public static void main(String args[]) {


        System.out.println("....*....");
        System.out.println("...***...");
        System.out.println("*********");
        System.out.println(".*******.");
        System.out.println("*********");
        System.out.println("...***...");
        System.out.println("....*....");

    }
}

Here "." means " " space character. Can we print this star using loop ?

Not sure what you mean, but if you really want a loop, here is a solution!

public class Star {

    public static void main(String[] args) {

        int[] dots = new int[] {4, 3, 0, 1, 0, 3, 4};
        int width= 9;

        for (int i = 0; i < dots.length; i++) {
            for (int j = 0; j < width; j++) {
                if (j < dots[i] || j > width - dots[i] - 1) {
                    System.out.print(".");
                } else {
                    System.out.print("*");
                }
            }
            System.out.println();
        }

    }
}

Hope It Helps! This is Easy to understand for Low Class Students Rather than Creating Arrays

class starPattern {
    public static void main(String []args) {
        for(int i=1;i<=7;i++) {
            for(int j=1;j<=9;j++) {
                if(i==3 || i==5 || j==5 || (i==4 && j>1 && j<9) || ((i==2 || i==6) && (j>3 && j<7))) {
                    System.out.print("*");
                }else{
                    System.out.print(" ");
                }
            }
            System.out.println();
        }
    }
}

My Computer Teacher Check it and it was Correct Method!

A Brief Description About this Pattern


1↓  1→  2   3   4   5   6   7   8   9
2                   *               
3               *   *   *           
4   *   *   *   *   *   *   *   *   *
5       *   *   *   *   *   *   *   
6   *   *   *   *   *   *   *   *   *
7               *   *   *           
8                   *               

I'm using Java 11. You should be able to draw any enantiomorphic pattern of asterisks. Read the comments for detailed explanation.

import java.util.Arrays;
class Star {
    public static void main(String[] args) {
        int[] stars = new int[] { 1, 3, 9, 7, 9, 3, 1 }; // asterisks per line
        int max = Arrays.stream(stars).max().getAsInt(); // max asterisks in any line
        for (int i = 0; i < stars.length; i++) { // prints the asterisks for a given row, and pads it left and right with spaces
            System.out.print(" ".repeat((max - stars[i]) / 2) + "*".repeat(stars[i]) + " ".repeat((max - stars[i] + 1) / 2));
    }
}

Yes, it can be done using loops and should not be done via sout statements. Also seems like you are a beginner in programming, so I will suggest you to try out logic for these kind of questions yourself. It will help in your growth.

Just try out line by line. These questions generally need 2 loops, one to move in vertical direction and other to move in horizontal direction. You can think it of as ai*j matrix( eg 3X3, 4X4).

So try it out yourself start with some basic one's. If you still need solution just ping me but please try it on your own first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM