[英]having trouble making the sides of my diamond symmetrical
I'm trying to make a diamond out of /,\\, and astricks only using for loops and if's but the sides come out wrong, the right side is too big and the left side is too short. 我正在尝试使用/,\\制作菱形,并且仅使用for循环进行敲击,如果是,但侧面出了错,则右侧太大,而左侧太短。 this is for school by the way 这是去学校的
public class Main
{
public static void main (String[]args)
{
int n = 7;
{
for (int i = 0; i < n; i++)
{
for (int a = 0; a < (n - (i + 1)); a++)
{
System.out.print (" ");
}
for (int b = 0; b < (i * 2); b++)
{
if (b < n / 2)
{
System.out.print ("/");
}
else if (b == n / 2)
{
System.out.print ("*");
}
else if (b > n / 2)
{
System.out.print ("\\");
}
}
System.out.println ();
}
for (int i = n - 1; i >= 0; i--)
{
for (int a = 0; a < (n - (i + 1)); a++)
{
System.out.print (" ");
}
for (int b = 0; b < (i * 2); b++)
{
if (b < n / 2)
{
System.out.print ("\\");
}
else if (b == n / 2)
{
System.out.print ("*");
}
else if (b > n / 2)
{
System.out.print ("/");
}
}
System.out.println ();
}
}
}
}
These questions are not really for SO but... So what you're doing wrong is that you are trying to put everything into a single loop. 这些问题不是真正针对SO的,而是...所以您做错了,您正在尝试将所有内容放入一个循环中。 While this is possible it makes the code complex and difficult to track. 尽管这是可能的,但它使代码变得复杂且难以跟踪。 I can fix this but you have just too many miscalculations of when to print / and when to print . 我可以解决这个问题,但是您对何时打印/何时打印的估计过多。 So I just simplified it for you: 因此,我为您简化了此操作:
Do this for half of the times: 1. output the spaces 进行一半时间:1.输出空格
output the / 输出/
output the * 输出*
output the \\ 输出\\
Now do everything in reverse for the 2nd half. 现在在第二半做相反的事情。
public class Main
{
public static void main (String[]args)
{
int n = 7;
{
for (int i = 0; i < n/2; i++)
{
for (int a = 0; a < (n/2-(i+1)); a++)
{
System.out.print (" ");
}
for (int b = 0; b < i; b++)
{
System.out.print ("/");
}
System.out.print ("*");
for (int c = 0; c < i; c++)
{
System.out.print ("\\");
}
System.out.println ();
}
for (int i = n/2-1; i >= 0; i--)
{
for (int a = 0; a < (n/2-(i+1)); a++)
{
System.out.print (" ");
}
for (int b = 0; b < i; b++)
{
System.out.print ("\\");
}
System.out.print ("*");
for (int c = 0; c < i; c++)
{
System.out.print ("/");
}
System.out.println ();
}
}
}
}
I just rewrote the whole thing, just wanted to share; 我只是重写了整个内容,只是想分享;
public static void main(String[] args) {
String star = "*";
String for_slash = "/";
String back_slash = "\\";
String space = " ";
int n = 4;
for (int i = 0; i < n; i++) {
String line = "";
int d = i % n;
for (int l = n - 1; l > 0; l--) {
line += ((l < (d + 1)) ? for_slash : space) + space;
}
line += star;
for (int l = 0; l < n - 1; l++) {
line += space + ((l < d) ? back_slash : space);
}
System.out.println(line);
}
for (int i = 0; i < n; i++) {
String line = "";
int d = i % n;
for (int l = 0; l < n - 1; l++) {
line += ((l > (d - 1)) ? back_slash : space) + space;
}
line += star;
for (int l = n - 1; l > 0 ; l--) {
line += space + ((d < l) ? for_slash : space);
}
System.out.println(line);
}
}
You can edit the spacing etc. yourself 您可以自己编辑间距等
n = 6
*
/ * \
/ / * \ \
/ / / * \ \ \
/ / / / * \ \ \ \
/ / / / / * \ \ \ \ \
\ \ \ \ \ * / / / / /
\ \ \ \ * / / / /
\ \ \ * / / /
\ \ * / /
\ * /
*
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.