I am making a diamond using ASCII art. Yes, I know the code is beyond sloppy. Anyways, the last part is not running inside of an else block, leaving the diamond not finished. Full code: http://pastebin.com/14HnZADe
我正在使用ASCII艺术制作钻石。是的,我知道代码超出了草率。无论如何,最后一部分没有在一个else块内运行,留下钻石未完成。完整代码:http://pastebin.com/14HnZADe
Current output:
The for loop:
for循环:
for(int i = 1; i<=size; i++) {
for(int j=1; j<=size; j++) {
if(j<i) {
System.out.print(" ");
}
else if(j==i || j>i) {
System.out.print("*");
} else {//this block is not executing, and I do not know why.
for(int ki = 1; ki<=size; ki++) { // how do I fix it?
for(int n = size; n>=1; n--) {
if(j>=i) {
System.out.print("*");
} else {
System.out.print(" ");
}
}
System.out.println();
}
}
}
System.out.println();
}
2 个解决方案
#1
1
One of your conditions has to be met before it gets to the else
block:
在到达else块之前,必须满足您的一个条件:
if(j<i) {
System.out.print(" ");
}
else if(j==i || j>i) {
j
is either less than i
or greater than i
or equal to i
. You have specified all possible conditions therefore the else
will never execute...
j小于i或大于i或等于i。您已指定所有可能的条件,因此else将永远不会执行...
#2
0
You have
if (j<i) {
//..
} else if (j==i || j>i) {
// ..
} else {
// this will never happen
}
But j==i || j>i
is the same as j>=i
. The else block will never execute because j will always be either smaller than i or greater than or equal to i. There are no other options!
但是j == i || j> i与j> = i相同。 else块永远不会执行,因为j总是小于i或大于或等于i。没有其他选择!
#1
1
One of your conditions has to be met before it gets to the else
block:
在到达else块之前,必须满足您的一个条件:
if(j<i) {
System.out.print(" ");
}
else if(j==i || j>i) {
j
is either less than i
or greater than i
or equal to i
. You have specified all possible conditions therefore the else
will never execute...
j小于i或大于i或等于i。您已指定所有可能的条件,因此else将永远不会执行...
#2
0
You have
if (j<i) {
//..
} else if (j==i || j>i) {
// ..
} else {
// this will never happen
}
But j==i || j>i
is the same as j>=i
. The else block will never execute because j will always be either smaller than i or greater than or equal to i. There are no other options!
但是j == i || j> i与j> = i相同。 else块永远不会执行,因为j总是小于i或大于或等于i。没有其他选择!