How to make a loop to print the following pattern?

巧了我就是萌 提交于 2019-12-13 09:29:54

问题


I want to write a program that prints out the following pattern by using loop. Right now I am able to print it by using only system.out.println. I have no idea which loop I should make.

The pattern will be:

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

My code:

  package pettern;

public class Pettern {

    public static void main(String[] args) {
        //initializing
        String tenStar = "* * * * * * * * * *";
        String NineStar = "* * * * * * * * *";
        String EightStar = "* * * * * * * *";
        String SevenStar = "* * * * * * *";
        String SixStar = "* * * * * *";
        String FiveStar = "* * * * *";

        //printing them out
        System.out.println(tenStar);
        System.out.println(NineStar);
        System.out.println(EightStar);
        System.out.println(SevenStar);
        System.out.println(SixStar);
        System.out.println(FiveStar);

    }

}

回答1:


Loop in the range 5 <= n <= 10, and each time print "* " n times. At the end of a loop, print a newline.

for (int i = 10; i >= 5; i--) {
    for (int j = 0; j < i; j++) {
        System.out.print("* ");
    }
    System.out.println();
}

(demo)




回答2:


Try this:

public static void main(String args[])
    {
        for (int i = 10; i >= 5; i--)
        {
            printStars(i);
        }

    }

    private static void printStars(int i)
    {
        for (int j = 1; j <= i; j++)
        {
            System.out.print("*");
        }
        System.out.println();
    }

Output:

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



回答3:


Try this :-

 public class Pattern
{
public static void main(String[] args)
{
    int j=10 ;
    for(int i = 0 ; i < 6 ; i++)
    {
        printStar(j) ;
        System.out.println() ;
        j-- ;

    }




}

static void printStar(int n)
{
    for(int i = 0 ; i < n ; i++)

    System.out.print("*") ;     

}

}



回答4:


You can do this without using two loops by doing this:

for (int i = 10; i >= 5; i--) {
    String stars = new String(new char[i]).replace("\0", "* ");
    System.out.println(stars);
}

Output:

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

Explanation: What's happening here is a String is being created using the specified array of chars, which are all initialized to \0 (the null character). The String at this point contains i null characters. The .replace is replacing the character sequence "\0" with the character sequence "* " and the result is a String consisting of the repeated sequence.




回答5:


java-8 solution:

public static void printPattern(int rows) {
    IntStream.range(0, rows).map(r -> rows - r).forEach(x -> {
        IntStream.rangeClosed(1, rows + x - 2).forEach(y -> {
            System.out.print(" *");
        });
        System.out.println();
    });
}

Usage:

printPattern(6);

Output:

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


来源:https://stackoverflow.com/questions/27195039/how-to-make-a-loop-to-print-the-following-pattern

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!