使用 java for 循环绘制形状
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29486087/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Drawing shapes using java for loops
提问by Shamal Sandeep
I'm just getting started with Java and my teacher asked me to draw the following two shapes using nested for loops.
我刚刚开始使用 Java,我的老师让我使用嵌套的 for 循环绘制以下两个形状。
*
***
*****
*******
*********
***********
*
* *
* *
* *
* *
***********
However, I was able to draw the first figure using the following code.
但是,我能够使用以下代码绘制第一个图形。
class TriangleDrawing{
public static void main(String args[]){
for(int x = 1; x <= 6; x++){
for(int y = 1; y <= (6-x); y++){
System.out.print(" ");
}
for(int z = 0; z < (x + (x-1)); z++){
System.out.print("*");
}
for(int p = 1; p <= (6-x); p++){
System.out.print(" ");
}
System.out.println();
}
}
}
I'm having trouble with drawing the second figure.
Can anyone help me out drawing that one, by using nested for loops?
我在绘制第二个图形时遇到了麻烦。
任何人都可以通过使用嵌套的 for 循环来帮助我绘制那个吗?
回答by paxdiablo
It appears to me that the second figure is simply the first figure with the internal asterisks replaced with spaces.
在我看来,第二个数字只是第一个数字,内部星号替换为空格。
That means you only have to change the z
-loop so that, on each line but the first and last, it:
这意味着您只需要更改z
-loop,以便在除第一行和最后一行之外的每一行中:
- prints oneasterisk;
- prints
N
spaces, whereN
starts at one for the second line, and increases by two for each line you're on; and - print the final asterisk.
- 打印一个星号;
- 打印
N
空格,N
第二行从 1 开始,每行增加 2;和 - 打印最后一个星号。
For the first and last lines, the code will need to stay the same. For the former, you only want one asterisk, for the latter, you want all asterisks.
对于第一行和最后一行,代码需要保持不变。对于前者,您只需要一个星号,对于后者,您需要所有星号。
And, as an aside, the third loop is totally unnecessary. There's no point putting spaces at the end of the line (in this case) since no-one can see them.
而且,顺便说一句,第三个循环是完全没有必要的。在行尾(在这种情况下)放置空格是没有意义的,因为没有人可以看到它们。
Since it's classwork, I urge you to try and implement that yourself. For completeness, I include the solution below:
由于这是课堂作业,我敦促您尝试自己实施。为了完整起见,我提供了以下解决方案:
public class Test {
public static void main(String args[]){
int sz = 6;
// First line "*"
for (int y = 1; y < sz; y++)
System.out.print(" ");
System.out.println("*");
// Middle lines "* *"
for (int x = 2; x < sz; x++) {
for (int y = 1; y <= sz - x; y++)
System.out.print(" ");
System.out.print("*");
for (int y = 1; y < x * 2 - 2; y++)
System.out.print(" ");
System.out.println("*");
}
// Final line "*****"
System.out.print("*");
for (int y = 1; y < sz * 2 - 2; y++)
System.out.print("*");
System.out.println("*");
}
}
回答by Prashant
change your for loop which is printing *
as:
更改打印*
为的for 循环:
for(int z = 0; z < (x + (x-1)); z++){
if(z==0 || z ==(x + (x-1))-1 || x==6)
System.out.print("*");
else
System.out.print(" ");
}
instead of
代替
for(int z = 0; z < (x + (x-1)); z++){
System.out.print("*");
}
回答by Stultuske
In this block:
在这个块中:
for(int z = 0; z < (x + (x-1)); z++){
System.out.print("*"); }
for(int z = 0; z < (x + (x-1)); z++){
System.out.print("*"); }
check whether you are in the first and/or last element. if so, print '*' else, print " "
检查您是否在第一个和/或最后一个元素中。如果是,打印'*',否则打印“”