It is the true nature of the struggle to do a good deed to be held up by duty. --Balzac
The content of this lecture: 100 cases commonly used in Java
Example 1: Printing pyramids
public class T {public static void main (string[] args) {int lay=10;for (int i=1;i<=lay;i++) {///presentation layer for (int j=1;j<= lay-i;j++) {//Find out the space law System.out.print ("");} for (int k=1;k<=i*2-1;k++) {//Find out * regular System.out.print ("*");} System.out.println ();}}}
Results:
* *** ***** ******* ********* *********** ************* *************** ************ ************************
Example 2: Print an empty pyramid
public class T {public static void main (string[] args) {int lay=10;for (int. i=1;i<=lay;i++) {for (int j=1;j<=lay-i;j++ ) {System.out.print ("");} for (int k=1;k<=i*2-1;k++) {if (i==1| | I==lay) {//Determine if the layer is a top-level or low-System.out.print ("*");} Else{if (k==1| | k==i*2-1) {//First and last hit *system.out.print ("*");} Else{system.out.print ("");}} System.out.println ();}}}
Results:
* * * * * * * * * * * * * * * * ********************
Java Common 100 Cases