Java prints the positive pyramid, inverted pyramid and "Water Shadow" pyramid, and java Water Shadow

Source: Internet
Author: User

Java prints the positive pyramid, inverted pyramid and "Water Shadow" pyramid, and java Water Shadow
Java prints the positive pyramid, inverted pyramid, and "Water Shadow" Pyramid

-------~~~~~~~~~~

TeenagerThere is nothing to worry about. When I first started java, what I did was simply print out some interesting figures, and I wrote such a demo of the print pyramid, I will review my original feelings and share it with the beginners of java to cultivate their interest in programming. <(^-^)>. Pyramid Pattern1. Problem analysis we all know that the output is output from the leftmost end. Here, the first Asterisk is in the middle. This is because there are many spaces before the asterisk. When we use question marks to represent spaces, the actual output result should be in the form of Figure 1.2. 1.1 1.2 analysis, we can see that the printed pyramid has five rows, each row will print a few spaces, and then print a few more★So that we can know how to confirm the framework of the program. Public static void Triangle () {for (int I = 1; I <= 5; I ++) {// loop 5 times, print 5 rows // print the number of spaces // print★Number System. Out. Pringln (); // print the number of trips} now that we know the number of required rows, we need to confirm the number of spaces and★The number, let's look at its law. 2. Search for spaces and★Number.

, We can find that there are four spaces in the 1st rows, three in the 2nd rows, and two in the 3rd rows ,......, The number of spaces in the last row is 0, and the number of asterisks is 1 in 1st, 3 in 2nd, and 5 in 3rd ,......, Each row increments by 2 until the number of the last planet is 9. To sum up the data, we can get the rule as shown in Table 1.1.

Rule of space and stars
Number of rows Number of spaces 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
Rule Minus 1 5-number of rows Add 2 in sequence Number of rows * 2-1

According to the figure, we can find this rule, so it's easy.

3. determine the number of spaces.

Because the number of spaces in each line has a "5-number of rows" rule. Therefore, the number of spaces in line I is 5-i. Therefore, we only need to print 5-I spaces.

    

Public static void Triangle () {for (int I = 1; I <= 5; I ++) {// print 5 rows 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} note that when printing the number of spaces, do not enter println (); this will wrap the line. 4. print the number of stars because each planet number has a "Number * 2-1" rule. So in line I, the number of stars is 2 * I-1. So we just need to print out 2 * I-1 asterisks. Public static void Triangle () {for (int I = 1; I <= 5; I ++) {// print 5 rows for (int j = 1; j <= 5-i; j ++) {System. out. print (""); // number of printed spaces} for (int k = 1; k <= 2 * I-1; k ++) {System. out. print ("★"); // Print★Number} System. Out. Pringln (); // print the number of trips} OK. The program has been analyzed and our pyramid has been printed. I have reviewed it. Have you understood it? O ( ̄) d. The idea of the pyramid is like this. The idea of the inverted pyramid is exactly the same. I will not write it here. I believe that you can solve it well with the smart one, I simply put the rule of inverted pyramid space and number of stars and a demo I wrote below.
Number of rows Number of spaces Stars
1 0 1-1 9 5*2-1
2 1 2-1 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
Rule Increments by 1 Rows-1 Decrease by 2 in sequence Number of rows * 2-1 (reverse)
* ** Click it **: For more information about the diamond, see my javaScript script to print a straight line. O (partition _ partition) O well! My example1. Here is a demo I wrote. You can refer to it.

Package com. javase. demo;

Import java. util. collections;

 

/**
* Pyramid
* @ Author Mr. Zhang
*
*/
Public class Pyramid {

Static pipeline input = new pipeline (System. in );
/**
* ***** Print the pyramid *****
* 1. determine the number of pyramid rows.
* 2. Check the number of spaces.
* 3. Confirm the number of stars
* @ Param args
*/
Public static void main (String [] args ){

Entrance ();

}

/**
* Entry
*/
Public static void entrance (){
System. out. println ("Please select (0 -- positive pyramid, 1 -- inverted pyramid, 2 -- 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 function is not yet complete! ");
Break;
Default:
System. out. println ("enter the correct option! ");
Entrance ();
Break;
}
} Else if (! Select. equals (0) |! Select. equals (1) |! Select. equals (2 )){
NullSuccess ();
}
}

/**
* Print the positive pyramid
* @ Param input
*/
Public static void uprightPyramid (){
System. out. println ("Enter the number of rows :");
String row = input. nextLine ();

If (isNumber (row )){
Int rows = Integer. parseInt (row );
For (int I = 1; I <= rows; I ++) {// number of rows that are input cyclically,

For (int j = 1; j <= rows-I; j ++) {// output the space of each row in the loop
System. out. print ("");
}

For (int k = 1; k <= 2 * I-1; k ++) {// output★
System. out. print ("★");
}

System. out. println ();
}
System. out. println ("Print finished, thread ended ");
} Else {
NullSuccess ();
}

}
/**
* Print inverted pyramid
*/
Public static void fallPyramid (){
System. out. println ("Enter the number of rows :");
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 ++) {// print the number of spaces
System. out. print ("");
}

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

System. out. println ();
}

System. out. println ("Print finished, thread ended ");
} Else {
NullSuccess ();
}

}

/**
* Determines whether a number is used.
* @ 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! ");
}

}

Related Article

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.