1. Print out the Hollow Diamond :
2. The following is the logic implementation code:
1 Packagehimi.hebao04;2 3 ImportJava.util.Scanner;4 5 Public classTestDemo08 {6 Public Static voidMain (string[] args) {7 intlength = 1;//variable that stores the side length of a diamond8Scanner sc =NewScanner (system.in);//Scan object for input data9System.out.println ("Please enter the side length of the Diamond:");TenLength = Sc.nextint ();//gets the value of the edge length of the input One /*print the top half of a diamond with a length line of triangles*/ A for(inti = 1; I <= length; i++) {//number of lines to print - for(intj = 1; J <= Length-i; J + +) {//print the left blank area, with the accompanying decrement 1 -System.out.print (""); the } - for(intK = 1; K <= 2 * i-1; k++) {//the area of the Triangle - if(k = = 1 | | k = = 2 * i-1) {//Hollow Fetch except for the first and last outer lines of each row are blank, the starting position is no longer 1 rows and 1 columns, because the front space prints the placeholder -System.out.print ("*"); +}Else { -System.out.print (""); + } A } at System.out.println (); - } - /*print the lower half of a diamond triangle the middle row is common so the number of rows is Length-1*/ - for(inti = length-1; i > 0; i--) { - for(intj = 1; J <= Length-i; J + +) { -System.out.print (""); in } - for(intK = 1; K <= 2 * i-1; k++) { to if(k = = 1 | | k = = 2 * i-1) { +System.out.print ("*"); -}Else { theSystem.out.print (""); * } $ }Panax Notoginseng System.out.println (); - } the } + A}
Java Fundamentals Hardening 07: Print out Hollow diamonds