黑马程序员技术交流社区
标题:
如何用java写出一个矩形的边框
[打印本页]
作者:
CC_gogo
时间:
2014-10-3 10:33
标题:
如何用java写出一个矩形的边框
求解,如何用java写出一个矩形边框,例子如下:
*******
* *
* *
* *
*******
作者:
shileishihai
时间:
2014-10-3 10:39
/*
思路:1、需求规律为第一行与最后一行打印所有列,其他行只打印第一列和最后一列。
2、将所有行分为两部分:(1)第一行与最后一行为一部分;(2)其他行为一部分。
*/
class Demo1
{
public static void main(String[] args)
{
int i,j;
for(i=0;i<5;i++){
for(j=0;(i==0||i==4)&j<5;j++){
System.out.print("*");
}
for(j=0;(i!=0&&i!=4)&j<5;j++){
if(j==0||j==4){
System.out.print("*");
}
else{
System.out.print(" ");
}
}
System.out.println();
}
}
}
作者:
朴日男
时间:
2014-10-3 11:19
/*
*******
* *
* *
* *
*******
*/
class Temp
{
public static void main(String[] args)
{
DrawRec dr = new DrawRec(5,7);
dr.drawRec();
}
}
class DrawRec
{
private int side;
private int wide;
DrawRec(int side,int wide)
{
this.side = side;
this.wide = wide;
}
public void drawRec()
{
for (int x=0; x<side; x++)
{
for (int y=0; y<wide; y++)
{
if(x!=0 && x!=side-1)
{
if(y==0||y==wide-1)
System.out.print("*");
else
System.out.print(" ");
}
else
{
System.out.print("*");
}
}
System.out.println();
}
}
}
复制代码
作者:
李天富
时间:
2014-10-3 17:19
class Juxing
{
public static void main(String[] args)
{
method(20,30);
}
public static void method(int length,int width)
{
Rect.widOutter(width);
for(int i=1;i<length-1;i++)
Rect.widInner(width);
Rect.widOutter(width);
}
}
class Rect
{
public static void widOutter(int width)
{
String s=new String(new char[width]);
System.out.println(s.replace('\u0000','*'));
}
public static void widInner(int width)
{
String s=new String(new char[width-2]);
System.out.println("*"+s.replace('\u0000',' ')+"*");
}
}
复制代码
作者:
十万一千
时间:
2014-10-4 17:19
打印矩形边框
package com.itheima.番外;
public class 打点 {
public static void main(String[] args) {
int x = 5;
int y = 8;
String[][] ii = new String[x][y];
for (int i = 0; i <x; i++) {
for (int j = 0; j <y; j++) {
if ((i == 0) || (i ==x - 1) || (j == 0) || (j ==y-1)) {
ii[i][j] = "*\t";
}else {
ii[i][j] ="\t";
}
}
}
for (int i = 0; i < ii.length; i++) {
for (int j = 0; j < ii[i].length; j++) {
System.out.print(ii[i][j]);
}
System.out.println();
}
}
}
复制代码
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/)
黑马程序员IT技术论坛 X3.2