所以我一直在尝试用Java绘制一个钻石形状,我已经完成了钻石的上半部分,但下半部分没有按照我的要求打印。它不是减少到最后,而是保持不变,或者是随着它的下降而增加。
import java.util.Scanner;
public class Q1_Diamond{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for(int i = 0; i < lines/2; i++){
for(int j = 0; j < (lines-1)-i; j++){
System.out.print(" ");
}
for(int j = 0; j < i; j++){
System.out.print("*");
}
for(int j = 0; j < i+1; j++){
System.out.print("*");
}
System.out.println();
}
// Bottom half of Diamond
// Even number of lines
if(lines % 2 == 0){
for(int k = 0; k < (lines/2); k++){
for(int j = 0; j < (lines/2)+k; j++){
System.out.print(" ");
}
for(int j = 0; j < (lines/3 - 0.5f); j++){
System.out.print("*");
}
for(int j = 0; j < lines/2+1; j++){
System.out.print("*");
}
System.out.println();
}
}
// Odd number of lines
else{
not done yet...
}
}
}
移除 if
条件(即 if(lines % 2 == 0)
)的下半部分,只需用下面的循环声明重复与上半部分相同的代码即可。
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--)
完整的代码。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for (int i = 0; i < lines / 2; i++) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
// Bottom half of Diamond
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
一个示例运行:
How many lines? 10
*
***
*****
*******
*********
*********
*******
*****
***
*
另一个样本运行:
How many lines? 11
*
***
*****
*******
*********
***********
*********
*******
*****
***
*
你可以将常用的代码提取到一个方法中,例如 printLine
如下图所示。
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.print("How many lines? ");
int lines = sc.nextInt();
// Top half of Diamond
for (int i = 0; i < lines / 2; i++) {
printLine(lines, i);
}
// Bottom half of Diamond
for (int i = lines % 2 == 0 ? lines / 2 - 1 : lines / 2; i >= 0; i--) {
printLine(lines, i);
}
}
static void printLine(int lines, int i) {
for (int j = 0; j < (lines - 1) - i; j++) {
System.out.print(" ");
}
for (int j = 0; j < i; j++) {
System.out.print("*");
}
for (int j = 0; j < i + 1; j++) {
System.out.print("*");
}
System.out.println();
}
}
下面是另一种方法,它的方法略有不同。
String symb = "*";
Scanner input = new Scanner(System.in);
int max = 0;
while (max <= 1) {
System.out.print("Enter a positive no > 1: ");
max = input.nextInt();
}
System.out.println("Width and height are " + (2*max -3)+"\n");
for (int i = 1; i < max; i++) {
System.out.println(" ".repeat(max - i) + symb);
symb += "**";
}
for (int i = 2; i < max; i++) {
System.out
.println(" ".repeat(i) + symb.substring(2 * i));
}
当提示输入并提供5时,就会打印出来。
Enter a positive no > 1: 5
Width and height are 7
*
***
*****
*******
*****
***
*
解决这个问题的另一种方法是确定星号每行中的空格和星号数量的表达式。显然,这需要以行数(n)和我们所在的行来计算。
如果我们将线数从i=0到n-1,我们就可以得到
nAsterisks = 1 + 2 * min(i, n-i-1)
和
nSpaces = (n+1)/2 - 1 - min(i, n-i-1)
有了这些我们就可以写一些紧凑的Java。
static void printStar(int n)
{
for(int i=0, k=0; i<n; i++, k=Math.min(i, n-i-1))
{
for(int j=0; j<(n+1)/2-1-k; j++) System.out.print(" ");
for(int j=0; j<1+2*k; j++) System.out.print("*");
System.out.println();
}
}
对于 printStar(8)
我们得到。
*
***
*****
*******
*******
*****
***
*
和 printStar(9)
给予
*
***
*****
*******
*********
*******
*****
***
*