public class Diamonds_2 {
static private String[] d_array;
public static void main(String[] args) throws Exception {
System.out.println("Enter an odd number, please: ");
java.util.Scanner sc = new java.util.Scanner(System.in);
boolean run = true;
while (run) {
int size = sc.nextInt();
if (size % 2 != 1 || size == 1) {
System.out.println("Please, an ODD number > 1: ");
} else {
d_array = new String[size];
StringBuilder sb = new StringBuilder();
int midRow = size / 2 + 1;
// make the full row
for (int i = 0; i < size; i++) {
sb.append("*");
}
char cs = ' ';
d_array[midRow - 1] = sb.toString();
int fLetter = 0;
int lLetter = size - 1;
int i = 1;
while (i < midRow) {
sb.setCharAt(fLetter, cs);
sb.setCharAt(lLetter, cs);
d_array[midRow - 1 - i] = sb.toString();
d_array[midRow - 1 + i] = sb.toString();
fLetter++;
lLetter--;
i++;
}
}
run = false;
String msg = "If you know calculus\nthis can be done\n";
msg += "with one \"for loop\", \nand 2 lines,\n";
msg += "spaces.substring(0,x); stars.substring(0,y);";
System.out.printf("%s%n%n", msg);
System.out.println("but here it is: the sane way...\n");
for (int i = 0; i < d_array.length; i++) {
System.out.println(d_array[i]);
}
}
doSparkle();
}
static void doSparkle() {
int midRow = d_array.length / 2;
int midCol = d_array[0].length() / 2 + 1;
String slash = "\\/";
char[] slashes = slash.toCharArray();
int leftSlash = midCol-1;
int rightSlash = midCol;
int i = 1;
// System.out.printf("midRow: %d, midCol: %d, leftSlash: %d, rightSlash: %d, i: %d%n%n",
// midRow, midCol, leftSlash, rightSlash, i);
while (i <= midRow ) {
StringBuilder sb1 =
new StringBuilder(d_array[midRow - i]);
StringBuilder sb2 =
new StringBuilder(d_array[midRow + i]);
sb1.setCharAt(leftSlash, slashes[0]);
sb2.setCharAt(leftSlash, slashes[1]);
sb1.setCharAt(rightSlash, slashes[1]);
sb2.setCharAt(rightSlash, slashes[0]);
d_array[midRow - i] = sb1.toString();
d_array[midRow + i] = sb2.toString();
leftSlash--;
rightSlash++;
i++;
}
printArr();
}
static void printArr() {
for (int i = 0; i < d_array.length; i++) {
System.out.println(d_array[i]);
}
}
}
0 comments:
Post a Comment