「java矩形打印」java打印数学正方形

博主:adminadmin 2022-12-07 20:24:07 62

本篇文章给大家谈谈java矩形打印,以及java打印数学正方形对应的知识点,希望对各位有所帮助,不要忘了收藏本站喔。

本文目录一览:

用java打印一个矩形

public class Rhombus {

public static void main(String[] args) {

int rows = 11;

for (int i = 0; i rows; i++) {

if (i = rows / 2) {

for (int j = 0; j = rows / 2 + i; j++) {

if (j rows / 2 - i)

System.out.print(" ");

else

System.out.print("*");

}

System.out.println();

} else {

for (int j = 0; j rows - i + rows / 2; j++) {

if (j i - rows / 2)

System.out.print(" ");

else

System.out.print("*");

}

System.out.println();

}

}

}

}

JAVA用二维数组打印出一个矩形

public class ArrayTest

{

public static void main(String[] args)

{

/*定义一个int类型二维数组*/

int[][] array=new int[5][5];

int temp=1;

/*利用for循环对二维数组赋值*/

for(int i=0;i5;i++)

{

for(int j=0;j5;j++)

{

array[i][j]=temp++;

}

}

/*输出二维数组*/

for(int i=0;i5;i++)

{

for(int j=0;j5;j++)

{

/*控制输出结果的换行*/

if(j==4)

System.out.println(array[i][j]+" ");

else

System.out.print(array[i][j]+" ");

}

}

}

}

有JAVA编写程序 打印一个矩形!

public class RectangleDemo {

private static int M = 20;

private static int N = 18;

private int m;// 长度

private int n;// 宽度

RectangleDemo(int m, int n) {

if (m 0) this.m = m;

else this.m = M;

if (n 0) this.n = n;

else this.n = N;

}

public void print() {

for (int i = 0; i n; i++) {

if (i == 0 || i == n - 1) {

for (int j = 0; j m; j++) {

if (j == 0 || j == m - 1)

System.out.print("|");

else

System.out.print("-");

}

} else {

for (int j = 0; j m; j++) {

if (j == 0 || j == m - 1)

System.out.print("|");

else

System.out.print(" ");

}

}

System.out.println();

}

}

public static void main(String[] a) {

RectangleDemo re = new RectangleDemo(20, 8);

re.print();

}

}

结果

要想以‘*’为边 把输出改成 * 就可以了

java矩形打印的介绍就聊到这里吧,感谢你花时间阅读本站内容,更多关于java打印数学正方形、java矩形打印的信息别忘了在本站进行查找喔。

The End

发布于:2022-12-07,除非注明,否则均为首码项目网原创文章,转载请注明出处。