初学者 Java 受控循环未产生预期结果
Beginner Java Controlled Loop Not Producing Expected Results
我正在尝试创建一个菱形(由星号组成),但是我似乎无法让右下角的腿不对?为什么我的代码不正确?提前致谢
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();
}
}
}
我稍微调整了你的程序...你需要做更多的工作来去掉一些额外的星号。至少它会给你更多的想法:
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 i = N ; i >= 1; i--)
{
for (int j = 0; j < (N - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
}
}
如果你提取一个方法,事情会更具可读性。但首先,
for (int c2 = N*2; c2 >= c0+2; c2--) System.out.print(" ");
应该是这样的
for (int c2 = 0; c2 < (N-c0)*2; c2++) System.out.print(" ");
或类似
static String getSpaces(int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(' ');
}
return sb.toString();
}
你可以这样称呼它
for (int c0 = 0; c0 < N; c0++) {
System.out.print(getSpaces(N - c0));
System.out.print("*");
System.out.print(getSpaces(c0 * 2));
System.out.print("*");
System.out.println();
}
for (int c0 = 1; c0 <= N; c0++) {
System.out.print(getSpaces(c0));
System.out.print("*");
System.out.print(getSpaces((N - c0) * 2));
System.out.print("*");
System.out.println();
}
我正在尝试创建一个菱形(由星号组成),但是我似乎无法让右下角的腿不对?为什么我的代码不正确?提前致谢
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();
}
}
}
我稍微调整了你的程序...你需要做更多的工作来去掉一些额外的星号。至少它会给你更多的想法:
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 i = N ; i >= 1; i--)
{
for (int j = 0; j < (N - i); j++)
System.out.print(" ");
for (int j = 1; j <= i; j++)
System.out.print("*");
for (int k = 1; k < i; k++)
System.out.print("*");
System.out.println();
}
}
}
如果你提取一个方法,事情会更具可读性。但首先,
for (int c2 = N*2; c2 >= c0+2; c2--) System.out.print(" ");
应该是这样的
for (int c2 = 0; c2 < (N-c0)*2; c2++) System.out.print(" ");
或类似
static String getSpaces(int n) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < n; i++) {
sb.append(' ');
}
return sb.toString();
}
你可以这样称呼它
for (int c0 = 0; c0 < N; c0++) {
System.out.print(getSpaces(N - c0));
System.out.print("*");
System.out.print(getSpaces(c0 * 2));
System.out.print("*");
System.out.println();
}
for (int c0 = 1; c0 <= N; c0++) {
System.out.print(getSpaces(c0));
System.out.print("*");
System.out.print(getSpaces((N - c0) * 2));
System.out.print("*");
System.out.println();
}