- Import Java.util.Scanner;
- public class Dengyao2 {
- Public Dengyao2 () {
- Super ();
- }
- public static void Main (string[] args) {
- System.out.println ("Please enter the diamond side length:");
- Scanner input = new Scanner (system.in);
- if (Input.hasnextint ()) {
- int a = Input.nextint ();
- Dengyao2 triangle = new Dengyao2 ();
- Triangle.function (a);
- } else {
- SYSTEM.OUT.PRINTLN ("You entered the wrong format!") Please enter an integer!! ");
- }
- Input.close ();
- }
- public void function (int x) {
- for (int i = 1; i <= x; i++) {
- for (int j = 1; J <= X-i; j + +) {
- System.out.print ("");
- }
- for (int k = 1; k <= 2 * i-1; k++) {
- System.out.print ("*");
- }
- System.out.println ();
- }
- for (int i = x-1; i > 0; i--) {
- for (int j = 1; J <= X-i; j + +) {
- System.out.print ("");
- }
- for (int k = 1; k <= 2 * i-1; k++) {
- System.out.print ("*");
- }
- System.out.println ();
- }
- }
- }
Using Java to achieve diamond-shaped printout