ユーザーから整数をキャプチャし(データが有効であると想定)、整数のサイズに応じて菱形を出力するプログラムをJavaで作成しようとしています。つまり、ユーザーが入力5
すると、出力は次のようになります。
--*--
-*-*-
*---*
-*-*-
--*--
これまでのところ:
if (sqr < 0) {
// Negative
System.out.print("#Sides of square must be positive");
}
if (sqr % 2 == 0) {
// Even
System.out.print("#Size (" + sqr + ") invalid must be odd");
} else {
// Odd
h = (sqr - 1) / 2; // Calculates the halfway point of the square
// System.out.println();
for (j = 0; j < sqr; j++) {
for (i = 0; i < sqr; i++) {
if (i != h) {
System.out.print(x);
} else {
System.out.print(y);
}
}
System.out.println();
}
}
出力するだけです:
--*--
--*--
--*--
--*--
--*--
の値を下げることを考えていましたh
が、それではダイヤモンドの左側しか生成されませんでした。