【发布时间】:2015-09-24 23:12:42
【问题描述】:
我正在尝试创建一个菱形(由星号制成),但是我似乎无法获得右下腿,不是吗?为什么我的代码不正确?提前致谢
import java.util.Scanner;
public class Diamond
{
public static void main(String[] args)
{
System.out.println("Enter the size of the diamond and press enter:");
Scanner kb = new Scanner(System.in);
int N = kb.nextInt();
for (int c0 = 1; c0 <= N; c0++) // establishes the number of lines in the diamond
{
for (int c1 = 0; c1 <= N-1-c0; c1++) System.out.print(" "); // establishes the number of spaces before each asterisk
System.out.print("*");
for (int c2 = 3; c2 <= c0*2; c2++) System.out.print(" "); //adds a space between both asterisks. This boolean check repeats for each line
System.out.print("*");
System.out.println();
}
for (int c0 = 1; c0 <= N; c0++) // establishes the number of lines in the diamond
{
for (int c1 = N; c1 >= N+2-c0; c1--) System.out.print(" "); // establishes the number of spaces before each asterisk
System.out.print("*");
for (int c2 = N*2; c2 >= c0+2; c2--) System.out.print(" "); //adds a space between both asterisks. This boolean check repeats for each line
System.out.print("*");
System.out.println();
}
}
}
【问题讨论】:
标签: java loops for-loop counter