Packagecom.wh.lingxing;ImportJava.util.Scanner; Public classlingxing { Public Static voidMain (string[] args) {Scanner sc=NewScanner (system.in); for (;;) {System.out.println ("Please enter the number:"); intnum =Sc.nextint (); intnum2 = 0; if(num% 2 = = 0) {//when NUM is evennum2 = NUM/2; //Upper part of Diamond for(inti = 1; I <= num2; i++) { for(intj = num2-i; J > 0; j--) {System.out.print (" "); } for(intK = 1; K <= 2 * i-1; k++) {System.out.print ("*"); } System.out.print ("\ n"); } //the lower half of the diamond for(inti = 1; i < num2; i++) { for(intj = 1; J <= I; J + +) {System.out.print (" "); } for(intK = 1; K <= num-1-2 * i; k++) {System.out.print ("*"); } System.out.print ("\ n"); } } Else{//when num is oddnum2 = (num + 1)/2; //Upper part of Diamond for(inti = 1; I <= num2; i++) { for(intj = num2-i; J > 0; j--) {System.out.print (" "); } for(intK = 1; K <= 2 * i-1; k++) {System.out.print ("*"); } System.out.print ("\ n"); } //the lower half of the diamond for(inti = 1; i < num2; i++) { for(intj = 1; J <= I; J + +) {System.out.print (" "); } for(intK = 1; K <= num-1-2 * i; k++) {System.out.print ("*"); } System.out.print ("\ n"); } } } }}
A diamond pattern implemented with a for loop