Rhombus is still relatively simple graphics, mainly observe the law, and do not blindly think of what is divided into two parts, in fact, the law is very easy to find. Basically only need high school mathematics knowledge is ok, simple algorithm then take absolute value can! Note the positive odd number of rows 3, 5, 7, 9… , the actual uplink number is also the total number of the widest row in the middle of the diamond (blank + diamond)!
Solid diamond
Start with the code, which is very simple:
/** */ @test public void test9() {Scanner Scanner = new Scanner(system.in); Int I = scanner.nextint (); for (int j = 0; j < i; J++) {// blank for (int k = 0; k < Math.abs(i / 2 - j); k++) { System.out.print(" "); } // * for (int k = 0; k < i - 2 * Math.abs(i / 2 - j); k++) { System.out.print("*"); } System.out.println(); }}Copy the code
The hollow diamond
On the basis of the above small changes can be:
/** */ @test public void test10() {Scanner Scanner = new Scanner(system.in); Int I = scanner.nextint (); for (int j = 0; j < i; J++) {// blank for (int k = 0; k < Math.abs(i / 2 - j); k++) { System.out.print(" "); } // *+ blank for (int k = 0; k < i - 2 * Math.abs(i / 2 - j); k++) { if (k == 0 || k == i - 2 * Math.abs(i / 2 - j) - 1) { System.out.print("*"); } else { System.out.print(" "); } } System.out.println(); }}Copy the code
So you can read the code, right? Really do not know how to find can give me a message!