Java print positive pyramid, inverted pyramid and "Water Shadow" pyramid

Source: Internet
Author: User

Java print positive pyramid, inverted pyramid and "Water Shadow" pyramid

-------- original article, to reprint, please indicate the source

little Boy .Nothing to do, think of their own beginner Java when the classic is to print out some interesting graphics, the whim of their own write a print pyramid of the demo, their own review of the original feeling, but also shared to beginners Java fellow, together to cultivate the interest of programming < ( -^) >. Pyramid pattern 1, problem analysis we all know that when the printout is printed, it is output from the leftmost side, and here the first asterisk is in the middle. This is actually because there are many spaces before the asterisk.              When we use a question mark to denote a space, the actual output should be the form of Figure 1.2. 1.1 1.2 Analysis, we can see this print out the pyramid has 5 lines, each line will print a few spaces, and then print a few ★ number, so that we know how to confirm the framework of the program. Public Static voidTriangle () { for(inti = 1;i <= 5;i++) {//Cycle 5 times, print 5 lines                          //number of spaces to print//Print ★ NumberSystem. out. PRINGLN ();//Print travel numberNow that we know the number of rows we need, we need to confirm the number of spaces and the number of ★ numbers, and we'll look at the rules. 2, look for spaces and the law of ★ number.

, we can see that there are 4 spaces in line 1th, line 2nd is 3, line 3rd is 2, ..., each line is decremented until the last line of space is 0, and the number of asterisks is 1, the 2nd line is 3, the 3rd line is 5, ..., each line increments by 2 until the last number of planets is 9. Summing up the data, we can get the pattern shown in table 1.1.

the laws of space and stars
Number of rows Number of spaces Number of stars
1 4 5-1 1 1*2-1
2 3 5-2 3 2*2-1
3 2 5-3 5 3*2-1
4 1 5-4 7 4*2-1
5 0 5-5 9 5*2-1
Law Minus 1 in turn. 5-Number of rows Add 2 in turn Number of rows *2-1

According to the figure we can find this pattern, then it is not easy.

3, determine the number of spaces

Because the number of spaces per line has the rule of "5– rows". So in line I, the number of spaces is 5–i. So we just have to print out a 5–i space.

    

public static void Triangle () {for (int i = 1;i <= 5;i++) {//Loop 5 times, print 5 lines for (int j = 1;j <= 5-i;j++     ) {System.out.print (""); Print the number of spaces}//Print ★ number System. out. PRINGLN (); Print the number of trips}} pay attention to print the number of spaces do not lose to println (); it will be wrapped. 4, the number of printed stars has a "line number *2–1" rule for each planet. So on line I, the number of asterisks is 2*i–1.            So all we have to do is print 2*i–1 an asterisk. public static void Triangle () {for (int i = 1;i <= 5;i++) {//Loop 5 times, print 5 lines for (int j = 1;j <= 5-i;j++     ) {System.out.print (""); Print the number of spaces} for (int k = 1; k <= 2 * i-1;k++) {System.out.prin    T ("★"); Print ★ Number} System. out. PRINGLN (); Print travel Number}} OK, the program to this analysis is complete, and our pyramid is also printed out, I reviewed, you understand? O ( ̄▽ ̄) d is the idea of the pyramid, then the corresponding inverted pyramid of thinking and this is the same, I do not write in this one by one, I believe that the smart you can be a good solution, I will simply put down the pyramid of space and the number of stars and I wrote a demo put below it.
Number of rows Number of spaces Number of stars
1 0 1-1 9 5*2-1
2 1 0-s 7 4*2-1
3 2 3-1 5 3*2-1
4 3 4-1 3 2*2-1
5 4 5-1 1 1*2-1
Law Increment by 1 Number of rows-1 Decrement by 2 in turn Number of rows *2-1 (reverse)
DA * * * *: About the diamond, you can refer to my essay JavaScript print straight inverted line to do oh. O (∩_∩) o well! My example 1, the following is a demo I wrote, we can refer to see OH.

Package Com.javase.demo;

Import Java.util.Scanner;

/**
* Pyramid
* @author Mr.zhang
*
*/
public class Pyramid {

Static Scanner input = new Scanner (system.in);     
/**
* * * * * * * * * * * * * * * * * * * * * * * * 1, determine pyramid lines
* 2, confirm number of spaces
* 3, confirm stars
* @param args
*/
public static void Main (string[] args) {

entrance ();

}

/**
* Entry Entry
*/
public static void entrance () {
SYSTEM.OUT.PRINTLN ("Please select (0--pyramid, 2--Pyramid, Diamond Pyramid)");
String select = Input.nextline ();

if (Isnumber (select)) {
int selectint = Integer.parseint (select);

Switch (selectint) {
Case 0:
Uprightpyramid ();
Break
Case 1:
Fallpyramid ();
Break
Case 2:
System.out.println ("This feature is not yet perfect! ");
Break
Default
System.out.println ("Please enter the correct option!") ");
Entrance ();
Break
}
}else if (!select.equals (0) | |!select.equals (1) | |!select.equals (2)) {
Nullsuccess ();
}
}

/**
* Print Positive Pyramid
* @param input
*/
public static void Uprightpyramid () {
System.out.println ("Please enter the number of lines:");
String row = Input.nextline ();

if (Isnumber (row)) {
int rows = Integer.parseint (row);
for (int i = 1;i <= rows;i++) {//The number of rows entered in the loop,

for (int j = 1;j <= rows-i;j++) {//output loop space per line
System.out.print ("");
}

for (int k = 1;k <= 2 * i-1;k++) {//output loop per line ★
System.out.print ("★");
}

System.out.println ();
}
System.out.println ("Print complete, Thread end");
}else{
Nullsuccess ();
}

}
/**
* Print Inverted Pyramid
*/
public static void Fallpyramid () {
System.out.println ("Please enter the number of lines:");
String row = Input.nextline ();
if (Isnumber (row)) {
int rows = Integer.parseint (row);

for (int i = Rows;i >= 1;i--) {
for (int j = 0;j < rows-i;j++) {//number of spaces to print
System.out.print ("");
}

for (int k = 0;k < I * 2-1;k++) {//Print ★ Number
System.out.print ("★");
}

System.out.println ();
}

System.out.println ("Print complete, Thread end");
}else{
Nullsuccess ();
}

}

/**
* Judging whether it is a number
* @param str
* @return
*/
public static Boolean isnumber (String str) {
Boolean OK = false;
if (null! = str && str.matches ("^[0-9]")) {
return true;
}
return OK;
}

/**
* Call Error results
*/
public static void Nullsuccess () {
System.out.println ("You are not entering numbers, go over the waves, disobedient children!") ");
}

}

Java print positive pyramid, inverted pyramid and "Water Shadow" pyramid

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.