Java 如何打印这个图案?
我需要打印以下图案Java 如何打印这个图案?,java,algorithm,Java,Algorithm,我需要打印以下图案 0 0 0 0 0<br> 0 # # # 0<br> 0 # 0 # 0<br> 0 # # # 0<br> 0 0 0 0 0 0 0###0 0#0#0 0###0 0 0 0 0 0 我写的代码打印了这个 0 0 0 0 0<br> 0 # # # 0<br> 0 # # # 0<br> 0 # # #
0 0 0 0 0<br>
0 # # # 0<br>
0 # 0 # 0<br>
0 # # # 0<br>
0 0 0 0 0
0
0###0
0#0#0
0###0
0 0 0 0 0
我写的代码打印了这个
0 0 0 0 0<br>
0 # # # 0<br>
0 # # # 0<br>
0 # # # 0<br>
0 0 0 0 0<br>
0
0###0
0###0
0###0
0
代码是
public class PrintPattern {
public static void main(String[] args) {
int [][] arr = new int[5][5];
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(i>0 && i<4 && j!=0 && j!=4){
arr[i][j]=1;
}
}
}
for(int i=0;i<5;i++){
for(int j=0;j<5;j++){
if(arr[i][j]==1){
System.out.print(" "+"#"+" ");
}
else
System.out.print(" "+arr[i][j]+" ");
}
System.out.println();
}
}
}
公共类打印模式{
公共静态void main(字符串[]args){
int[]arr=新int[5][5];
对于(int i=0;i你应该试着算出常数
在这里:
你应该试着算出常数
在这里:
您可以尝试下面的代码
public class PrintPattern {
public static void main(String[] args) {
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
if(i==0 || i==4) {
System.out.print("0 ");
continue;
}
else if(j>0 && j<4 && (i != 2 || j != 2)) {
System.out.print("# ");
continue;
}
else
System.out.print("0 ");
}
System.out.print("\n");
}
}
}
公共类打印模式{
公共静态void main(字符串[]args){
对于(inti=0;i您可以尝试下面的代码
public class PrintPattern {
public static void main(String[] args) {
for(int i=0;i<5;i++) {
for(int j=0;j<5;j++) {
if(i==0 || i==4) {
System.out.print("0 ");
continue;
}
else if(j>0 && j<4 && (i != 2 || j != 2)) {
System.out.print("# ");
continue;
}
else
System.out.print("0 ");
}
System.out.print("\n");
}
}
}
公共类打印模式{
公共静态void main(字符串[]args){
对于(In=0;I.是什么式样?中间有一个环?交替的环?你应该为更高的条件做清楚。在中心只有一个‘0’还是在两个方向上都是0×0的序列?0的外环,然后是一个内环,等等,行数是固定在5的吗?你的问题的第一个版本是更好的——它显示了你所尝试的和不可行的。给我们展示一些我们可以批评的代码,不要只要求一个解决方案。模式是什么?中间的一个环?交替的环?你应该弄清楚更高的条件。其他方向?外环是零,内环是#等等,就是固定在5的行数?你所说的等等到底是什么意思
?你的问题的第一个版本更好——它显示了你尝试过什么,什么不起作用。向我们展示一些代码,我们可以批评,而不仅仅是寻求解决方案。0 0 0 0 0 0 0 0
0 0 0 0 0
0#0#0
0 0 0 0 0 0 0 0 0是您代码的输出,但如果(j>0&&j0&&jyes)进行了一些修改,请参见上面的注释。请编辑您的代码。0 0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0#0 0 0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0是您代码的输出,但如果更改0(j>0&&j0&&jj是的,它可以进行一些修改,请参见上面的注释。请编辑您的代码。是的,这个算法确实解决了我的问题。谢谢。这是我在javapublic static void print(int n){int center=n/2;for(int I=0;我可以解释一下这个int dist=max(abs(x-center),abs(y-center))中实现的方法
?@n1234这就是我所说的常数
,你可以把它当作到中心点的距离,它的奇偶性意味着网格的类型。是的,这个算法确实解决了我的问题。谢谢。这是我在javapublic static void print(int n){int center=n/2;中实现的方法(inti=0;我能请你解释一下这个intdist=max(abs(x-中心),abs(y-中心))
?@n1234这就是我所说的常数,你可以把它作为到中心点的距离,它的奇偶性意味着网格的类型。