-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDiamond.java
More file actions
51 lines (42 loc) · 1.24 KB
/
Diamond.java
File metadata and controls
51 lines (42 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package com.mycompany.diamond;
/**
*
* @author ERIC
*/
import java.util.Scanner;
public class Diamond {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Prompt user for input
System.out.print("Enter the number of rows for the diamond: ");
int rows = scanner.nextInt();
// Upper part of the diamond
for (int i = 1; i <= rows; i++) {
// Print spaces
for (int j = i; j < rows; j++) {
System.out.print(" ");
}
// Print stars
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
System.out.println();
}
// Lower part of the diamond
for (int i = rows - 1; i >= 1; i--) {
// Print spaces
for (int j = rows; j > i; j--) {
System.out.print(" ");
}
// Print stars
for (int j = 1; j <= (2 * i - 1); j++) {
System.out.print("*");
}
System.out.println();
}
scanner.close();
}
}